Commit f46b8148 by BellCodeEditor

save project

parent 19a042df
Showing with 29 additions and 13 deletions
import pygame import pygame
import random
from pygame import locals from pygame import locals
pygame.init() # 初始化 pygame.init() # 初始化
...@@ -18,7 +19,12 @@ hero = [pygame.image.load('hero1.png'), ...@@ -18,7 +19,12 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero5.png'),] pygame.image.load('hero5.png'),]
index = 0 index = 0
y=400 y=400
t=30
x=150 x=150
obstacle=random.choice([apple,stone,cacti])
rect=obstacle.get_rect()
rect.x=1000
rect.y=500-rect.height
jumpState="runing" jumpState="runing"
pygame.display.set_caption("悟空快跑") pygame.display.set_caption("悟空快跑")
wkzx=['hero1.png','hero2.png','hero3.png','hero4.png','hero5.png'] wkzx=['hero1.png','hero2.png','hero3.png','hero4.png','hero5.png']
...@@ -27,28 +33,38 @@ while True: ...@@ -27,28 +33,38 @@ while True:
if event.type == locals.QUIT: if event.type == locals.QUIT:
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:
jumpState="up"
if jumpState=="up": if jumpState=="up":
if y>150: if t>0:
y-=5 y-=t
x+=1 t-=2
else: else:
jumpState="down" jumpState="down"
if jumpState=="down": if jumpState=="down":
if y<400: if t<=30:
y+=5 y+=t
x-=1 t+=2
else: else:
jumpState="runing" jumpState="runing"
t=40
wukong=hero[index] wukong=hero[index]
index+=1 if jumpState=="runing":
if index>=5: index+=1
index=0 if index>=5:
index=0
if rect.x<=0-rect.width:
obstacle=random.choice([apple,stone,cacti])
rect=obstacle.get_rect()
rect.x=1000
rect.y=500-rect.height
rect.x-=12
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
screen.blit(road, (0, 500)) screen.blit(road, (0, 500))
screen.blit(wukong, (x, y)) screen.blit(wukong, (x, y))
screen.blit(obstacle,(rect.x,rect.y))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(60) FPS.tick(30)
\ No newline at end of file \ 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