Python开发教程:基于pygame的兔子吃月饼小游戏
小游戏规则简介
实现
初始化游戏窗口
import sys
import pygame
width = 800
height = 800
pygame.init()
windows = pygame.display.set_mode((width, height))
pygame.display.set_caption('兔子吃月饼!!')
while True:
windows.fill((204, 204, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.flip()
游戏逻辑
实现玩家类
class Rabbit:
"""
玩家兔子类
"""
def __init__(self, top, left, height, width):
# 初始位置及大小
self.top = top
self.left = left
self.height = height
self.width = width
# 游戏人物的初始位置
self.rect = pygame.Rect(self.left, self.top, self.width, self.height)
self.player_image = pygame.image.load('兔子.png')
self.player_stretched_image = pygame.transform.scale(self.player_image, (height, width))
# 控制移动变量
self.move_left = False
self.move_right = False
self.move_up = False
self.move_down = False
# 每次移动的大小
self.MOVESPEED = 5
# 重量
self.weight = 5
def move(self):
"""
控制移动
:return:
"""
if self.move_down and self.rect.bottom < height:
self.rect.top += self.MOVESPEED
self.rect.bottom += self.MOVESPEED
if self.move_up and self.rect.top > 0:
self.rect.top -= self.MOVESPEED
self.rect.bottom -= self.MOVESPEED
if self.move_left and self.rect.left > 0:
self.rect.left -= self.MOVESPEED
self.rect.right -= self.MOVESPEED
if self.move_right and self.rect.right < width:
self.rect.left += self.MOVESPEED
self.rect.right += self.MOVESPEED
实现月饼类
class MoonCake:
"""
月饼类
"""
def __init__(self):
# 位置及大小
self.rect = pygame.Rect(random.randint(0, 750), random.randint(0, 750), 20, 20)
# 图像
self.moon_cake_image = pygame.image.load("./月饼/月饼{}.png".format(random.randint(1, 8)))
交互逻辑
def game_run():
global width
global height
# 是否结束
end = False
# 创建时钟对象 (可以控制游戏循环频率)
clock = pygame.time.Clock()
# 月饼计数器
moon_cake_limit = 20
player = None
if not player:
player = Rabbit(300, 100, 64, 64)
moon_cakes = []
for i in range(20):
moon_cake = MoonCake()
moon_cakes.append(moon_cake)
# 游戏循环
while True and not end:
# 给屏幕填充颜色
windows.fill((204, 204, 255))
# 监听键盘事件
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
player.move_right = False
player.move_left = True
if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
player.move_left = False
player.move_right = True
if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]:
player.move_down = False
player.move_up = True
if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]:
player.move_up = False
player.move_down = True
player.move()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT or event.key == K_a:
player.move_left = False
if event.key == K_RIGHT or event.key == K_d:
player.move_right = False
if event.key == K_UP or event.key == K_w:
player.move_up = False
if event.key == K_DOWN or event.key == K_s:
player.move_down = False
# 月饼不足20时生成新的月饼
if len(moon_cakes) < moon_cake_limit:
# 生成新的月饼
moon_cakes.append(MoonCake())
# 画兔子
windows.blit(player.player_stretched_image, player.rect)
# 显示兔子的重量
text = pygame.font.SysFont("microsoftyaheimicrosoftyaheiui", 30)
text_fmt = text.render("重量:{}斤!".format(player.weight), True, (255, 255, 255))
windows.blit(text_fmt, (0, 0))
# 画月饼
for moon_cake in moon_cakes:
windows.blit(moon_cake.moon_cake_image, moon_cake.rect)
# 碰撞检测
for moon_cake in moon_cakes:
if player.rect.colliderect(moon_cake.rect):
moon_cakes.remove(moon_cake)
# 吃到月饼兔子会变重
player.weight += 2
pygame.display.flip()
pygame.display.update()
# 通过时钟对象指定循环频率
clock.tick(40)
if player.weight > 10:
end = True
while end:
windows.fill((204, 204, 255))
text = pygame.font.SysFont("microsoftyaheimicrosoftyaheiui", 50)
text_fmt1 = text.render("你的兔子撑死了!!".format(player.weight), True, (255, 255, 255))
text_fmt2 = text.render("game over!".format(player.weight), True, (255, 255, 255))
windows.blit(text_fmt1, (200, 200))
windows.blit(text_fmt2, (260, 300))
clock.tick(40)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
end = False
player = Rabbit(random.randint(0, 750), random.randint(0, 750), 64, 64)
moon_cakes.clear()
for i in range(20):
moon_cake = MoonCake()
moon_cakes.append(moon_cake)
pygame.display.flip()
pygame.display.update()
搜索下方加老师微信
老师微信号:XTUOL1988【切记备注:学习Python】
领取Python web开发,Python爬虫,Python数据分析,人工智能等精品学习课程。带你从零基础系统性的学好Python!
*声明:本文于网络整理,版权归原作者所有,如来源信息有误或侵犯权益,请联系我们删除或授权
评论