Reputation: 377
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.
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.
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 :)
Upvotes: 1
Views: 122
Reputation: 14179
At the end of your update
function, modify the block:
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.
Upvotes: 1