Commit 9a557cef by BellCodeEditor

auto save

parent 04844f5a
Showing with 80 additions and 41 deletions
...@@ -3,6 +3,29 @@ from pygame import locals ...@@ -3,6 +3,29 @@ from pygame import locals
import random # 导入random模块 import random # 导入random模块
pygame.init() # 初始化 pygame.init() # 初始化
class Block(pygame.sprite.Sprite): # 创建障碍物精灵类,类名为Block
# def定义 __init__()函数加上参数,形参123为障碍物123
def __init__(self,image1,image2,image3):
super().__init__() # 调用super()函数继承父类属性
# 加载障碍物图片
self.image = random.choice([image1,image2,image3]) # 随机选择一种作为障碍物
# 根据障碍物位图的宽高设置矩形
self.rect = self.image.get_rect()
# 障碍物绘制坐标
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
class Player(pygame.sprite.Sprite): # 悟空
def __init__(self, image): # def定义 __init__()加上参数,图片参数
super().__init__() # 用super()方法继承父类属性
# 加载悟空精灵图像
self.image = image # 重写image属性
# image的get_rect()方法,可以返回pygame.Rect(0,0,图像宽,图像高)
self.rect = self.image.get_rect() # 重写rect属性
self.rect.x = 150 # 设置初始x坐标
self.rect.y = 400 # 设置初始y坐标
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
...@@ -22,10 +45,11 @@ index = 0 ...@@ -22,10 +45,11 @@ index = 0
y = 400 y = 400
jumpState = "runing" jumpState = "runing"
t = 30 t = 30
obstacle = random.choice([bush, stone, cacti]) road_x = 0 # 设置道路x坐标为0并赋值给变量road_x
rect = obstacle.get_rect() bg_x = 0 # 设置远景x坐标为0并赋值给变量bg_x
rect.x = 1000 time = 0
rect.y = 500 - rect.height
block_list = pygame.sprite.Group() # 创建精灵组
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
...@@ -36,40 +60,55 @@ while True: ...@@ -36,40 +60,55 @@ while True:
if jumpState == "runing": if jumpState == "runing":
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
jumpState = "up" jumpState = "up"
if gamestate == True: # if判断当gamestate的值为True时
if jumpState == "up": # 起跳状态 if jumpState == "up": # 起跳状态
if t > 0: if t > 0:
y -= t y -= t
t -= 2 t -= 2
else: else:
jumpState = "down" jumpState = "down"
if jumpState == "down": # 降落状态 if jumpState == "down": # 降落状态
if t <= 30: if t <= 30:
y += t y += t
t += 2 t += 2
else: else:
jumpState = "runing" jumpState = "runing"
t =30 t =30
# 悟空造型 # 悟空造型
wukong = hero[index] wukong = Player(hero[index]) # 将设置悟空造型的代码改成用类来实现
if jumpState == "runing": # 跑步状态下 if jumpState == "runing": # 跑步状态下
index += 1 index += 1
if index >= 5: if index >= 5:
index = 0 index = 0
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) # 远处背景 bg_x -= 1 # 将远景x坐标每次循环都-1
screen.blit(road, (0, 500)) # 路 if bg_x <= -1000: # if判断当远景x坐标小于-1000
screen.blit(wukong, (150, y)) # 悟空 bg_x = 0 # 远景x坐标回到0
screen.blit(background, (bg_x, 0)) # 远景代码中x坐标改为bg_x
if rect.x <= 0-rect.width: # 障碍物消失 road_x -= 8 # 将道路x坐标每次循环都-8 使道路和障碍物以相同速度向左移动
if road_x <= -1000: # if判断当道路x坐标小于-1000
road_x = 0 # 道路x坐标回到0
screen.blit(road, (road_x, 500)) # 道路背景代码中x坐标改为变量road_x
screen.blit(wukong.image, (150, y)) # 悟空代码里参数1改为.image
# 创建障碍物对象 # 创建障碍物对象
obstacle = random.choice([bush,stone,cacti]) time += 1 # 将time值+1
rect = obstacle.get_rect() if time >= 60: # if判断time达到60
rect.x = 1000 r = random.randint(0, 100) # 随机数0,100并赋值给遍历r
rect.y = 500 - rect.height if r > 40: # 判断r>40
rect.x -= 8 obstacle = Block(bush,cacti,stone) # 创建障碍物精灵
screen.blit(obstacle, (rect.x, rect.y)) block_list.add(obstacle) # 将精灵添加进精灵组
# 刷新画面 time = 0 # 将变量time设为0
pygame.display.update() for sprite in block_list: # for循环,遍历、展示障碍物
FPS.tick(60) sprite.rect.x -= 8 # 将精灵的x坐标-8
\ No newline at end of file # 将程序渲染出来
screen.blit(sprite.image,(sprite.rect.x, sprite.rect.y))
if sprite.rect.x <= 0-sprite.rect.width: # if判断精灵是否完全消失在窗口左边
sprite.kill() # 使用kill()方法将自己清除
if pygame.sprite.collide_rect(wukong, sprite): # 精灵碰撞检测
gemeover = pygame.image.load('gameover.png') # 导入图片素材,游戏结束
screen.blit(gameover, (400, 200)) # 将游戏结束展示在窗口上
gamestate = False # 将变量gamestate的值设为False
# 刷新画面
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment