简体   繁体   中英

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:

在此处输入图片说明

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

图片

and move it back:

图片

that happens.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM