1

Images are not shown in the window until I close the script. I am making a multiplayer game for two. I want to display a map using images displayed in a grid, but the images simply don't show up. I have tried adding print statements to figure out if the function was executed (it was) It seems window.blit(image, (x, y)) doesn't work, but I can't figure out why.

import pygame
from time import sleep
pygame.init()
clock = pygame.time.Clock()

WIDTH = 624
HEIGHT = 360
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('snoeppot')
#plaatjes
gras = pygame.image.load('sprites/Grass.png')
struik = pygame.image.load('sprites/struik.png')
vloerRed = pygame.image.load('sprites/blauwe_vloer.png')
vloerBlue = pygame.image.load('sprites/rode_vloer.png')
SafeSpace = pygame.image.load('sprites/safe_space.png')
IngangPortaal = pygame.image.load('sprites/ingang_portaal.png')
uitgangPortaal = pygame.image.load('sprites/uitgang_portaal.png')
water = pygame.image.load('sprites/water.png')
weg = pygame.image.load('sprites/weg.png')
ijs = pygame.image.load('sprites/ijs.png')
snoeppotPlatform = pygame.image.load('sprites/snoeppot_platform.png')
#uiterlijk class each uiterlijk is one tile in the map
class uiterlijk:
    blokken = [gras, struik, 0, SafeSpace, IngangPortaal, #0-4
               uitgangPortaal, water, weg, ijs, 0, #5-9
               snoeppotPlatform]
    def __init__(self, soort, X, Y):
        self.soort = soort #defines the kind of tile 0-26
        self.pos = [X, Y]
        if 11 <= self.soort <=26: #11 trought 26 use the same image.
            self.image = pygame.transform.scale(gras, (tegel, tegel))
        else:
            self.image = pygame.transform.scale(self.blokken[soort], (tegel, tegel)) #looks in list blokken for an image
    def draw(self):
        window.blit(self.image, (tegel*self.pos[0], tegel*self.pos[1]))
#the map I want to display each number equals a image
speelveld = [
[1, 1, 0, 11, 11, 21, 11, 11, 0, 1, 0, 12, 12, 22, 22, 12, 12, 0, 1, 0, 13, 13, 23, 13, 13, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0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 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 14, 14, 24, 14, 14, 0, 1, 0, 15, 15, 25, 25, 15, 15, 0, 1, 0, 16, 16, 26, 16, 16, 0, 1, 1]]

tegel = 18 #size of the images

speelveld_tiles = [[uiterlijk(speelveld[Y][X], X, Y) for X in range(len(speelveld[0]))] for Y in range(len(speelveld))]

while True:
    #draws whole window white
    window.fill((0, 255, 255))
    #draws the map
    for rij in range(len(speelveld)):
        for x in range(len(speelveld[0])):
            speelveld_tiles[rij][x].draw()
    #updates screen
    pygame.display.update()
    clock.tick(60)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Fuzetea17
  • 19
  • 3

1 Answers1

1

You have to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    #draws whole window white
    window.fill((0, 255, 255))
    #draws the map
    for rij in range(len(speelveld)):
        for x in range(len(speelveld[0])):
            speelveld_tiles[rij][x].draw()
    #updates screen
    pygame.display.update()
    clock.tick(60)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174