April 13th, 2013, 02:11 PM
FPS movement of Obejcts to fast - Logical Problem
I know this is somehow an easy question but i can't get my head around it.
Python3 with pygame
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
April 13th, 2013, 03:16 PM
April 13th, 2013, 04:09 PM
clock.tick() should make the game run at your chosen framerate, as for the zombies,
first, when you make their animations, make it so:
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.
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
April 13th, 2013, 05:10 PM
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
Originally Posted by Lucantrop
April 13th, 2013, 05:27 PM
Well, i didn't read your code, but clock.tick() is probably much shorter(requires only 2 lines):
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.
clock = pygame.time.Clock()
clock.tick(35) # in the main loop
April 13th, 2013, 05:31 PM
Is clock.tick(35) CPU independent? So will it run on all machines with the same speed ?
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
April 14th, 2013, 01:34 AM
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.
April 15th, 2013, 05:00 AM
In terms of restricting the frame rate of an entire game: 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:
where this is a part of an update function for the sprite in question.
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()