Reputation: 321
I am developing a program that will allow user to create and edit a GUI made in Pygame. The program itself will have a nice GUI and for that I am making a module to simplify its creation. While everything else for now works as I expect, I am hitting on that one problem that I simply can't solve, neither understand:
When I start the program a black window shows up. The problem is that the window should be white and containing a black rectangle with an image in it. I tried clicking in it and moving it around, but it didn't show anything.
First I thought that I didn't update the window, but I checked the script and found out that I did. Then I started trying different things with the window and found out that only the part of the window that exits my actual screen (monitor) gets updated and it gets updated instantly the moment I drag it out and back into screen.
Can someone please explain me what is happening, why is it happening and how I can fix it?
EDIT
I found out that the window also updates correctly if I minimize it and then maximize it. So the problem is not so critical but it is very annoying.
Here is the code:
import pygame
import time
def initialize():
global display
global screen
pygame.init()
display = pygame.display
screen = None
class Screen():
def __init__(self, size, color=[255, 255, 255]):
global screen
self.size = size
self.color = color
self.screen = display.set_mode(self.size)
self.fill(self.color)
self.update()
screen = self
def update(self, rectangle=None):
display.update(rectangle)
def fill(self, color, rectangle=None):
self.screen.fill(color, rectangle)
def draw(self, sprite):
self.screen.blit(sprite.image, sprite.rectangle[0])
class Engine():
def __init__(self):
self.events = []
self.running = False
def update_events(self):
self.events += pygame.event.get()
def get_events(self):
events = self.events
self.events = []
return events
def start(self, function=None, args=[], kwargs={}, frequency=21, function_first=False):
self.running = True
while self.running:
if function_first and function:
function(*args, **kwargs)
self.update_events()
if not function:
for event in self.events:
if event.type == pygame.QUIT:
self.running = False
pygame.quit()
break
if not function_first and function:
function(*args, **kwargs)
if self.running:
tick = 1.0 / frequency
time.sleep(tick)
def stop(self):
self.running = False
class Sprite():
def __init__(self, rectangle, color=[0, 0, 0]):
self.rectangle = rectangle
self.color = color
self.image = None
def update(self):
global screen
screen.fill(self.color, self.rectangle)
screen.draw(self)
def load_image(self, image):
self.image = pygame.image.load(image).convert()
if __name__ == "__main__":
initialize()
s = Screen([500, 500])
e = Engine()
sprite = Sprite([[10, 10], [200, 200]])
sprite.load_image("test.png")
sprite.update()
s.update()
e.start()
Here are the screenshots:
When I pull the window out of my screen:
Upvotes: 0
Views: 1322
Reputation: 1457
You have some errors in your code.
You don't update the screen in your main loop.
You should always call pygame.event.get in every iteration of your main loop.
Don't use time.sleep, use pygame.time.clock.tick
Upvotes: 1