Commit cc76abba by BellCodeEditor

save project

parent 080ee70a
Showing with 71 additions and 49 deletions
...@@ -6,26 +6,30 @@ pygame.init() # 初始化 ...@@ -6,26 +6,30 @@ pygame.init() # 初始化
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
pygame.display.set_caption("悟空酷跑")
# 载入图片 # 载入图片
background = pygame.image.load('bg.png') # 背景 background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路 road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头 stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌 cacti = pygame.image.load('cacti.png') # 仙人掌
bush = pygame.image.load('bush.png') # 灌木丛 bush = pygame.image.load('bush.png') # 灌木丛
hero = [pygame.image.load('hero1.png'), heroes = [pygame.image.load('hero1.png'),pygame.image.load('hero2.png'),pygame.image.load('hero3.png'),pygame.image.load('hero4.png'),pygame.image.load('hero5.png'),]
pygame.image.load('hero2.png'), state = "moving"
pygame.image.load('hero3.png'), i=0
pygame.image.load('hero4.png'), y=400
pygame.image.load('hero5.png')] t=30
index = 0 road_x=0
y = 400 bg_x=0
jumpState = "runing"
t = 30 block=random.choice([stone,cacti,bush])
obstacle = random.choice([bush, stone, cacti]) rect=block.get_rect()
rect = obstacle.get_rect() rect.x=1000+rect.width
rect.x = 1000 rect.y=500-rect.height
rect.y = 500 - rect.height clock=random.choice([stone,cacti,bush])
crect=clock.get_rect()
crect.x=1000+crect.width+random.randint(100,400)
if crect.x-rect.x<70 or crect.x-rect.x>-70:
crect.x+=100
crect.y=500-crect.height
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
...@@ -33,43 +37,61 @@ while True: ...@@ -33,43 +37,61 @@ while True:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
if event.type == locals.KEYDOWN: if event.type == locals.KEYDOWN:
if jumpState == "runing": if state=="moving" and event.key==locals.K_UP:
if event.key == locals.K_SPACE: state = "up"
jumpState = "up" if state!="moving" and event.key==locals.K_DOWN:
state = "speed"
if jumpState == "up": # 起跳状态 if state=="up":
if t > 0: if t>0:
y -= t y-=t
t -= 2 t-=1.5
else: else:
jumpState = "down" state="down"
if jumpState == "down": # 降落状态 if state=="down":
if t <= 30: if t<=30:
y += t y+=t
t += 2 t+=1.5
else: else:
jumpState = "runing" state="moving"
t =30 t-=1.5
if state == "speed":
# 悟空造型 if t<=30:
wukong = hero[index] y+=t
if jumpState == "runing": # 跑步状态下 t+=2
index += 1 else:
if index >= 5: state="moving"
index = 0 t-=2
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) # 远处背景 bg_x-=10
screen.blit(road, (0, 500)) # 路 if bg_x<-1000:
screen.blit(wukong, (150, y)) # 悟空 bg_x=0
screen.blit(background, (bg_x, 0))
if rect.x <= 0-rect.width: # 障碍物消失 road_x-=30
# 创建障碍物对象 if road_x<-1000:
obstacle = random.choice([bush,stone,cacti]) road_x=0
rect = obstacle.get_rect() screen.blit(road, (road_x, 500))
rect.x = 1000 screen.blit(heroes[i], (150, y))
rect.y = 500 - rect.height if i<4 and state == "moving":
rect.x -= 8 i+=1
screen.blit(obstacle, (rect.x, rect.y)) else:
i=0
if rect.x<0-rect.width:
block=random.choice([stone,cacti,bush])
rect=block.get_rect()
rect.x=1000+rect.width
rect.y=500-rect.height
if crect.x<0-crect.width:
clock=random.choice([stone,cacti,bush])
crect=clock.get_rect()
crect.x=1000+crect.width+random.randint(100,400)
if crect.x-rect.x<70 or crect.x-rect.x>-70:
crect.x+=100
crect.y=500-crect.height
rect.x-=30
screen.blit(block,(rect.x,rect.y))
crect.x-=random.randint(5,20)
screen.blit(clock,(crect.x,crect.y))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(60) FPS.tick(20)
\ 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