I'm trying to learn game programming (and programming in general), therefore im making a simple sidescroller in Python using pygame.
The problem is, when I'm moving the character (a simple cube) to the left it moves faster than when im moving to the right, as shown in console later on.
The main class with the loop (I've cut out some of it, because it doesn't have relevance, I think, but I can post it if necessary):
player.speed = 135
player = Player.Cube([255,0,0],600, screen.get_height()*0.8-screen.get_height()*0.125,screen.get_height()*0.125,screen.get_height()*0.125)
running = True
clock = pygame.time.Clock()
while running:
deltaTime = clock.tick(60) / 1000
screen.blit(background, (0, 0))
screen.blit(player.image,(player.rect.x,player.rect.y))
screen.blit(grass,(0,screen.get_height()-grass.get_height()))
keystate = pygame.key.get_pressed()
if keystate[K_RIGHT]:
player.moveRight(deltaTime)
elif keystate[K_LEFT]:
player.moveLeft(deltaTime)
pygame.display.flip()
Cube class:
class Cube(pygame.sprite.Sprite):
speed = 0
def __init__(self, color, left,top, width, height):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
self.left = left
self.top = top
self.image = pygame.Surface([width,height])
self.image.fill(color)
self.rect = pygame.Rect(left, top, width, height)
def moveRight(self,deltaTime):
oldX=self.rect.x
self.rect.x += self.speed*deltaTime
print(self.speed*deltaTime)
print("deltaX: " + str(self.rect.x-oldX))
def moveLeft(self,deltaTime):
oldX = self.rect.x
self.rect.x -= self.speed*deltaTime
print(self.speed * deltaTime)
print("deltaX: " + str(self.rect.x-oldX))
As you can see, I'm trying to print out, how many pixels I moved to the right vs how many to the left:
Speed times deltaTime, right: 2.2950000000000004
deltaX, right: 2
exiting game
Speed times deltaTime, left: 2.2950000000000004
deltaX, left: -3
I don't understand one bit of this, does anyone now what the cause of this is? Also, my movement is stuttering a bit, why is that?