I'm new to Python but I decided to give it a try making a fighting game in Pygame. My character will only move left and only when an update is forced.
I tried:
KEY DOWN
if event.key == pygame.K_d:
KEY UP
if event.key == pygame.K_d or event.key == pygame.K_a:
x_change = 0
And it works to an extent. It's just annoying when you need to suddenly change direction, but can't change direction.
I also changed the OR with AND, so it will stop when both are lifted, but that prevents movement from both directions.
Made it do so I can move while not fighting for testing reasons
fighting = False
while not fighting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
fighting - False
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
x_change1 = -5
if keys[pygame.K_d]:
x_change1 = 5
if not keys[pygame.K_a] and keys[pygame.K_d]:
x_change1 = 0
x1 += x_change1
gameDisplay.fill(white)
fighter1(x1,y1)
pygame.display.update()
clock.tick(28)
My guy moves X pixels left only when I force an update such as pressing A again (left) lifting A or moving my mouse over the window. D (right) registers but doesn't move. Once A is pressed, it won't stop going even if the key is up.
I want to fix this so I can then more easily add jumping, attacks and a player 2.
You are missing a not operator here:
if not keys[pygame.K_a] and keys[pygame.K_d]:
x_change1 = 0
Add it like this:
if not keys[pygame.K_a] and not keys[pygame.K_d]:
x_change1 = 0