Commit 7f1415d2 by BellCodeEditor

save project

parent 5d0d8f13
Showing with 64 additions and 45 deletions
...@@ -12,6 +12,14 @@ class Block(pygame.sprite.Sprite): ...@@ -12,6 +12,14 @@ class Block(pygame.sprite.Sprite):
self.rect.x = 1000 self.rect.x = 1000
self.rect.y = 500-self.rect.height self.rect.y = 500-self.rect.height
class Player(pygame.sprite.Sprite):
def __init__(self,image):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x = 150
self.rect.y = 400
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
...@@ -37,6 +45,8 @@ time = 0 ...@@ -37,6 +45,8 @@ time = 0
road_x = 0 road_x = 0
gamestate = True
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:
...@@ -46,56 +56,65 @@ while True: ...@@ -46,56 +56,65 @@ while True:
if jumpState == "runing": if jumpState == "runing":
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
jumpState = "up" jumpState = "up"
if jumpState == "up": # 起跳状态
if t > 0:
y -= t
t -= 2
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
t += 2
else:
jumpState = "runing"
t =30
# 悟空造型 wukong =Player(hero[index])
wukong = hero[index]
if jumpState == "runing": # 跑步状态下 if jumpState == "runing": # 跑步状态下
index += 1 index += 1
if index >= 5: if index >= 5:
index = 0 index = 0
# 将背景图画上去 if gamestate == True:
screen.blit(background, (0, 0)) # 远处背景 # # 悟空造型
screen.blit(road, (road_x, 500)) # 路 # wukong =Player(hero[index])
screen.blit(wukong, (150, y)) # 悟空 # if jumpState == "runing": # 跑步状态下
# index += 1
# if index >= 5:
# index = 0
if jumpState == "up": # 起跳状态
if t > 0:
y -= t
wukong.rect.y = y
t -= 2
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
wukong.rect.y = y
t += 2
else:
jumpState = "runing"
t =30
# 将背景图画上去
screen.blit(background, (0, 0)) # 远处背景
screen.blit(road, (road_x, 500)) # 路
screen.blit(wukong.image, (150, y)) # 悟空
# 道路移动
road_x -= 8
if road_x<-1000:
road_x = 0
# 道路移动
road_x -= 8
if road_x<-1000:
road_x = 0
# if obstacle.rect.x <= 0-obstacle.rect.width: # 障碍物消失 time += 1
# # 创建障碍物对象 if time >= 60:
# obstacle = Block(bush,stone,cacti) time = 0
# obstacle.rect.x -= 8 num = random.randint(0,50)
# screen.blit(obstacle.image, (obstacle.rect.x, obstacle.rect.y)) if num>20:
obstacle = Block(bush,stone,cacti)
Block_list.add(obstacle)
for spite in Block_list:
spite.rect.x -= 8
screen.blit(spite.image, (spite.rect.x, spite.rect.y))
if spite.rect.x <= 0-spite.rect.width:
spite.kill()
time += 1 if pygame.sprite.collide_rect(wukong,spite):
if time >= 60: game_over = pygame.image.load("gameover.png")
time = 0 screen.blit(game_over,(400,200))
num = random.randint(0,50) gamestate = False
if num>20:
obstacle = Block(bush,stone,cacti)
Block_list.add(obstacle)
for spite in Block_list:
spite.rect.x -= 8
screen.blit(spite.image, (spite.rect.x, spite.rect.y))
if spite.rect.x <= 0-spite.rect.width:
spite.kill()
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(60) FPS.tick(60)
\ 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