Commit f2aea7bf by BellCodeEditor

auto save

parent 64044261
Showing with 31 additions and 10 deletions
...@@ -20,6 +20,11 @@ hero = [pygame.image.load('hero1.png'), # 五张悟空跑步图片放入一 ...@@ -20,6 +20,11 @@ hero = [pygame.image.load('hero1.png'), # 五张悟空跑步图片放入一
index = 0 # 作为列表的下标 index = 0 # 作为列表的下标
y = 400 # 纵坐标 y = 400 # 纵坐标
jumpState = "runing" # 表示正在跑步 jumpState = "runing" # 表示正在跑步
t = 30 # 表示跳跃时每次移动的距离
obstacle = random.choice([bush, stone, cacti])
rect = obstacle.get_rect()
rect.x = 1000
rect.y = 500 - rect.height
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
...@@ -27,27 +32,42 @@ while True: ...@@ -27,27 +32,42 @@ while True:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
if event.type == locals.KEYDOWN: # 发生键盘按键事件 if event.type == locals.KEYDOWN: # 发生键盘按键事件
if event.key == locals.K_SPACE: # 检测是否按下空格键 if jumpState == "runing":
jumpState = "up" # 表示上升 if event.key == locals.K_SPACE: # 检测是否按下空格键
if jumpState == "up": # 上升 jumpState = "up" # 表示上升
if y > 150:
y -= 5 if jumpState == "up": # 起跳
if y > 0:
y -= t
t -= 2
else: else:
jumpState = "down" # 表示降落 jumpState = "down" # 表示降落
if jumpState = "down": # 降落 if jumpState = "down": # 降落
if y < 400: if y < 30:
y += 5 y += t
t += 2
else: else:
jumpState = "runing" jumpState = "runing"
t = 30
wukong = hero[index] # 按照下标,取出悟空的造型并展示出来 wukong = hero[index] # 按照下标,取出悟空的造型并展示出来
index += 1 # 变量增加1 if jumpState == "runing":
if index >= 5: # 诺超出列表长度 index += 1 # 变量增加1
index = 0 # 重新设置为0 if index >= 5: # 诺超出列表长度
index = 0 # 重新设置为0
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
screen.blit(road, (0, 500)) screen.blit(road, (0, 500))
screen.blit(wukong, (150, y)) # 悟空 screen.blit(wukong, (150, y)) # 悟空
if rect.x <= 0-rect.width: # 判段障碍物x坐标超过0减去障碍物的宽度(说明障碍物完全消失在画面中)
# 创建障碍物对象
obstacle = random.choice([bush, stone, cacti])
rect = obstacle.get_rect()
rect.x = 1000
rect.y = 500 - rect.height
rect.x -= 8 # 让障碍物的x坐标不断减小
screen.blit(obstacle, (rect.x, rect.y)) # 将它渲染出来
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(60) 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