Commit f2aea7bf by BellCodeEditor

auto save

parent 64044261
Showing with 26 additions and 5 deletions
......@@ -20,6 +20,11 @@ hero = [pygame.image.load('hero1.png'), # 五张悟空跑步图片放入一
index = 0 # 作为列表的下标
y = 400 # 纵坐标
jumpState = "runing" # 表示正在跑步
t = 30 # 表示跳跃时每次移动的距离
obstacle = random.choice([bush, stone, cacti])
rect = obstacle.get_rect()
rect.x = 1000
rect.y = 500 - rect.height
while True:
for event in pygame.event.get():
......@@ -27,20 +32,26 @@ while True:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN: # 发生键盘按键事件
if jumpState == "runing":
if event.key == locals.K_SPACE: # 检测是否按下空格键
jumpState = "up" # 表示上升
if jumpState == "up": # 上升
if y > 150:
y -= 5
if jumpState == "up": # 起跳
if y > 0:
y -= t
t -= 2
else:
jumpState = "down" # 表示降落
if jumpState = "down": # 降落
if y < 400:
y += 5
if y < 30:
y += t
t += 2
else:
jumpState = "runing"
t = 30
wukong = hero[index] # 按照下标,取出悟空的造型并展示出来
if jumpState == "runing":
index += 1 # 变量增加1
if index >= 5: # 诺超出列表长度
index = 0 # 重新设置为0
......@@ -48,6 +59,15 @@ while True:
screen.blit(background, (0, 0))
screen.blit(road, (0, 500))
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()
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