[英]Pygame: Game Over Screen not displaying when in theory, it should
I'm back at it with this game and it almost works, the gameplay is almost right but it's just the game over screen.我回到了这个游戏,它几乎可以工作,游戏玩法几乎是正确的,但它只是屏幕上的游戏。 Basically, it's supposed to blit the game-over screen when the player's lives drop to 0 and then wait 5 seconds before closing.
基本上,它应该在玩家的生命降到 0 时使游戏结束屏幕闪烁,然后在关闭前等待 5 秒。
In this MCVE example, I've made it so the player's lives decrement every 0.5 seconds but it still produces the same issue.在这个 MCVE 示例中,我已经使玩家的生命每 0.5 秒减少一次,但它仍然会产生相同的问题。
I've dry-run the program a few times and can't see what's wrong with it.我已经试运行了几次程序,但看不出它有什么问题。
Here's my code:这是我的代码:
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()
If anyone knows what amateur mistake I've made this time, that'd be great.如果有人知道我这次犯了什么业余错误,那就太好了。
Thanks :)谢谢 :)
At the end of your update
function, modify the block:在
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
Should update the screen correctly.应该正确更新屏幕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.