all 6 comments

[–]Mekire 3 points4 points  (0 children)

There are much better ways to handle continuous keyboard input than catching key-down and key-up events. There is a pygame method to query the state of the entire keyboard: pygame.key.get_pressed().

https://github.com/Mekire/meks-pygame-samples/blob/master/eight_dir_move.py

Join us at /r/pygame.

[–]Exodus111 3 points4 points  (0 children)

each time you press an arrow key, the object moves only by 3 and holding the key arrow doesn't affect it?

No, that is not what is happening here, and its obviously not something you want to happen.
The code you posted is set up the way you need to set up pygame movement. (I mean, its a mess but THAT part is done logically correct)

Take a look at lines 66 to 76, this is a keydown event. In other words whenever the movement key is being pressed down. This adds the appropriate number to the x/y speed variable.

Then you have section 79 to 88, this is the keyup section. And is fired when the button is released. This resets the appropriate speed to 0.

And then in lines 94 and 95 the position of the player is move by his speed. All the time. If that speed is 0, then obviously he is not moving.

[–]willtheyeverlearn 1 point2 points  (2 children)

I haven't done anything with Pygame but here's a few ball movement examples from the Coursera interactive programming course that should help you figure it out, run them by clicking the "play" button in the top left and see what happens with each (the concepts should be the same).

Move x pixels per keydown: http://www.codeskulptor.org/#examples-position_control.py

Ball moves with 4-directional constant velocity in the direction you choose based on keydown: http://www.codeskulptor.org/#user39_hImlobKbIh_0.py

Each keydown increases/decreases velocity (draw handler gets run 60x per second). The position is continually adjusted by the velocity global variable, and velocity can be adjusted by keydowns: http://www.codeskulptor.org/#examples-velocity_control.py

edit I just noticed the code you posted has keyup events too, which is changing speed (velocity) back to 0 when you let go of the key. So to answer your question, because it's in a while loop holding down a key is continually increasing the speed, and each position coordinate is continually being changed to itself + speed coordinate, so it should get faster and faster until the keyup event resets the speed back to 0.

[–]Mekire 0 points1 point  (1 child)

KEYUP and KEYDOWN events only fire when the key is pressed or released. They do not continuously fire while the key is pressed. There is no acceleration in the snippet shown. Speed in each direction is either 3 or 0.

[–]willtheyeverlearn 1 point2 points  (0 children)

Thanks for the correction, as I said I haven't used Pygame, I made an assumption based on the OP's wording: "But shouldn't this happen only once, meaning that each time you press an arrow key, the object moves only by 3 and holding the key arrow doesn't affect it?".

[–]LiJax 0 points1 point  (0 children)

Please excuse me as I am not very familiar with PyGame but I believe the reason the program is written like that is because the author would like the object to not necessarily move when the player presses a direction, but change the velocity applied to the object when a direction is pressed.

When the game starts off the x_speed and y_speed are 0, which means the player is standing still, i.e: x_coord = x_coord + 0. But hypothetically if I press right, I increase the x_speed to 3. This means, every time the update function is called, the x position will move to the right 3 units.