Commit 63dd28f9 by BellCodeEditor

save project

parent d7fc96d1
Showing with 68 additions and 67 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): gamestate = True
def __init__(self,image): index = 0
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x=150
self.rect.y=400
# shan_x=0
y = 400 y = 400
jumpState = "runing" jumpState = "runing"
t = 30 t = 30
...@@ -54,65 +57,63 @@ while True: ...@@ -54,65 +57,63 @@ 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 jumpState == "up": # 起跳状态
if t > 0:
y -= t
# wukong.rect.y = y
t -= 2
else:
jumpState = "down"
if jumpState == "down": # 降落状态
if t <= 30:
y += t
# wukong.rect.y = y
t += 2
else:
jumpState = "runing"
t =30
# 悟空造型 if gamestate:
if jumpState == "runing": # 跑步状态下 if jumpState == "up": # 起跳状态
index += 1 if t > 0:
if index >= 5: y -= t
index = 0 t -= 2
screen.blit(wukong.image, (wukong.rect.x, wukong.rect.y)) # 悟空 else:
# 将背景图画上去 jumpState = "down"
mountain_x -= 5 if jumpState == "down": # 降落状态
if mountain_x < -1000: if t <= 30:
mountain_x = 0 y += t
screen.blit(background, (mountain_x, 0)) # 远处背景 t += 2
else:
jumpState = "runing"
t =30
# 悟空造型
wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
# 将背景图画上去
mountain_x -= 5
if mountain_x < -1000:
mountain_x = 0
screen.blit(background, (mountain_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.image, (150, y)) # 悟空
# if things.rect.x <= 0-things.rect.width: # 障碍物消失 # if things.rect.x <= 0-things.rect.width: # 障碍物消失
# # 创建障碍物对象 # # 创建障碍物对象
# things = Block(bush,cacti,stone) # things = Block(bush,cacti,stone)
# things.rect.x -= 8 # things.rect.x -= 8
time += 1
if time > 60:
num = random.randint(0,100)
if num >40:
things = Block(bush,cacti,stone)
block_list.add(things)
time = 0
for i in block_list:
i.rect.x -= 8 time += 1
screen.blit(i.image, (i.rect.x, i.rect.y)) if time > 60:
if i.rect.x < 0 - i.rect.width: num = random.randint(0,100)
i.kill() if num >40:
things = Block(bush,cacti,stone)
block_list.add(things)
time = 0
for i in block_list:
i.rect.x -= 8
screen.blit(i.image, (i.rect.x, i.rect.y))
if i.rect.x < 0 - i.rect.width:
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)) # 刷新画面
# 刷新画面 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