Commit 24182f9b by BellCodeEditor

save project

parent e1a92d85
Showing with 72 additions and 55 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,62 +53,70 @@ while True: ...@@ -44,62 +53,70 @@ while True:
if jumpstate == 'running': if jumpstate == 'running':
if event.key==locals.K_w: if event.key==locals.K_w:
jumpstate='up' jumpstate='up'
if jumpstate =='up': wukong=Player(hero[index])
if t>0: if gamestate==True:
y-=t if jumpstate =='up':
t-=2 if t>0:
else: y-=t
jumpstate='down' wukong.rect.y=y
if jumpstate == 'down': t-=2
if t <=30: else:
y+=t jumpstate='down'
t+=2 if jumpstate == 'down':
else: if t <=30:
jumpstate='running' y+=t
t=30 wukong.rect.y=y
t+=2
wukong = hero[index] else:
index+=1 jumpstate='running'
if index==5: t=30
index=0
if y < 400 and y>150: index+=1
wukong=hero[3] if index==5:
bg_x-=1 index=0
if bg_x <=-1000:
bg_x=0 bg_x-=1
if bg_x <=-1000:
bg_x=0
road_x-=8
if road_x<=-1000:
road_x=0
time+=1
if time>=60:
time=0
num=random.randint(0,100)
if num>=40:
obstacle=Block(bush,stone,cacti)
block_list.add(obstacle)
road_x-=8 screen.blit(background, (bg_x, 0))
if road_x<=-1000: for sprite in block_list:
road_x=0
sprite.rect.x-=8
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x<= 0-sprite.rect.width:
sprite.kill()
if pygame.sprite.collide_rect(wukong,sprite):
gameover=pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate=False
time+=1 # 将背景图画上去
if time>=60:
time=0
num=random.randint(0,100)
if num>=40:
obstacle=Block(bush,stone,cacti)
block_list.add(obstacle)
time=0
screen.blit(background, (bg_x, 0))
for prop in block_list:
prop.rect.x-=8 screen.blit(road, (road_x, 500))
screen.blit(wukong.image, (150, y))
screen.blit(prop.image,(prop.rect.x,prop.rect.y)) #screen.blit(.image,(prop.rect.x,prop.rect.y))
if prop.rect.x<=0-prop.rect.width: # obstacle.rect.x-=8
prop.kill()
# 将背景图画上去
screen.blit(road, (road_x, 500))
screen.blit(wukong, (150, y))
#screen.blit(.image,(prop.rect.x,prop.rect.y))
# obstacle.rect.x-=8
# if obstacle.rect.x <=0-obstacle.rect.width: # if obstacle.rect.x <=0-obstacle.rect.width:
# obstacle=Block(bush,stone,cacti) # obstacle=Block(bush,stone,cacti)
# 刷新画面 # 刷新画面
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