Page 1 of 1

Pygame problem ,PLEASE HELP

Posted: Fri Mar 27, 2009 9:46 pm
by YenTex
I'm reading a pygame tutorial, and when I type this code (below), I get a black screen, when I try to quit, I see the box with the background, and then my program freezes.
I'm running a mac, with Python 2.5

Code: Select all

#Initilize
import pygame
pygame.init()

#Display
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("move a box")

#Entities
#yellow background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255, 255, 0))

#make a red 25 x 25 box
box = pygame.Surface((25, 25))
box = box.convert()
box.fill((255, 0, 0))

# set up some box variables
box_x = 0
box_y = 200

#ACTION

    #Assign
clock = pygame.time.Clock()
keepGoing = True

    #Loop
while keepGoing:

    #Time
    clock.tick(30)

    #Events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keepGoing = False

#modify box value
box_x += 5
#check boundries
if box_x > screen.get_width():
    box_x = 0
    
#Refresh screen
screen.blit(background, (0, 0))
screen.blit(box, (box_x, box_y))
pygame.display.flip()

Re: Pygame problem ,PLEASE HELP

Posted: Sun Mar 29, 2009 11:06 am
by fantastico
The lines starting with #modify box value aren't in the scope of the while loop, since they aren't indented. So what happens at the moment is that they are only executed once after the loop exits.