Commit 63dd28f9 by BellCodeEditor

save project

parent d7fc96d1
Showing with 19 additions and 18 deletions
...@@ -9,6 +9,15 @@ class Block(pygame.sprite.Sprite): ...@@ -9,6 +9,15 @@ class Block(pygame.sprite.Sprite):
self.rect=self.image.get_rect() self.rect=self.image.get_rect()
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
pygame.init() # 初始化 pygame.init() # 初始化
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
...@@ -21,22 +30,16 @@ road = pygame.image.load('road.png') # 路 ...@@ -21,22 +30,16 @@ 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') # 灌木丛
gameover = pygame.image.load('gameover.png')
hero = [pygame.image.load('hero1.png'), hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero2.png'), pygame.image.load('hero2.png'),
pygame.image.load('hero3.png'), pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'), pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')] pygame.image.load('hero5.png')]
index = 0
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
# shan_x=0
gamestate = True
index = 0
y = 400 y = 400
jumpState = "runing" jumpState = "runing"
t = 30 t = 30
...@@ -54,30 +57,28 @@ while True: ...@@ -54,30 +57,28 @@ while True:
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
jumpState = "up" jumpState = "up"
wukong = Player(hero[index])
print(wukong.image) if gamestate:
if jumpState == "up": # 起跳状态 if jumpState == "up": # 起跳状态
if t > 0: if t > 0:
y -= t y -= t
# wukong.rect.y = y
t -= 2 t -= 2
else: else:
jumpState = "down" jumpState = "down"
if jumpState == "down": # 降落状态 if jumpState == "down": # 降落状态
if t <= 30: if t <= 30:
y += t y += t
# wukong.rect.y = y
t += 2 t += 2
else: else:
jumpState = "runing" jumpState = "runing"
t =30 t =30
# 悟空造型 # 悟空造型
wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下 if jumpState == "runing": # 跑步状态下
index += 1 index += 1
if index >= 5: if index >= 5:
index = 0 index = 0
screen.blit(wukong.image, (wukong.rect.x, wukong.rect.y)) # 悟空
# 将背景图画上去 # 将背景图画上去
mountain_x -= 5 mountain_x -= 5
if mountain_x < -1000: if mountain_x < -1000:
...@@ -88,7 +89,7 @@ while True: ...@@ -88,7 +89,7 @@ while True:
if road_x <= -1000: if road_x <= -1000:
road_x = 0 road_x = 0
screen.blit(road, (road_x, 500)) # 路 screen.blit(road, (road_x, 500)) # 路
screen.blit(wukong.image, (150, y)) # 悟空
# if things.rect.x <= 0-things.rect.width: # 障碍物消失 # if things.rect.x <= 0-things.rect.width: # 障碍物消失
# # 创建障碍物对象 # # 创建障碍物对象
...@@ -104,13 +105,13 @@ while True: ...@@ -104,13 +105,13 @@ while True:
block_list.add(things) block_list.add(things)
time = 0 time = 0
for i in block_list: for i in block_list:
i.rect.x -= 8 i.rect.x -= 8
screen.blit(i.image, (i.rect.x, i.rect.y)) screen.blit(i.image, (i.rect.x, i.rect.y))
if i.rect.x < 0 - i.rect.width: if i.rect.x < 0 - i.rect.width:
i.kill() i.kill()
if pygame.sprite.collide_rect(wukong,i):
screen.blit(gameover,(400,240))
gamestate = False
# screen.blit(things.image, (things.rect.x, things.rect.y)) # screen.blit(things.image, (things.rect.x, things.rect.y))
# 刷新画面 # 刷新画面
......
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