Commit 909760c6 by BellCodeEditor

save project

parent 17b8b8b5
Showing with 59 additions and 45 deletions
...@@ -11,6 +11,13 @@ class Block(pygame.sprite.Sprite): ...@@ -11,6 +11,13 @@ class Block(pygame.sprite.Sprite):
shilf.rect=shilf.image.get_rect() shilf.rect=shilf.image.get_rect()
shilf.rect.x=1000 shilf.rect.x=1000
shilf.rect.y=500-shilf.rect.height shilf.rect.y=500-shilf.rect.height
class Play(pygame.sprite.Sprite):
def __init__(shilf,image):
super().__init__()
shilf.image=image
shilf.rect=shilf.image.get_rect()
shilf.rect.x=150
shilf.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时钟,控制游戏速度(帧数)
...@@ -30,13 +37,12 @@ index = 0 ...@@ -30,13 +37,12 @@ index = 0
y = 400 y = 400
jumpState = "runing" jumpState = "runing"
t = 30 t = 30
obstacle = random.choice([bush, stone, cacti]) blocklist=pygame.sprite.Group()
rect = obstacle.get_rect() yxzt=True
rect.x = 1000
rect.y = 500 - rect.height
dl = 0 dl = 0
bj = 0 bj = 0
time=0
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:
...@@ -46,46 +52,53 @@ while True: ...@@ -46,46 +52,53 @@ 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 yxzt==True:
if jumpState == "up": # 起跳状态 if jumpState == "up": # 起跳状态
if t > 0: if t > 0:
y -= t y -= t
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
t += 2 t += 2
else: else:
jumpState = "runing" jumpState = "runing"
t =30 t =30
# 悟空造型 # 悟空造型
wukong = hero[index] wukong = Play(hero[index])
if jumpState == "runing": # 跑步状态下 if jumpState == "runing": # 跑步状态下
index += 1 index += 1
if index >= 5: if index >= 5:
index = 0 index = 0
# 将背景图画上去 # 将背景图画上去
bj-=4 bj-=4
if bj<-1000: if bj<-1000:
bj=0 bj=0
screen.blit(background, (bj, 0)) # 远处背景 screen.blit(background, (bj, 0)) # 远处背景
dl-=9 dl-=9
if dl<-1000: if dl<-1000:
dl=0 dl=0
screen.blit(road, (dl, 500)) # 路 screen.blit(road, (dl, 500)) # 路
screen.blit(wukong, (150, y)) # 悟空 screen.blit(wukong.image, (150, y)) # 悟空
time+=1
if rect.x <= 0-rect.width: # 障碍物消失 if time>=60:
# 创建障碍物对象 time=0
obstacle = random.choice([bush,stone,cacti]) num=random.randint(0,50)
rect = obstacle.get_rect() if num>30:
rect.x = 1000 obstacle=Block(bush,stone,cacti)
rect.y = 500 - rect.height blocklist.add(obstacle)
rect.x -= 9 for i in blocklist:
screen.blit(obstacle, (rect.x, rect.y)) 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):
game = pygame.image.load('gameover.png')
screen.blit(game, (400,200))
yxzt=False
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(60) FPS.tick(60)
\ 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