It would be easiest if you could post all of your code, especially since when it comes to key mappings in video game creation on Python you really need to read between the lines to find issues. I took a quick look but that doesn't these are the only issues you might be facing currently.
However, from looking at your code I can tell you this:
1. The reason there is no animation when you jump while not moving is because your self.action will always end up being 'idle'
Firstly, we look at the key mapping function you have called handle_event()
. We see that the 'A' and 'D' keys probably wouldn't affect you in this case.
Next we look at your jump. In this case, there don't seem to be any issues.
Finally, we look at your last condition. if self.moving == False:
, then self.action = 'idle'
. But we aren't moving, are we? Because you aren't moving, your self.moving
is False
. And because it is False
, that means the action will end up being your idle animation.
Let's go over how Pygame frames work. In each "frame," of the game, or while loop, it runs the functions it needs to run, and updates to the next frame. Because of this, your game will only update to the next frame after it has finished running your function. That's why you ultimately get 'idle'
as your animation state. It's the last thing in the function before the next frame update, and the conditions are right.
2. You see animation when you jump while moving that repeats indefinitely because you don't properly set your action.
if self.grounded:
, then self.action != 'jump'
. But in Python, the !=
is a comparison operator, not an assignment operator. That means it tells you if something is True
or not, but doesn't do anything. In fact, there is no way to set a variable to anything but one value as you have tried to do here. You would use !=
the same way you would use ==
in an if statement. It basically means "not equal" as in "if not equal".Now I'm going to attempt to "fix" your code. This answer is getting pretty long so I'll just add comments in your code instead of explaining everything. I don't know why but Stack Overflow set my codeblock to css and I don't know how to change it so the colors will be off. This code is to replace your `handle_event` function.
'''In your player object, create a new variable called self.jump_frame = 0'''
# New variable to change the states of the player object. Makes things more organized.
def set_action(self, action=None, direction=None, velx=None, vely=None): #Making variables default to None if not given when set_action() is called
self.action = action if action != None else self.action
self.direction = direction if direction != None else self.direction #This is basically an if statement crammed into 1 line. Feel free to expand it if you want. It basically reads "set self.direction to the variable direction unless direction is None, in which case set it to itself (so that it doesn't change)"
self.vel.x = velx if velx != None else self.vel.x #Same concept
self.vel.y = vely if vely != None else self.vel.y
def handle_event(self, event):
if event.type == pg.KEYDOWN:
if event.key == pg.K_d:
self.set_action(action='run', direction='right', velx=self.speed) #Call our new function
self.moving = True
elif event.key == pg.K_a:
self.set_action(action='run', direction='left', velx=-self.speed)
self.moving = True
if event.key == pg.K_w:
self.jumping = True
if self.jumping == True:
if self.jump_frame < 5: #You can adjust this and change this portion of the code depending on how you want your jump to work or to add more realistic jump momentum
self.set_action(action='jump', vely=10) #We jump for 5 frames, stop for the 5th frame, and then fall until we have hit the floor
elif self.jump_frame == 5:
self.set_action(action='jump', velv=0)
else:
self.set_action(action='jump', vely=-10)
self.jump_frame += 1
if self.grounded:
self.set_action(vely=0)
self.jumping = False
self.jump_frame = 0
elif event.type == pg.KEYUP:
if event.key == pg.K_d:
self.moving = False
if self.jumping == False:
self.set_action(action='idle', direction='right')
elif event.key == pg.K_a:
self.moving = False
if self.jumping == False:
self.set_action(action='idle', direction='left')
if self.moving == False and self.jumping == False: #Make sure that you are DEFINITELY not moving. Jumping is moving too, after all.
self.set_action(action='idle', velx=0)
As for your last question about downvotes, well let's just say that when people who are used to this platform see a response that doesn't follow the standard guidelines for questions (sometimes even if it's a newcomer), they will downvote the question. When downvoted, you lose reputation points. If you get enough downvotes on your question, your question will be automatically closed (or at least I believe so). You can check out the link Julien posted in the comment to check out the guidelines for question asking.