Glitchd
Glitchd

Reputation: 361

pygame screen only works when i move the window off the screen

    import pygame, sys
from pygame.locals import *
pygame.init()

sizex = 400
sizey = 300
tilesize = 25

tile = pygame.image.load('images/tile.png')
tile = pygame.transform.scale(tile, (tilesize, tilesize))

screen = pygame.display.set_mode((sizex,sizey))

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    for row in range(sizex):
        for column in range(sizey):
            screen.blit(tile,(column*tilesize, row*tilesize,tilesize,tilesize))

I am using this code to output:

enter image description here

however, when I run it the screen is black, if i move half of the window off of the computer screen:

picture

and move it back:

picture

that happens.

Can someone pls explain why this happens and how to fix it.

Upvotes: 2

Views: 533

Answers (1)

Torxed
Torxed

Reputation: 23480

The reason for this is most likely that nothing triggers a re-draw event. Meaning the buffer never updates. Except when the window is moved outside of the screen, that portion will trigger a re-draw event for that area.

Manually adding a update or flip at the end of your while, should force a update of the scene, making things look nice again:

import pygame, sys
from pygame.locals import *
pygame.init()

sizex = 400
sizey = 300
tilesize = 25

tile = pygame.image.load('images/tile.png')
tile = pygame.transform.scale(tile, (tilesize, tilesize))

screen = pygame.display.set_mode((sizex,sizey))

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    for row in range(sizex):
        for column in range(sizey):
            screen.blit(tile,(column*tilesize, row*tilesize,tilesize,tilesize))

    pygame.display.flip() # or pygame.display.update()

For anyone familiar with pygame/gl and screen updates, this will be some what taxing. In ideal cases, you would only update areas that are in need of updates.
For instance, keeping track of which portions of the screen you've moved a character, or which mouse events have triggered certain elements on the screen. And then only do pygame.display.update(rectangle_list) with a area to update.

Here's a good description of what the two does and why using update() might be a good idea: Difference between pygame.display.update and pygame.display.flip

Upvotes: 3

Related Questions