Pygame:从理论上讲,不显示屏幕游戏

时间:2019-01-24 20:37:39

标签: python time pygame

我再次使用这款游戏,它几乎可以正常工作,游戏玩法几乎是正确的,但这只是屏幕上的游戏。 基本上,应该在玩家的生命值降为0时关闭游戏结束画面,然后等待5秒钟再关闭。

在此MCVE示例中,我做到了,因此播放器的寿命每0.5秒减少一次,但仍然会产生相同的问题。

我已经对该程序进行了几次试运行,但看不到它出了什么问题。

这是我的代码:

import pygame
import time
import itertools
import os

pygame.init()
SCREENWIDTH = 1000
SCREENHEIGHT = 650
screen = pygame.display.set_mode([SCREENWIDTH, SCREENHEIGHT])
pygame.display.set_caption("Eleeza Crafter: The Cloud Colours", "EleezaCrafter")
screen.fill((255, 123, 67))
pygame.draw.rect(screen, (0, 255, 188), (0, 50, 1000, 650), 0)
myfont = pygame.font.SysFont('Ink Free', 30)
gameoverscreen = myfont.render('Game Over!', False, (0, 0, 0))

background = screen.copy()
clock = pygame.time.Clock()
stageon = True

def gameover():
    screen.blit(pygame.image.load("gameover.png"), (0,0))
    time.sleep(5)
class Player(pygame.sprite.Sprite):
    sprite = pygame.image.load("Sprites/lee.png")

    def __init__(self, *groups):
        super().__init__(*groups)
        self.image = Player.sprite
        self.rect = self.image.get_rect(topleft=(445, 550))
        self.pos = pygame.Vector2(self.rect.topleft)
        self.lives = 10
        self.score = 0
        self.hitbox = self.rect.inflate(-3, -3)
    def update(self):
        key = pygame.key.get_pressed()
        dist = 2
        if key[pygame.K_DOWN]:
            self.rect.y += dist
            self.hitbox.y += dist
        elif key[pygame.K_UP]:
            self.rect.y -= dist
            self.hitbox.y -= dist
        if key[pygame.K_RIGHT]:
            self.rect.x += dist
            self.hitbox.x += dist
        elif key[pygame.K_LEFT]:
            self.rect.x -= dist
            self.hitbox.x -= dist

        if self.rect.right > SCREENWIDTH:
            self.rect.right = SCREENWIDTH
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.bottom > SCREENHEIGHT:
            self.rect.bottom = SCREENHEIGHT
        if self.rect.top < 50:
            self.rect.top = 50

        self.lives -= 1
        time.sleep(0.5)

        if self.lives <= 0:
            screen.blit(pygame.image.load("gameover.png"), (0,0))
            time.sleep(5)
            os._exit(0)
            pygame.quit()
            stageon = False



sprites = pygame.sprite.Group()
player = Player(sprites)

lives = myfont.render('Lives: {0}'.format(player.lives), False, (255, 255, 255))

def main():
    while stageon:
        for events in pygame.event.get():
            if events.type == pygame.QUIT or stageon == False:
                screen.blit(pygame.image.load("gameover.png"), (0,0))
                time.sleep(5)
                pygame.quit()
                return


        sprites.update()
        lives = myfont.render('Lives: {0}'.format(player.lives), False, (255, 255, 255))
        screen.blit(background, (0, 0))
        screen.blit(lives, (850, 0))
        sprites.draw(screen)
        pygame.display.update()

        clock.tick(100)
        if stageon == False:
            screen.blit(pygame.image.load("gameover.png"), (0, 0))
            time.sleep(5)
            return
if __name__ == '__main__':
    main()

如果有人知道我这次 犯了什么业余错误,那就太好了。

谢谢:)

1 个答案:

答案 0 :(得分:1)

update函数的末尾,修改块:

if self.lives <= 0:
    screen.blit(pygame.image.load("gameover.png"), (0,0))
    pygame.display.flip() # Add this.
    time.sleep(5)
    os._exit(0)
    pygame.quit()
    stageon = False

应正确更新屏幕。