#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0

    Post White freezing pygame window


    I am trying to make a short animation sequence in pygame (using python 3.3). For some reason, the animation seems to only like doing a certain number of loops before pygame decides it is not going to show anything but a white screen until the end of the animation. The code seems alright because it will work perfectly fine if I shorten the animation length, but when I play it the length I want it to be, the screen turns white after a certain point. The program doesnt actually crash. It keeps running, just refusing to show anything but the white screen.

    python Code:
     
    battle_map = pygame.image.load("map 1 battle screen.bmp")
    item = pygame.image.load("item box.bmp")
    item.set_colorkey(clear)
    step_done = False
    x_move = -100
    y_move = 400
     
    #during this first loop, a car putters down along a street, no problem
    for i in range (47):
        screen.blit(battle_map,(0,0))
        screen.blit(image_data,(x_move,y_move),(right,player[used].character,100,100))
        x_move += 10
        if i//5 == i/5:
            y_move = 395
        else:
            y_move = 400
        pygame.display.flip()
        clock.tick(20)
     
    # during this loop, the car waits. here is usually where the screen goes white, though if I lower the range to 20, there are no issues
    for i in range(50):
        screen.blit(battle_map,(0,0))
        screen.blit(image_data,(x_move,y_move),(right,player[used].character,100,100))
        pygame.display.flip()
        clock.tick(20)
     
    # by this point, the screen is white and I can't see this, but the car is supposed to drive off the screen
    for i in range (47):
        screen.blit(battle_map,(0,0))
        screen.blit(image_data,(x_move,y_move),(right,player[used].character,100,100))
        x_move += 10
        if i//5 == i/5:
            y_move = 395
        else:
            y_move = 400
        pygame.display.flip()
        clock.tick(20)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    This is caused by you cutting the program off from the event queue. This is not how you should be trying to animate. You are actually lucky your program isn't locking up completely.

    These lines:
    python Code:
    pygame.display.flip()
    clock.tick(20)
    should really only exist one place in your whole program. And despite what you are doing your event loop should run every frame.

    If you post a link to your image I'll try to show you.

    -Mek
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0
    I havent got a link to the images I am using. They are just on my comp. I could e-mail them I guess if you like.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Don't have more time right now but try this:
    python Code:
    import os
    import sys
    import pygame as pg
     
     
    class Car(object):
        def __init__(self,location):
            self.image = pg.Surface((50,50)).convert()
            self.image.fill((255,0,0))
            self.rect = self.image.get_rect(topleft=location)
            self.animate_fps = 20.0
            self.timer = 0.0
            self.state = "DRIVE"
            self.frame_max = {"DRIVE":60, "WAIT":30, "LEAVE":80}
            self.next_state = {"DRIVE":"WAIT", "WAIT":"LEAVE", "LEAVE":"DONE"}
            self.frame = 0
     
        def update(self):
            if self.state != "DONE":
                self.animate()
                self.change_state()
     
        def animate(self):
            now = pg.time.get_ticks()
            if now-self.timer > 1000.0/self.animate_fps:
                if self.state in ("DRIVE","LEAVE"):
                    self.drive()
                self.frame += 1
                self.timer = now
     
        def change_state(self):
            if self.frame >= self.frame_max[self.state]:
                self.frame = 0
                self.state = self.next_state[self.state]
     
        def drive(self):
            self.rect.x += 5
            if not self.frame%10:
                self.rect.y += 10
            elif not self.frame%5:
                self.rect.y -= 10
     
        def draw(self,surface):
            surface.blit(self.image,self.rect)
     
     
     
    class Control(object):
        def __init__(self):
            os.environ["SDL_VIDEO_CENTERED"] = '1'
            pg.init()
            self.screen = pg.display.set_mode((700,500))
            self.screen_rect = self.screen.get_rect()
            self.clock = pg.time.Clock()
            self.done = False
            self.fps = 60.0
            self.background = pg.Surface((self.screen_rect.size)).convert()
            self.background.fill((255,255,255))
            self.car = Car((0,200))
     
        def event_loop(self):
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    self.done = True
     
        def main_loop(self):
            while not self.done:
                self.event_loop()
                self.car.update()
                self.screen.blit(self.background, (0,0))
                self.car.draw(self.screen)
                pg.display.update()
                self.clock.tick(self.fps)
     
     
    if __name__ == "__main__":
        run_it = Control()
        run_it.main_loop()
        pg.quit()
        sys.exit()


    -Mek

    Edit:
    Slight change to code.

    Edit2:
    Another slight change.
    Last edited by Mekire; December 5th, 2013 at 02:20 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0
    wow... I wasnt even close. I didnt think to use classes like that to do it. Thanks! That is amazing. It is using a few things I havent learned yet, but I am sure I can puzzle it all out.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0
    I think I figured it out. The code you posted was getting stuck on "wait" because there was no if condition for when it gets to that state. I threw one in and got it to work in the end. Thanks so much! I feel like a chimp among gods when looking for help here sometimes.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Um... the code I posted does not get stuck on "WAIT". It doesn't need an if statement for "WAIT" because all it does in the "WAIT" state is count frames (unless you are wanting it to put-put up and down in place).

    The code starts in "DRIVE"; the car moves across the screen. Then changes to "WAIT"; the car stops completely. Then changes to "LEAVE" and drives off the screen.

    Once the code changes state to "DONE" the animate function no longer gets called.

    -Mek
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0
    oh. oops. when I pasted it, I screwed up the indentation. my bad. Thanks again!

IMN logo majestic logo threadwatch logo seochat tools logo