Commit 4168e34c by BellCodeEditor

save project

parent 7cf60527
Showing with 25 additions and 18 deletions
...@@ -17,7 +17,9 @@ hero = [pygame.image.load('hero1.png'), ...@@ -17,7 +17,9 @@ 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')]
number = 0
y = 400
t = 30
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__()
...@@ -33,15 +35,14 @@ class Player(pygame.sprite.Sprite): ...@@ -33,15 +35,14 @@ class Player(pygame.sprite.Sprite):
self.rect=self.image.get_rect() self.rect=self.image.get_rect()
self.rect.x=150 self.rect.x=150
self.rect.y=400 self.rect.y=400
group=pygame.sprite.Group() group = pygame.sprite.Group()
index = 0 index = 0
y = 400 y = 400
jumpState = "runing" jumpState = "runing"
t = 30 t = 30
rect = obstacle.get_rect() time = 0
rect.x = 1000
rect.y = 500 - rect.height
changex = 0 changex = 0
gamestate = False
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:
...@@ -51,45 +52,51 @@ while True: ...@@ -51,45 +52,51 @@ 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 changex<=-1000: if changex<=-1000:# 路的移动
changex = 0 changex = 0
else: else:
changex-=8 changex-=8
if gamestate==False:
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 = "runing" jumpState = "runing"
t =30 t =30
# 悟空造型 # 悟空造型
wukong = hero[index] wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下 if jumpState == "runing": # 跑步状态下
index += 1 index += 1
if index >= 5: if index >= 5:
index = 0 index = 0
# 将背景图画上去 # 将背景图画上去
time+=1
if time>=60:
time=0
number=random.randint(0,50)
if number>20:
obstacle=Block(stone,cacti,bush)
group.add(obstacle)
screen.blit(background, (0, 0)) # 远处背景 screen.blit(background, (0, 0)) # 远处背景
screen.blit(road, (changex, 500)) # 路 screen.blit(road, (changex, 500)) # 路
screen.blit(wukong.image, (150, y)) # 悟空 screen.blit(wukong.image, (150, y)) # 悟空
if rect.x <= 0-rect.width: # 障碍物消失
# 创建障碍物对象
obstacle = random.choice([bush,stone,cacti])
rect = obstacle.get_rect()
rect.x = 1000
rect.y = 500 - rect.height
rect.x -= 8
for sprite in group: for sprite in group:
screen.blit(obstacle, (rect.x, rect.y)) screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
pygame.sprite.collide_rect() sprite.rect.x-=8
if sprite.rect.x <= 0-sprite.rect.width: # 障碍物消失
sprite.kill()
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(30) FPS.tick(30)
# pygame.sprite.collide_rect()
\ 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