使用pygame实现滑雪小人游戏
'''
创建一个窗口:
设置窗口的宽高,窗口的背景颜色填充,窗口的标题,窗口的图标,设置开始前的背景音乐
创建一个雪人类:
传入一个雪人图片
设置雪人图片的大小,雪人的初始位置
雪人移动的速度,雪人在移动,在移动方向的同时显示相应的图片背景
以及相应的背景配音,雪人移动设置雪人的边界不出界,雪人撞树播放相应背景音乐以及背景图
雪人吃到旗帜播放相应背景音乐
创建一个树的类:
随机生成树的位置以及个数,设置雪人撞树一次扣100分的机制
创建一个旗帜的类:
随机生成旗帜的位置以及个数,设置雪人吃到一个旗帜加10分的机制
树和旗帜的类可放在同一个类中
'''
import pygame
import random
snowman_images = ['./skier_down.png', './skier_crash.png', './skier_left1.png', './skier_left2.png',
'./skier_right1.png', './skier_right2.png', ]
class Snowman(pygame.sprite.Sprite):
def __init__(self,image,speed):
'''
:param image: 图片的路径
:param location: 列表类型的属性,[x横轴坐标,y纵轴的坐标]
:param speed: 列表的类型,[横轴的速度,纵轴的速度]
'''
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.rect=self.image.get_rect()
self.rect.center=[320,100]#设置图像的初始位置
# self.rect.left,self.rect.top=location
# self.rect.left=location[0]
# self.rect.right=location[1]
self.angle=0
def turn(self,status):
'''这个方法实现的是小人的转向问题
左右两个状态,主要是转向的幅度不同
右转一样
status:代表的是用户输入的状态
'''
if status == 0:
self.image = pygame.image.load(snowman_images[status])
elif status == 2:
self.image = pygame.image.load(snowman_images[status])
elif status == 4:
self.image = pygame.image.load(snowman_images[status])
class TreeFlag(pygame.sprite.Sprite):
def __init__(self, image, location,type):
'''
:param image:这是旗帜或者树木
:param location: 位置
:param type: 标注小树还是旗帜
:param speed: 列表类型
'''
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image) # <Surface(30x64x32 SW)>
self.rect = self.image.get_rect() # <rect(0, 0, 30, 64)>
self.rect.center=location
self.type=type
self.passed=False
def update(self):
global speed
self.rect.centery -= speed[1]
if self.rect.centery<0:
self.kill()
def create_obstacle():
#这个函数用来真正创建小树和旗帜,并且它的大小和我们的游戏界面的大小一致,我们设计将这个界面
#分成10份,创建10个精灵分布在这个界面上,注意一定不能发生精灵重叠的现象,所以创建
global group
location_list = []
for i in range(10):
row = random.randint(0, 9)
col = random.randint(0, 9)
location = [row * 64 + 25, col * 64 + 30 + 640]
if location not in location_list:
# 判断位置是否在位置列表里面,如果不在则创建新的精灵
location_list.append(location)
type = random.choice(['tree', 'flag'])
if type == 'tree':
image = './skier_tree.png'
elif type == "flag":
image = './skier_flag.png'
obj = TreeFlag(image, location, type)
group.add(obj)
def draw_win(snow):
'''绘制屏幕,除去之前的痕迹'''
global window
global group
window.fill([255,255,255])
group.draw(window)
window.blit(snow.image, snow.rect)
window.blit(text_obj, [10, 50])
pygame.display.flip()
if __name__ == '__main__':
#设置初始化操作:创建窗口,设置帧
pygame.init()
window = pygame.display.set_mode((640, 640))
pygame.display.set_caption('skiergame')
window.fill([255, 255, 255]) # rgb值
snow=Snowman(snowman_images[0],[0,1])
window.blit(snow.image, snow.rect)
#设置帧,创建一个时间对象
clock=pygame.time.Clock()
position=0
#记录小树所在的那张图的位置
speed=[0,2]
#设置速度
group = pygame.sprite.Group()
font=pygame.font.Font(None,50)
score=0
create_obstacle()
x = 235
y = 0
while True:
clock.tick(200)
Keep_moving = pygame.key.get_pressed()
for even in pygame.event.get():
if even.type==pygame.QUIT:
print("关闭窗口")
exit()
if Keep_moving[pygame.K_UP] or Keep_moving[pygame.K_w]:
snow.rect.centery = snow.rect.centery - 1
snow.image = pygame.image.load('./skier_down.png')
if snow.rect.top< 0:
snow.rect.top = 0
elif Keep_moving[pygame.K_DOWN] or Keep_moving[pygame.K_s]:
print('down')
snow.rect.centery = snow.rect.centery + 1
snow.image = pygame.image.load('./skier_down.png')
if snow.rect.bottom>640:
snow.rect.bottom=640
elif Keep_moving[pygame.K_LEFT] or Keep_moving[pygame.K_a]:
print('left')
snow.rect.centerx = snow.rect.centerx - 1
snow.image=pygame.image.load('./skier_left1.png')
if snow.rect.centerx<15:
snow.rect.centerx=15
elif Keep_moving[pygame.K_RIGHT] or Keep_moving[pygame.K_d]:
print('right')
snow.rect.centerx = snow.rect.centerx + 1
snow.image = pygame.image.load('./skier_right1.png')
if snow.rect.centerx>640-15:
snow.rect.centerx=640-15
else:
snow.image = pygame.image.load('./skier_down.png')
window.fill([255, 255, 255])
position += speed[1]
#标注小树以及旗帜的位置,随着循环跟着小树的变化而变化
if position>=640:
create_obstacle()
position=0
group.update()
#调用函数,绘制图片
return_list=pygame.sprite.spritecollide(snow,group,False)
if return_list:
# print('发生碰撞')
if return_list[0].type == 'tree' and return_list[0].passed == False:
print('碰到树')
score = score - 100
snow.image = pygame.image.load('./skier_crash.png')
draw_win(snow)
pygame.time.delay(1000)
snow.image = pygame.image.load(snowman_images[0])
return_list[0].passed = True
elif return_list[0].type == 'flag' and return_list[0].passed==False:
print('碰到旗帜')
score = score + 10
return_list[0].passed = True
text_obj=font.render('score:'+str(score),True,(0,0,0))
draw_win(snow)
pygame.display.update()