当我想要pygam时游戏结束屏幕不出现

2024-03-29 14:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,找到一个方法,在正确的时间点游戏结束屏幕。我在pygame上制作了一个平台,我希望在我的角色死亡动画发生后,当敌人有他的攻击动画时,游戏会出现在屏幕上。现在它绕过任何动画,只要精灵之间的碰撞检测是真的,它就说游戏结束了。有什么想法吗?在

谢谢

def GameOver():

    intro = True

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

        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)

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


class Enemy:
    def __init__(self,x,y):

        self.x=x
        self.y=y
        self.width=40
        self.height = 40
        self.speed=1
        self.s0 = pygame.image.load("Images/Enemy/s0.png")
        self.s1 = pygame.image.load("Images/Enemy/s1.png")
        self.s2 = pygame.image.load("Images/Enemy/s2.png")
        self.s3 = pygame.image.load("Images/Enemy/s3.png")
        self.attack = pygame.image.load("attack.png")
        self.RotatedAttack = pygame.transform.flip(self.attack ,True,False)
        self.rotateds0 = pygame.transform.flip(self.s0 ,True, False)
        self.rotateds1 = pygame.transform.flip(self.s1 ,True, False)
        self.rotateds2 = pygame.transform.flip(self.s2 ,True, False)
        self.rotateds3 = pygame.transform.flip(self.s3 ,True, False)

        self.collision = False
        self.collision1 = False

        self.TimeTarget=10

        self.TimeNum=0

        self.currentImage=0

    def move(self,player):

        if self.x > player.x:
            self.x -= self.speed
            if self.currentImage > 3:
                self.currentImage = 0
            if self.collision == True:

                if self.currentImage < 4:
                    #image of him attacking
                    self.currentImage = 8
                    SpearAttack.play(loops = 0, maxtime = 10)                

        elif self.x < player.x:
            self.x += self.speed
            if self.currentImage < 4:    
                self.currentImage = 4
            if self.collision == True:
                if player.y > 487:

                    if self.currentImage == 4 or 5 or 6 or 7:
                        #image of him attacking flipped
                        self.currentImage = 9
                        SpearAttack.play(loops = 0, maxtime = 10)
                        #Tried putting gameover here but it skips the animation

        if self.x < player.x:
            if self.x > player.x:
                if self.currentImage > 3:
                    self.currentImage = 0       

    def update(self,CollisionDetect,player,enemy2):

        self.TimeNum+=1
        if self.TimeNum == self.TimeTarget:

            if self.currentImage ==0:
                self.currentImage=1

            elif self.currentImage ==1:
                self.currentImage=2

            elif self.currentImage == 2:
                self.currentImage=3

            elif self.currentImage ==3:
                self.currentImage =0

            elif self.currentImage ==4:
                self.currentImage=5

            elif self.currentImage ==5:
                self.currentImage=6

            elif self.currentImage == 6:
                self.currentImage=7

            elif self.currentImage ==7:
                self.currentImage = 4

            self.TimeNum=0

        if self.currentImage==0:

            screen.blit(self.s0, (self.x,self.y))

        elif self.currentImage==1:
            screen.blit(self.s1, (self.x,self.y))

        elif self.currentImage==2:
            screen.blit(self.s2, (self.x,self.y))

        elif self.currentImage ==3:
            screen.blit(self.s3, (self.x,self.y))

        elif self.currentImage==4:
            screen.blit(self.rotateds0, (self.x,self.y))

        elif self.currentImage==5:
            screen.blit(self.rotateds1, (self.x,self.y))

        elif self.currentImage==6:
            screen.blit(self.rotateds2, (self.x,self.y))

        elif self.currentImage ==7:
            screen.blit(self.rotateds3, (self.x,self.y))

        elif self.currentImage ==8:
            screen.blit(self.attack, (self.x,self.y))
            #tried putting GameOver() here but it skips the blit.
        elif self.currentImage ==9:
            screen.blit(self.RotatedAttack, (self.x,self.y))

        self.collision = CollisionDetect(self.x,self.y,self.width,self.height,player.x,player.y,player.width,player.height)

Tags: imageselffalsetrueifpngtransformload
3条回答

通常,当您定义调用它或函数的内容时,应该导入time

import time

然后在动画完成后:

^{pr2}$

我发现了问题,这不是延迟之类的问题,而是代码的问题。在

其中包含以下代码:

if self.currentImage == 4 or 5 or 6 or 7:
    #image of him attacking flipped
    self.currentImage = 9
    SpearAttack.play(loops = 0, maxtime = 10)
    #Tried putting gameover here but it skips the animation

把游戏放在那边。以下是出现问题的代码行:

^{pr2}$

Python不是这样工作的。说那句话总有一天会变成真的。你认为这与:

^{3}$

不是的。你说的是:

if (self.currentImage == 4) or (5) or (6) or (7):

除0以外的任何整数始终返回true。在

请尝试以下代码行,这应该可以工作:

if self.currentImage in [4,5,6,7]:

玩游戏玩得开心!(如果这是一个词)

我得说你必须推迟动画制作。pygame似乎没有默认的动画队列,因此您必须处理它。有几件事可以真正起作用:

  • 使用time.sleep()-但是,如果动画是单个线程的一部分,这将不起作用,因为整个线程将停止(并且动画应该冻结)。因此,我推荐第二种选择,因为它对我来说更合理。

  • 使用pygame计时器pygame.time.set_timer(),它应该是一个线程,因此不会冻结矛动画。你必须发送一个gameover事件,然后在gameover函数中被捕捉到,并在捕捉到该事件后将该事件的计时器重置为0(以停止循环)。https://www.pygame.org/docs/ref/time.html#comment_pygame_time_set_timer

  • 使用线程(请参见:Python threading.timer - repeat function every 'n' seconds)并在矛动画之后使用计时器调用该线程

以下内容与错误本身无关,但您的代码总体设计不佳,因此这可能会有所帮助: 您试图从类的实例(在本例中为敌人)调用游戏中的函数(在本例中为敌人),这是一种错误的做法,并且在语义上是不正确的。敌人阶级根本不应该知道游戏,但是它应该被游戏所使用,而不是相反。它可以触发某些事件,但是当游戏结束时(以及类似的游戏相关事件),应该由一个不同的全局处理程序来处理,而不是一个类的单个实例。在

另外,不要有无休止的if-else条件,您应该考虑使用某种映射,例如字典。它可以保护您的许多行代码,它更易于维护,也更易于阅读。在

相关问题 更多 >