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

    Join Date
    Apr 2013
    Posts
    23
    Rep Power
    0

    Arrow FPS movement of Obejcts to fast - Logical Problem


    Hi

    I know this is somehow an easy question but i can't get my head around it.

    Some info:
    Python3 with pygame
    Linux Os

    Ok here is the Situation:

    I got a Timer and he repeats every 0.02 seonds the execution of gameLoop. So this way i will get 35 FPS in the whole game without relying on CPU time. Game loop also draws everything on the screen. This approach is ok right ? I could only find tutorials who use some kind of CPU cycles to manage Frames...

    Now i got a zombie class. The zombie moves 1px per frame. That is quite fast for a zombie!

    So for now i have a counter that the zombie only moves every 3 frame to make it slower. I think think is the wrong approach.

    Another idea i had was to create another timer thread for moving the zombie maybe 1px per second. so this thread would be executed every second. I think this would be a better approach.

    But here are my questions:

    If i use another thread for moving can it go out of think with the main loop and does this have any consequences for interaction (shooting at the zombie )?

    I would probably need another thread for attack animations for the zombie this would ran at different speed. in drawing.

    What do you think of my approaches ?
    Which on is better ?
    Do you have another idea of impelementing this ?

    Code can be downloaded: http://www.filedropper.com/gof2

    Thanks for getting my head straight

    Manuel
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0
    take a look at clock.tick()
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    2

    Post


    clock.tick() should make the game run at your chosen framerate, as for the zombies,
    first, when you make their animations, make it so:
    Code:
    speed = 3
    mov_speed = 3
    def update(move, speed, mov_speed):
        if move == True:
            speed -= 1 # countdown to changing animation frame
            x += mov_speed # makes him move 3 pixels per frame
            if speed == 0:
                # next animation frame
    That means the zombie moves 90 pixels per second if you have your clock.tick() at 30 frames. Put it into your zombie class, tho that maybe too fast, so try slowing it down if you need to.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    23
    Rep Power
    0
    Thanks for the zombie method.

    Is there an more elegant way ?

    Also it the idea with the thread (time based redraw )so good or should i use the clock.tick() method ?

    Thanks a lot

    Manuel

    Originally Posted by Lucantrop
    clock.tick() should make the game run at your chosen framerate, as for the zombies,
    first, when you make their animations, make it so:
    Code:
    speed = 3
    mov_speed = 3
    def update(move, speed, mov_speed):
        if move == True:
            speed -= 1 # countdown to changing animation frame
            x += mov_speed # makes him move 3 pixels per frame
            if speed == 0:
                # next animation frame
    That means the zombie moves 90 pixels per second if you have your clock.tick() at 30 frames. Put it into your zombie class, tho that maybe too fast, so try slowing it down if you need to.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    2
    Well, i didn't read your code, but clock.tick() is probably much shorter(requires only 2 lines):
    Code:
    clock = pygame.time.Clock()
    # and
    clock.tick(35) # in the main loop
    It's also helluva lot easier to keep track of, and you can directly change the frames per second, instead of the other way around.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    23
    Rep Power
    0
    Is clock.tick(35) CPU independent? So will it run on all machines with the same speed ?


    Code:
    tick()
        update the clock
        tick(framerate=0) -> milliseconds
        :sg:` -> `
    
        This method should be called once per frame. It will compute how many milliseconds have passed since the previous call.
    
        If you pass the optional framerate argument the function will delay to keep the game running slower than the given ticks per second. This can be used to help limit the runtime speed of a game. By calling Clock.tick(40) once per frame, the program will never run at more than 40 frames per second.
    
        Note that this function uses SDL_Delay function which is not accurate on every platform, but does not use much cpu. Use tick_busy_loop if you want an accurate timer, and donít mind chewing cpu.

    Originally Posted by Lucantrop
    Well, i didn't read your code, but clock.tick() is probably much shorter(requires only 2 lines):
    Code:
    clock = pygame.time.Clock()
    # and
    clock.tick(35) # in the main loop
    It's also helluva lot easier to keep track of, and you can directly change the frames per second, instead of the other way around.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0
    Clock.tick() will run at the specified FPS if the computer it's running on is capable of keeping up.

    30 fps == 30 fps on any all machines as long as they are fast enough.

    Think of it as specifying a maximum FPS that you want your game to run at.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    In terms of restricting the frame rate of an entire game:
    Code:
    Myclock.tick(fps)
    is what SHOULD be used (where Myclock is an instance of pygame.time.Clock). This would be called once per frame, the same time you call pygame.display.update (or flip).

    On the other hand, if we are talking about restricting the frame rate of a specific animation you usually want something more like this:
    python Code:
    if pygame.time.get_ticks()-self.timer > 1000/self.fps_of_animation:
        self.current_frame = (self.current_frame+1) % len(self.frame_list)
        self.image = self.frame_list[self.current_frame]
        self.timer = pygame.time.get_ticks()
    where this is a part of an update function for the sprite in question.

    -Mek

IMN logo majestic logo threadwatch logo seochat tools logo