在pygame中创建一个有效的Game Over功能

时间:2016-01-19 00:32:08

标签: python pygame

我做了一个平台游戏,当我死的时候,我希望有一个场景游戏。我已经成功地能够在我的玩家精灵显示其死动画之后,在屏幕上显示游戏结束场景。然而,当gameover屏幕将我带回主菜单,并且我点击播放时,它显然会让我回到我的精灵死了。基本上在我的精灵被杀之后我想让动画恢复正常(走路)而不是继续死。死亡动画在与敌人或陷阱发生碰撞时发生。这似乎是一项非常复杂的任务,我只是想对某人提出意见。有帮助吗?顺便说一句,我的代码的前三分之一是玩家类,其中底部三分之二是屏幕上的游戏等...

class Player(pygame.sprite.Sprite):
def __init__(self,x,y,width = 65, height = 35):
    pygame.sprite.Sprite.__init__(self)
    self.x = x
    self.y = y
    self.hspeed,self.vspeed = 0,0
    self.speed = 2

    self.images=[]
    r0 = pygame.image.load("Images\Player\i1.png")
    r1 = pygame.image.load("Images\Player\i2.png")
    r2 = pygame.image.load("Images\Player\i3.png")
    r3 = pygame.image.load("Images\Player\i4.png")
    self.hurt = pygame.image.load("Images\Player\Hurt.png")
    self.images.append(r0)
    self.images.append(r1)
    self.images.append(r2)
    self.images.append(r3)

    self.rotatedimages = []
    rr0 = pygame.transform.flip(r0 ,True, False)
    rr1 = pygame.transform.flip(r1 ,True, False)
    rr2 = pygame.transform.flip(r2 ,True, False)
    rr3 = pygame.transform.flip(r3 ,True, False)
    self.rotatedimages.append(rr0)
    self.rotatedimages.append(rr1)
    self.rotatedimages.append(rr2)
    self.rotatedimages.append(rr3)

    self.gravity = 0.35
    self.index = 0
    self.image = self.images[self.index]
    self.rect = pygame.Rect(self.x,self.y,width,height)

    self.TimeNum=0
    self.TimeTarget=10
    self.Timer = 0
    self.collision = False

def update(self, event = None):
    self.calcgravity()

    self.rect.x += self.hspeed
    self.rect.y += self.vspeed

    key = pygame.key.get_pressed()
    if key[pygame.K_RIGHT]:
        self.TimeNum+=1
        if self.TimeNum == self.TimeTarget:
            self.index +=1
            if self.index >= len(self.images):
                self.index = 0
            self.image = self.images[self.index]

            self.TimeNum = 0
    print(self.Timer)
    if key[pygame.K_LEFT]:
        self.TimeNum+=1
        if self.TimeNum == self.TimeTarget:
            self.index +=1
            if self.index >= len(self.rotatedimages):
                self.index = 0
            self.image = self.rotatedimages[self.index]
            self.TimeNum = 0

def calcgravity(self):
    if self.vspeed == 0:
        self.vspeed = 1
    else:
        self.vspeed += self.gravity

#This is what i dont understand
def dead(self):
    PlayerDead.play(loops = 0, maxtime = 100)
    self.Timer += 1
    if self.Timer >= 10:
        self.images.append(self.hurt)
        self.image = self.hurt
        self.hspeed = 0
        if self.Timer == 20:
            GameOver() 

def move(self, hspeed, vspeed):
    self.hspeed += hspeed
    self.vspeed += vspeed

def Level1PlatColl(self, BlockListGrass, TrapList, enemygroup, PowerUps):

    PlatformCollision = pygame.sprite.spritecollide(self, BlockListGrass, False )
    for each_object in PlatformCollision:
        if self.vspeed > 0:
            self.rect.bottom = each_object.rect.top
            self.vspeed = 0
        if self.vspeed < 0:
            self.rect.top = each_object.rect.bottom
            self.vspeed = 0

    TrapCollision = pygame.sprite.spritecollide(self,TrapList,False )
    for each_trap in TrapCollision:
        self.dead()
        #there might be something i could do here but i dont know what

    EnemyCollision = pygame.sprite.spritecollide(self,enemygroup,False )
    for each_object in EnemyCollision:
        self.dead()

    PowerUpsCollision = pygame.sprite.spritecollide(self,PowerUps,False )
    for speedboost in PowerUpsCollision:
        PowerUps.remove(speedboost)
def text_objects(text, font):
textSurface = font.render(text, True, (0,0,0))
return textSurface, textSurface.get_rect()


def GameOver():
##    PlayerDead.stop()
##    SpearAttack.stop()
##    SadTrombone.play()

screen.blit(pygame.image.load("background0.jpg").convert(), (0,0))
largeText = pygame.font.SysFont("elephant",60)
TextSurf, TextRect = text_objects("GameOver", largeText)
TextRect.center = ((screen_width/2),(screen_height/2))
screen.blit(TextSurf, TextRect)

intro = True

while intro:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    Buttons("BACK TO MAIN MENU",100,500,400,50,TURQUOISE,DARK_TURQUOISE,"back")

    pygame.display.update()
    clock.tick(15)

def Buttons(msg,x,y,w,h,inactive,active,action=None):

mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()

if x+w > mouse[0] > x and y+h > mouse[1] > y:
    pygame.draw.rect(screen, active, (x,y,w,h))
    if click[0]==1 and action != None:
        if action == "play":
            #music.stop()
            main()
        if action == "instructions":
            Instructions()
        if action == "next_level":
            #music.stop()
            main2()
        if action == "nextlevel":
            main3()
        if action == "back":
            #music.stop()
            #this is the button where i can get back to main menu
            GameIntro()          

else:
    pygame.draw.rect(screen, inactive , (x,y,w,h))

ButtonFont = pygame.font.SysFont("elephant",30)
TextSurf, TextRect = text_objects(msg, ButtonFont)
TextRect.center = ((x+w/2),(y+h/2))
screen.blit(TextSurf,TextRect)  

2 个答案:

答案 0 :(得分:0)

将所有游戏开始变量包装到一个函数中,这样您就可以在需要再次启动游戏时调用它。基本上你用游戏功能做了什么。除了重置变量的开始游戏功能。

答案 1 :(得分:0)

定义一个从一开始就初始化游戏的功能。 所以,在第一次,你通过调用开始游戏,

new_game()

之后,当游戏结束时,你可以使用条件来开始新游戏,或者创建一个新的 game_over()函数来处理 new_game无法处理的事情功能。

if game_ending_condition:
    new_game()

通过这种方式,您可以避免在 game_over 功能中将所有变量重新分配给起始值。