Members - Reputation: 103
Posted 19 December 2012 - 08:12 PM
Here is the input class:
[source lang="python"]class inputs: videomode = None l = False r = False u = False d = False run = True def getkeys(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.run = False keys = pygame.key.get_pressed() if keys[pygame.K_a]: self.l = True else: self.l = False if keys[pygame.K_w]: self.u = True else: self.u = False if keys[pygame.K_d]: self.r = True else: self.r = False if keys[pygame.K_s]: self.d = True else: self.d = False[/source]
Here is the input function in the game class (where i suspect the problem is):
[source lang="python"]def iupd(self): self.i.getkeys() if self.i.r: self.player.vx = self.player.speed else: self.player.vx = 0 if self.i.l: self.player.vx = -self.player.speed else: self.player.vx = 0 if self.i.d: self.player.vy = self.player.speed else: self.player.vy = 0 if self.i.u: self.player.vy = -self.player.speed else: self.player.vy = 0[/source]
I suspect that something's happening where the right and down get canceled out by the left and up, but I can't see how to correct this.
Members - Reputation: 922
Posted 19 December 2012 - 10:47 PM
(example assuming right is true, left is false)
In the second method posted (iupd()), you're canceling out, for example, by looking at r and setting the player's x-direction speed and then looking at l and setting that very same player's x-direction speed to 0. Note that just as a fluke of the way you have ordered the if-else statements, left will cancel out right's motion but right won't cancel out left's motion (because you have the if self.i.r and then after that you have if self.i.l).
You have the same situation going with your y direction speed.
One way to address that problem is to make your method more like:
self.i.getkeys() self.player.vx = 0 self.player.vy = 0 if self.i.r: self.player.vx += self.player.speed if self.i.l: self.player.vx -= self.player.speed if self.i.d: self.player.vy += self.player.speed if self.i.u: self.player.vy -= self.player.speedWith this approach is that if you press left and right simultaneously, vx will add the variable speed and then subtract that same value, balancing out at 0 x-direction speed, which seems reasonable for someone trying to go both left and right at the same time.
Another idea would be to do this:
if self.i.r: self.player.vx = self.player.speed else: if self.i.l: self.player.vx = -self.player.speed else: self.player.vx = 0It brings in the side effect that if someone were pressing right and left at the same time, the player would go right (because the first else wouldn't get hit to check for left being true).
Edited by j-locke, 19 December 2012 - 10:49 PM.