Commit c5bb3a60 by BellCodeEditor

save project

parent 8b3866bb
Showing with 58 additions and 43 deletions
...@@ -18,6 +18,7 @@ hero = [pygame.image.load('hero1.png'), ...@@ -18,6 +18,7 @@ hero = [pygame.image.load('hero1.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')]
gameover = pygame.image.load('gameover.png')
index = 0 index = 0
y = 400 y = 400
jumpState = "runing" jumpState = "runing"
...@@ -26,6 +27,7 @@ road_x=0 ...@@ -26,6 +27,7 @@ road_x=0
bg_x=0 bg_x=0
obstacle_list = [bush, stone, cacti] obstacle_list = [bush, stone, cacti]
cd=0 cd=0
game=True
class Block(pygame.sprite.Sprite): class Block(pygame.sprite.Sprite):
def __init__(self,list): def __init__(self,list):
...@@ -37,6 +39,14 @@ class Block(pygame.sprite.Sprite): ...@@ -37,6 +39,14 @@ class Block(pygame.sprite.Sprite):
obstacle=Block(obstacle_list) obstacle=Block(obstacle_list)
block_list=pygame.sprite.Group() block_list=pygame.sprite.Group()
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
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,50 +56,54 @@ while True: ...@@ -46,50 +56,54 @@ 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 game:
if jumpState == "up": # 起跳状态 wukong = player(hero[index])
if t > 0: if jumpState == "runing": # 跑步状态下
y -= t index += 1
t -= 2 if index >= 5:
else: index = 0
jumpState = "down" if jumpState == "up": # 起跳状态
if jumpState == "down": # 降落状态 if t > 0:
if t <= 30: y -= t
y += t wukong.rect.y=y
t += 2 t -= 2
else: else:
jumpState = "runing" jumpState = "down"
t =30 if jumpState == "down": # 降落状态
if t <= 30:
y += t
wukong.rect.y=y
t += 2
else:
jumpState = "runing"
t =30
# 悟空造型
wukong = hero[index]
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (bg_x, 0)) # 远处背景 screen.blit(background, (bg_x, 0)) # 远处背景
screen.blit(road, (road_x, 500)) # 路 screen.blit(road, (road_x, 500)) # 路
screen.blit(wukong, (150, y)) # 悟空 screen.blit(wukong.image, (150, y)) # 悟空
if cd==60: if cd==40:
r=random.randint(0,100) r=random.randint(0,100)
if r>60: if r>30:
obstacle=Block(obstacle_list) obstacle=Block(obstacle_list)
block_list.add(obstacle) block_list.add(obstacle)
cd=0 cd=0
for o in block_list: for o in block_list:
o.rect.x-=8 o.rect.x-=10
screen.blit(o.image,(o.rect.x, o.rect.y)) if pygame.sprite.collide_rect(o,wukong):
if o.rect.x+o.rect.width<0: game=False
o.kill() screen.blit(gameover,(400,150))
road_x -= 8 if o.rect.x+o.rect.width<0:
if road_x<=-1000: o.kill()
road_x=0 screen.blit(o.image,(o.rect.x, o.rect.y))
bg_x-=2 road_x -= 10
if bg_x<=-1000: if road_x<=-1000:
bg_x=0 road_x=0
# 刷新画面 bg_x-=3
cd=cd+1 if bg_x<=-1000:
print(len(block_list)) bg_x=0
# 刷新画面
cd=cd+1
#print(len(block_list))
pygame.display.update() pygame.display.update()
FPS.tick(60) FPS.tick(60)
\ 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