您的位置:首页 > 编程语言 > Python开发

python编写滑雪小游戏

2019-03-30 09:30 489 查看

效果:

1.创建滑雪小人的精灵类:小人拥有一个移动的方法move()用来控制他的方向。
创建精灵可以让后面的碰撞实现起来更加方便。pygame.key.get_pressed() 方法是用来实现一直按住方向键,让小人一直移动的。如果不用这个方法,小人一直按住就只会移动一下。

class SkierClass(pygame.sprite.Sprite):
'''
image:图片路径
location:列表类型的属性[x横轴坐标,y纵轴坐标]
speed:[横轴速度,纵轴速度]
'''
def __init__(self,image,location,speed):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(image)
self.rect=self.image.get_rect()#<rect(0, 0, 30, 64)>
self.rect.left=location[0]
self.rect.top = location[1]#设置图像的初始位置
self.speed=speed
def move(self):
isPress = pygame.key.get_pressed()  # bool :True
if isPress[pygame.K_a] == True or isPress[pygame.K_LEFT] == True:
if self.rect.left>0:
self.rect.left -= 1
self.image = imageLeft1
elif isPress[pygame.K_s] == True or isPress[pygame.K_DOWN] == True:
if self.rect.bottom < window.get_height() - 64:
self.rect.bottom += 1
self.image = imageDown
elif isPress[pygame.K_d] == True or isPress[pygame.K_RIGHT] == True:
if  self.rect.right < window.get_width() - 30:
self.rect.left += 1
self.image = imageRight1

2.创建树和旗子的精灵类:树是障碍物,旗子可以得分,该精灵类用type属性区分它们。
update()方法是重写了原来的update()方法。用来让树和旗子以一定的速度向上移动。

class TreeFlag(pygame.sprite.Sprite):
def __init__(self,image,location,speed,type):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(image)
self.rect=self.image.get_rect()
self.rect.left,self.rect.top = location
self.speed=speed
self.type=type
self.passed = False
def update(self):
self.rect = self.rect.move(self.speed)
self.speed[1] = -1
if self.rect.top < -48:
self.kill()

3.该方法用来实现小人在窗口的加载

def create_ski():
window.fill([255, 255, 255])
window.blit(ski.image, ski.rect)

4.这个方法是用来生成树和旗子的。group是一个树和旗子的精灵组。我先把窗口分成了十分,避免树和旗子之间的间隙太小。又定义了一个位置的列表,避免随机产生的树和旗子会有重叠的情况。

def create_obstacle():
global  group
llist = []
for a in range(0, 10):
type = random.choice(['tree', 'flag'])
x = random.randint(0, 9)
y = random.randint(0, 9)
location = [x * 60, y * 60 + 600]
if location not in llist:
llist.append(location)
if type == 'tree':
image = './pic/skier_tree.png'
else:
image = './pic/skier_flag.png'
obj = TreeFlag(image, location, [0, 1], type)
group.add(obj)

5.这是一个检测精灵碰撞的方法。我用了把树和旗子用过循环从精灵组中拿出来,在每一个元素都判断是否与小人碰撞的方法检测。如果树和人相撞是不把树从精灵组中删除的,会产生一直扣分的情况,我用了树和旗子本身的一个属性passed来判断是否已经是相撞过的数,如果已经撞到了,就改变他的这个属性,就不会在发生一直扣分的情况了。

def collide_check():
# 检测人和树是否碰撞
global  ski
global score
global group
for obj in group:
if obj.type == 'tree' and obj.passed == False:
if pygame.sprite.collide_rect(ski , obj) :#人撞到树,切换图片,扣分
ski.image = imageCrash
score -= 100
draw_win()
pygame.time.delay(1000)
ski.image = imageDown
obj.passed=True

else:
if pygame.sprite.collide_rect(ski , obj) and obj.passed == False:#人撞到旗,不切图片,加分
group.remove(obj)
score += 10

6.这是一个重新绘制屏幕的方法,这里是为了不让物体移动后产生在屏幕上出现‘分身’的状况。需要把所有需要在屏幕上加载的对象都放在这里加载。不然就无法在屏幕上出现。

def draw_win():
global window
global group
global  ski
window.fill([255, 255, 255])

group.draw(window)
window.blit(ski.image, ski.rect)
window.blit(text,(10,10))
pygame.display.flip()

7.main函数:main函数定义了一些必要的对象。 其中定义了一个position,它是用来让整个窗口移动,并且在移动完整个屏幕后,重新加载一个窗口,达到窗口不断循环播放的一个作用。

if __name__ == '__main__':
#设置帧,创建了一个时间对象
clock = pygame.time.Clock()
position = 0
score = 0
# 创建1个小人
ski = SkierClass('./pic/skier_down.png', [300, 10], [0, 0])
#创建多棵树和旗子
group=pygame.sprite.Group()
create_obstacle()

myFont = pygame.font.Font("./abc.TTF", 30)
red = (255, 0, 0)

while True:
clock.tick(400)
for obj in pygame.event.get():
if obj.type == pygame.QUIT:  # 关闭窗口
exit()
position += 1
if position >= 600:
create_obstacle()
position=0
# 加载分数
text = myFont.render('Score:%d' % score, False, red)
window.blit(text, (10, 10))

create_ski()#创建人
ski.move()#人移动
group.update()#树旗移动
collide_check()#碰撞检测
draw_win()#调用函数,绘制图片
pygame.display.update()# 刷新  *必须要刷新,不然就不会显示
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: