Commit 24182f9b by BellCodeEditor

save project

parent e1a92d85
Showing with 27 additions and 10 deletions
...@@ -6,6 +6,13 @@ pygame.init() # 初始化 ...@@ -6,6 +6,13 @@ 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('悟空酷跑') pygame.display.set_caption('悟空酷跑')
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
class Block(pygame.sprite.Sprite): class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3): def __init__(self,image1,image2,image3):
super().__init__() super().__init__()
...@@ -15,6 +22,7 @@ class Block(pygame.sprite.Sprite): ...@@ -15,6 +22,7 @@ class Block(pygame.sprite.Sprite):
self.rect.y=500-self.rect.height self.rect.y=500-self.rect.height
# 载入图片 # 载入图片
# gameover = pygame.image.load('gameover.png')
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') # 石头
...@@ -32,6 +40,7 @@ t=30 ...@@ -32,6 +40,7 @@ t=30
road_x=8 road_x=8
bg_x=0 bg_x=0
time=0 time=0
gamestate=True
# obstacle=Block(stone,cacti,bush) # obstacle=Block(stone,cacti,bush)
block_list=pygame.sprite.Group() block_list=pygame.sprite.Group()
...@@ -44,26 +53,28 @@ while True: ...@@ -44,26 +53,28 @@ while True:
if jumpstate == 'running': if jumpstate == 'running':
if event.key==locals.K_w: if event.key==locals.K_w:
jumpstate='up' jumpstate='up'
wukong=Player(hero[index])
if gamestate==True:
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='running' jumpstate='running'
t=30 t=30
wukong = hero[index]
index+=1 index+=1
if index==5: if index==5:
index=0 index=0
if y < 400 and y>150:
wukong=hero[3]
bg_x-=1 bg_x-=1
if bg_x <=-1000: if bg_x <=-1000:
bg_x=0 bg_x=0
...@@ -79,21 +90,27 @@ while True: ...@@ -79,21 +90,27 @@ while True:
if num>=40: if num>=40:
obstacle=Block(bush,stone,cacti) obstacle=Block(bush,stone,cacti)
block_list.add(obstacle) block_list.add(obstacle)
time=0
screen.blit(background, (bg_x, 0)) screen.blit(background, (bg_x, 0))
for prop in block_list: for sprite in block_list:
sprite.rect.x-=8
prop.rect.x-=8 screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x<= 0-sprite.rect.width:
sprite.kill()
screen.blit(prop.image,(prop.rect.x,prop.rect.y)) if pygame.sprite.collide_rect(wukong,sprite):
if prop.rect.x<=0-prop.rect.width: gameover=pygame.image.load('gameover.png')
prop.kill() screen.blit(gameover,(400,200))
gamestate=False
# 将背景图画上去 # 将背景图画上去
screen.blit(road, (road_x, 500)) screen.blit(road, (road_x, 500))
screen.blit(wukong, (150, y)) screen.blit(wukong.image, (150, y))
#screen.blit(.image,(prop.rect.x,prop.rect.y)) #screen.blit(.image,(prop.rect.x,prop.rect.y))
# obstacle.rect.x-=8 # obstacle.rect.x-=8
......
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