On Sun, Sep 19, 2010 at 5:08 AM, kevin hayes <[email protected]> wrote:

> Hi, I'm just beginning to learn python and pygame. I've gone through the
> first four chapters of "Game Programming, The L-line, the Express Line to
> Learning.  Right now I'm working on an exercise that asks me to "make a box
> that moves in a diagonal path. Can someone tell me why when I run the code
> the program only executes while I move the mouse? Also, can you tell me how
> i can change my code to avoid this? Thanks, Kevin
>
> here is my code:
>
> import pygame
> pygame.init()
>
> screen = pygame.display.set_mode((640, 480))
> pygame.display.set_caption("Diagonal Red Square")
>
> background = pygame.Surface(screen.get_size())
> background = background.convert()
> background.fill((0, 0, 0))
>
> box = pygame.Surface((20, 20))
> box = box.convert()
> box.fill((255, 0, 0))
>
> box_x = 0   #box variable for the x-axis
> box_y = 0   #" " y-axis
>
> clock = pygame.time.Clock()
> keepGoing = True
>
> while keepGoing:
>     clock.tick(30)
>     for event in pygame.event.get():
>         if event.type == pygame.QUIT:
>             keepGoing = False
>
>         box_x += 5
>         box_y += 5
>
>     if box_y > screen.get_height():  #checking for screen boundaries
>             box_y = 0
>     if box_x > screen.get_width():
>             box_x = 0
>
>
>     screen.blit(background, (0, 0))
>     screen.blit(box, (box_x, box_y))
>     pygame.display.flip()
>
> pygame.quit()
>

You change the box position only if an event is present, so you need to move
the mouse or press keys to move.

Move out of the event loop the lines that change the position, that is,
dedent one level the lines:
box_x += ...
box_y += ...

--
claudio

Reply via email to