Commit b1df6cd9 by BellCodeEditor

save project

parent ca10315b
Showing with 67 additions and 49 deletions
...@@ -11,10 +11,21 @@ class Block(pygame.sprite.Sprite): ...@@ -11,10 +11,21 @@ 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
# 创建一个窗口 # 创建一个窗口
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("悟空酷跑")
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') # 石头
...@@ -35,6 +46,8 @@ bg_x = 0 ...@@ -35,6 +46,8 @@ bg_x = 0
time = 0 time = 0
#zhuangzhe = Block(stone,cacti,bush) #zhuangzhe = Block(stone,cacti,bush)
block_list = pygame.sprite.Group() block_list = pygame.sprite.Group()
huabu = 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:
...@@ -45,59 +58,64 @@ while True: ...@@ -45,59 +58,64 @@ while True:
if zhuangtai == "running": if zhuangtai == "running":
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
zhuangtai = "up" zhuangtai = "up"
if huabu == True:
wukong = player(hero[index])
if zhuangtai == "running":
index += 1
if index == 5:
index = 0
if zhuangtai == "up":
if nu > 0: if zhuangtai == "up":
y -= nu if nu > 0:
nu -=2 y -= nu
else: wukong.rect.y = y
zhuangtai = "down" nu -=2
else:
if zhuangtai == "down": zhuangtai = "down"
if nu <= 30:
y += nu if zhuangtai == "down":
nu += 2 if nu <= 30:
else: y += nu
zhuangtai = "running" wukong.rect.y = y
nu = 30 nu += 2
else:
zhuangtai = "running"
nu = 30
bg_x -= 1
if bg_x <= -1000:
wukong = hero[index] bg_x = 0
if zhuangtai == "running": screen.blit(background, (bg_x, 0))
index += 1
if index == 5:
index = 0
bg_x -= 1
if bg_x <= -1000:
bg_x = 0
screen.blit(background, (bg_x, 0))
road_x -= 8 road_x -= 8
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, (150, y)) screen.blit(wukong.image, (150, y))
# 刷新画面 # 刷新画面
#if zhuangzhe.rect.x <= 0-zhuangzhe.rect.width:
#zhuangzhe = Block(stone,cacti,bush) #if zhuangzhe.rect.x <= 0-zhuangzhe.rect.width:
# zhuangzhe.rect.x -= 8 #zhuangzhe = Block(stone,cacti,bush)
#zhuangzhe.rect.x -= 8
time += 1
if time >= 60:
time = 0
num = random.randint(0,100)
if num >40:
zhuangzhe = Block(stone,cacti,bush)
block_list.add(zhuangzhe)
for prop in block_list:
prop.rect.x-=8
screen.blit(prop.image,(prop.rect.x,prop.rect.y))
if prop.rect.x <= 0-prop.rect.width:
prop.kill()
time += 1
if time >= 60:
time = 0
num = random.randint(0,100)
if num >40:
zhuangzhe = Block(stone,cacti,bush)
block_list.add(zhuangzhe)
for prop in block_list:
prop.rect.x-=8
screen.blit(prop.image,(prop.rect.x,prop.rect.y))
if prop.rect.x <= 0-prop.rect.width:
prop.kill()
if pygame.sprite.collide_rect(wukong,prop):
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
huabu = False
#screen.blit(zhuangzhe.image, (zhuangzhe.rect.x, zhuangzhe.rect.y)) #screen.blit(zhuangzhe.image, (zhuangzhe.rect.x, zhuangzhe.rect.y))
pygame.display.update() pygame.display.update()
......
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