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!
pygame.Rectuses int's, you might be losing precision. You can store player location asfloat, then setrect.topleftorrect.centerto it.