2

I'm just learning GUI programming using python 3.2 and pygame 1.8. From what I understand the following code should display a white background. However all I get is a black screen. Also, I'm using IDLE in Windows 7 if that matters:

import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
background = pygame.Surface(screen.get_size())
background.fill((255, 255, 255))
screen.blit(background, (0, 0))

3 Answers 3

4

You must have a main loop that updates the screen. This code should work:

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))

background = pygame.Surface(screen.get_size())
background.fill((255, 255, 255))

while True:
    screen.blit(background, (0, 0))
    pygame.display.update()
2
  • 1
    i get this error now: in line 11 screen.update() AttributeError: 'pygame.Surface' object has no attribute 'update' Nov 17, 2011 at 20:31
  • @Ahmed - I am assuming you mean in the comment, there is a help link just underneath the "Add Comment" button which explains comment formatting. For code formatting put the code inside of `backticks`. Nov 17, 2011 at 20:43
2

You forgot to update the screen.

import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
background = pygame.Surface(screen.get_size())
background.fill((255, 255, 255))
screen.blit(background, (0, 0))

pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
0
1

You don't blit the backgound you just put the fill part in the loop. Also you are forgetting the pygame.display.update()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.