Commit 1890764b by BellCodeEditor

save project

parent a4233059
Showing with 74 additions and 57 deletions
...@@ -9,6 +9,14 @@ class Block(pygame.sprite.Sprite): ...@@ -9,6 +9,14 @@ 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))
...@@ -25,6 +33,7 @@ hero = [pygame.image.load('hero1.png'), ...@@ -25,6 +33,7 @@ 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')]
gameover=pygame.image.load('gameover.png')
block_list=pygame.sprite.Group() block_list=pygame.sprite.Group()
lu_x=0 lu_x=0
shan_x=0 shan_x=0
...@@ -34,67 +43,75 @@ jumpState = "runing" ...@@ -34,67 +43,75 @@ jumpState = "runing"
t = 30 t = 30
time=0 time=0
#aa=Block(bush,cacti,stone) #aa=Block(bush,cacti,stone)
aaa=True
while True: while True:
for event in pygame.event.get(): if aaa==True:
if event.type == locals.QUIT: for event in pygame.event.get():
# 接收到退出事件后退出程序 if event.type == locals.QUIT:
exit() # 接收到退出事件后退出程序
if event.type == locals.KEYDOWN: exit()
if jumpState == "runing": if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE: if jumpState == "runing":
jumpState = "up" if event.key == locals.K_SPACE:
jumpState = "up"
wukong = Player(hero[index])
if jumpState == "runing": # 跑步状态下
index += 1
if index >= 5:
index = 0
if jumpState == "up": # 起跳状态 if jumpState == "up": # 起跳状态
if t > 0: if t > 0:
y -= t y -= t
t -= 2 wukong.rect.y=y
else: t -= 2
jumpState = "down" else:
if jumpState == "down": # 降落状态 jumpState = "down"
if t <= 30: if jumpState == "down": # 降落状态
y += t if t <= 30:
t += 2 y += t
else: wukong.rect.y=y
jumpState = "runing" t += 2
t =30 else:
jumpState = "runing"
# 悟空造型 t =30
wukong = hero[index]
if jumpState == "runing": # 跑步状态下 # 悟空造型
index += 1
if index >= 5: # 将背景图画上去
index = 0 shan_x -=2
# 将背景图画上去 if shan_x <-1000:
shan_x -=2 shan_x=0
if shan_x <-1000: screen.blit(background, (shan_x, 0)) # 远处背景
shan_x=0 lu_x -=8
screen.blit(background, (shan_x, 0)) # 远处背景 if lu_x <-1000:
lu_x -=8 lu_x=0
if lu_x <-1000: screen.blit(road, (lu_x, 500)) # 路
lu_x=0 screen.blit(wukong.image, (150, y)) # 悟空
screen.blit(road, (lu_x, 500)) # 路
screen.blit(wukong, (150, y)) # 悟空
'''if aa.rect.x <= 0-aa.rect.width: # 障碍物消失 '''if aa.rect.x <= 0-aa.rect.width: # 障碍物消失
# 创建障碍物对象 # 创建障碍物对象
aa=Block(bush,cacti,stone)
aa.rect.x -= 8'''
time+=1
if time >=60:
num=random.randint(0,50)
if num >20:
aa=Block(bush,cacti,stone) aa=Block(bush,cacti,stone)
block_list.add(aa) aa.rect.x -= 8'''
time=0
for sprite in block_list:
sprite.rect.x -= 8 time+=1
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y)) if time >=60:
if sprite.rect.x<=0-sprite.rect.width: num=random.randint(0,50)
sprite.kill() if num >20:
aa=Block(bush,cacti,stone)
block_list.add(aa)
time=0
for sprite in block_list:
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):
aaa=False
screen.blit(gameover,(400,300))
#screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y)) #screen.blit(sprite.image, (sprite.rect.x, sprite.rect.y))
# 刷新画面 # 刷新画面
......
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