1

I've coded a bit c++ and java during the past few years and now i've started with python. The goal is to make a game. I've noticed, however, that at the basic animation part of a player moving left and right, my left "speed" is faster than the right "speed". Although my "speed" values are exactly the same.

The update function:

def update(self,dt,game):
    last=self.rect.copy()


    self.time+=dt


    if self.left:
       self.rect.x-=300*dt
    if self.right:
       self.rect.x+=300*dt

I've noticed that if i change the left speed to 250, instead of 300, it will run the same. For example if i press both left and right keys the player will stay on the exact same spot. However if i have the left and right speed both at 300. The player will move slowly to the left.

My game loop:

while 1:
        dt=clock.tick(60)
        if dt>1000/60.0:
            dt=1000/60.0

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                return
            if event.type==pygame.KEYDOWN and event.key==pygame.K_ESCAPE:
                return
            if event.type==pygame.KEYDOWN and event.key==pygame.K_LEFT:
                self.player.left=True
                self.player.image=self.player.leftimages[1]
            if event.type==pygame.KEYDOWN and event.key==pygame.K_RIGHT:
                self.player.right=True
                self.player.image=self.player.rightimages[1]
            if event.type==pygame.KEYUP and event.key==pygame.K_LEFT:
                self.player.left=False
                self.player.image=self.player.leftimages[0]
            if event.type==pygame.KEYUP and event.key==pygame.K_RIGHT:
                self.player.right=False
                self.player.image=self.player.rightimages[0]

        self.tilemap.update(dt/1000.0,self)
        screen.fill((0,0,0)) 
        self.tilemap.draw(screen)
        pygame.display.update()

I'm using a tmx library from Richard Jones to be able to import my map made in tiled map editor. I hope this is sufficient information to answer my weird question. I've asked my other programming friend and he finds it weird.

Some code might be off, beacause i've just copy pasted from my original code that has some more stuff into it. But i've extracted the most basic parts that control the movement.

Thank you for your time!

1
  • Warning, pygame.Rect uses int's, you might be losing precision. You can store player location as float, then set rect.topleft or rect.center to it. Commented Aug 22, 2013 at 0:11

2 Answers 2

1

Have you tried printing the value of dt from your update function? If the speed is different when you move left from when you move right, then that is what must be changing (backed up by you saying that holding left and right at the same time results in no movement).

Based on my own testing, it looks like pygame.time.Clock.tick() returns an integer value representing the number of milliseconds that have passed since the last call. Where you attempt to correct this value (dt=1000/60.0), you will be getting a floating point value. I suspect that for some reason, the timer is off when either pressing left or right, and your attempts to correct it manually are causing a different dt value to be passed in to the update function during these situations.

The pygame documentation suggests pygame.time.Clock.tick_busy_loop() if you need a more accurate (but CPU intensive) timer.

http://www.pygame.org/docs/ref/time.html#pygame.time.Clock.tick_busy_loop

Sign up to request clarification or add additional context in comments.

3 Comments

i've printed the dt value and checked if it is showing any difference but how ever it is not. I call the update function with the dt and all dt's are exactly same. they show 33ms ( 1000/30)... and they do not differ wheter i push the left or the right key :S... Is my approach with the time updating bad? How are most people using update methods and "game physics"?
i've found the error now. It's in the rounding! When my player is moving left and i have a speed of 300 with a clock tick of 60 frames per second. This gives a position change of 1.6 pixels per frame. Now when you subtract the pixels if you take -1.6 it will round it as -2... and when you add 1.6 it will only add it as 1.. I printed it on the command promt and got this Position before 548 Pixel/frame -1.6 Position after 546 Position before 546 Pixel/frame -1.6 Position after 544 Position before 544 Pixel/frame 1.6 Position after 545 Position before 545 Pixel/frame 1.6 Position after 546
@Batko it looks like when the pixels get shown they're just floored, so you can just subtract 1 from the left/up directions and it should work as expected, except in very rare cases. Your comments helped me out; you should post your findings as an answer.
0

Instead of calculating the change in time since the last frame and then basing how far the player will move off of that, you should call pygame.time.Clock.tick once a frame. Then you can assume that the time between frames will be the same every time, so dt will always be the same.

As of now if there is a drop in the fps, everything else in your program will slow down, but your player will start to jump ahead faster than everything else. Rather than relying on time elapsed, it's usually best to depend on the fps.

8 Comments

i am calling it. i defined clock as clock=pygame.time.Clock() then used clock.tick
@Batko I realized that after I posted this, but it still holds true. It is almost always better to have the physics rely on frames as opposed to real time. Is there some reason why you're doing the opposite?
Well when i followed tutorials, the speaker said it was better to follow the time. Well isnt it also frame dependent now aswell?
@Batko How often you update the position is frame dependent, but the actual physics, the velocity (ds/dt), you're making time dependent
Okay, how do you suggest i should make it frame dependent then? I'm a little lost at the moment on how to approach the matter
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.