Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎯 Unreal

Python problem

Started by Lunatic BoM Jun 2, 2010 at 9:12 AM 2 replies 800+ views
Original Post
Lunatic BoM
Lunatic BoM
Hi, so I started programming python recently, including pygame library. And now I have this bit of code:
import pygamepygame.init()screen = pygame.display.set_mode((640, 480))pygame.display.set_caption("move a box")background = pygame.Surface(screen.get_size())background = background.convert()background.fill((255, 255, 0))box = pygame.Surface((25, 25))box = box.convert()box.fill((255, 0, 0))box_x = 0box_y = 200clock = pygame.time.Clock()keepGoing = Truewhile keepGoing:    clock.tick(30)    for event in pygame.event.get():        if event.type == pygame.QUIT:            keepGoing = False            box_x += 5            if box_x > screen.get_width(480):                box_x = 0                screen.blit(background, (0, 0))                screen.blit(box, (box_x, box_y))                pygame.display.flip()

and for some unpredicted reason, screen freezes as I try to run this code.
Maybe you know what is the problem?
Thanks
Black Knight
Black Knight
You are trying to draw inside the loop that is handling events and also inside the case when the event is quit so it will only run that code when the quit event is sent.

while keepGoing:    clock.tick(30)    for event in pygame.event.get():        if event.type == pygame.QUIT:            keepGoing = False    box_x += 5    if box_x > screen.get_width(480):        box_x = 0    screen.blit(background, (0, 0))    screen.blit(box, (box_x, box_y))    pygame.display.flip()
Lunatic BoM
Lunatic BoM
Ermm, could you explain that a little better. Thanks in advance :)
stonemetal
stonemetal
 for event in pygame.event.get():        if event.type == pygame.QUIT:            keepGoing = False            box_x += 5            if box_x > screen.get_width(480):                box_x = 0                screen.blit(background, (0, 0))                screen.blit(box, (box_x, box_y))                pygame.display.flip()


What do you believe this code does?
In python indentation is important. Conversely new lines are not important. (This code would probably do what you expected if it was the other way around.)
Everything below the if event.type == pygame.QUIT is indented more than the if statement therefore they will only be executed if that if statement is true. Therefore nothing is ever drawn until you get a quit event. There are some other random weird things like why are you passing 480 to get_width (according to the documentation it doesn't take a parameter)?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.