Search code examples
pythonimagepygamewindow

Pygame problems - I try to close the window, the image blits, then unblits...what?


Well this is a weird situation.

So I'm trying to make a game in python, and it requires me to blit a picture of a character (name's Minty, just clarifying for the code), and I did it the regular way anyone would. However, this happens:

I run the code. The Pygame window opens. So far so good. There is no image however. There is no traceback. I try to X out and kill the program. Then the image suddenly appears. Wait, what?

Why is this happening? Has this happened to anyone? Here's my code (I cut out all unnecessary bits.)

import sys
import time
import pygame
from pygame.locals import *

pygame.init()
#MAIN GAME LOOP
while True:

    for events in pygame.event.get():
        if events.type == QUIT:
            quit()
            exit()

    #INITIALISING THE WINDOW. kept this in case anyone decides to try this out
    #CONSTANTS ARE CAPITAL, VARIABLES ARE LOWERCASE
    SCREENWIDTH = 900
    SCREENHEIGHT = 600
    SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
    screen = pygame.display.set_mode(SCREENSIZE)
    BG_COL = [255, 123, 67]
    clock = pygame.time.Clock()
    screen.fill(BG_COL)
    pygame.display.update()
    clock.tick(60)

    #INITIALISE ALL OBJECTS.

    class Opponent(pygame.sprite.Sprite):
        def __init__(self, name, title, sprite):
            self.name = name
            self.title = title
            self.sprite = sprite

    #INITIALISING CHARACTERS.

    Minty = Opponent("Minty Portal", "Fairy of Many Worlds", pygame.image.load("Portraits/minty-normal.png"))

    #BLIT CHARACTER
    screen.blit(Minty.sprite, [300, 90])
    pygame.display.update()

I don't know if I'm posting this right, any edits I should make, please comment below.
Thanks for the help :)


Solution

  • I think the problem is that you are redefining the screen variable every gameloop in the main function. I would recommend to initialize the window outside of the main gameloop. Try initializing everything that you only want initialized once, outside of the main gameloop.