Commit 51185de7 by BellCodeEditor

save project

parent da852cf9
Showing with 18 additions and 9 deletions
import pygame import pygame
import random
from pygame import locals from pygame import locals
pygame.init() # 初始化 pygame.init() # 初始化
...@@ -9,7 +10,7 @@ FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) ...@@ -9,7 +10,7 @@ FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
y=400 y=400
t=30 t=30
jump="running" jump="running"
pygame.display.set_caption("悟空跑障碍赛") 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') # 石头
...@@ -22,25 +23,29 @@ hero = [pygame.image.load('hero1.png'), ...@@ -22,25 +23,29 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero5.png')] pygame.image.load('hero5.png')]
index=0 index=0
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():
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 jump=="running":
jump="up" if event.key==locals.K_SPACE:
jump="up"
if jump=="up": if jump=="up":
if y > 0: if t > 0:
y-=t y-=t
t-=2 t-=2
else: else:
jump = "down" jump = "down"
if jump=="down": if jump=="down":
if y < 30: if t <= 30:
y +=t y +=t
t+=2 t+=2
else: else:
...@@ -48,9 +53,13 @@ while True: ...@@ -48,9 +53,13 @@ while True:
t=30 t=30
wukong = hero[index] wukong = hero[index]
index+=1 if jump=="running":
if index == 5: index+=1
index = 0 if index == 5:
index = 0
if rect.x <= 0-rect.width:
rect.x-=8
screen.blit(obstacle,(rect.x,rect.y))
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
......
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