Timeline for How to make objects move continuously while a key is pressed in pygame?
Current License: CC BY-SA 4.0
14 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| S Jan 12, 2022 at 3:10 | vote | accept | Command_Blocks | ||
| S Jan 12, 2022 at 3:10 | vote | accept | Command_Blocks | ||
| S Jan 12, 2022 at 3:10 | |||||
| Jan 11, 2022 at 13:24 | vote | accept | Command_Blocks | ||
| S Jan 12, 2022 at 3:10 | |||||
| Jan 11, 2022 at 9:43 | answer | added | YoloWex | timeline score: 2 | |
| Jan 4, 2022 at 12:41 | answer | added | Command_Blocks | timeline score: 1 | |
| Jan 4, 2022 at 12:28 | comment | added | Command_Blocks |
@DMGregory I did not try anything, I was just "curious". Anyway, after watching many tutorials on pygme, I got it. All you needed to do was create some variables like "leftPressed" and by default set it to false. and in pygame.KEYDOWN: set it to True. Then in pygame.KEYUP set it to False. and then create a movement block for the object use if leftPressed and then use the desired movement. It is very smooth. I am also going to answer my own question for this as not many people ask this question.
|
|
| Jan 4, 2022 at 3:41 | comment | added | DMGregory♦ | Sounds like you should edit your question to show what you tried. | |
| Jan 4, 2022 at 3:26 | comment | added | Command_Blocks |
so using variables and if and elif statements isn't hard. I never ran into trouble. I was just wondering. But how am I supposed to implement the use of variables in my code? I did the most sensible thing to do and it still does not work. I first did an event checker and if the down key is pressed while being held, Down_Key_Pressed = True. Then did the same with the key being released event, but this time it sets it to True. Then used if to move the hero. Unfortunately, no improvement. Could you edit the main loop and share it with me?
|
|
| Jan 3, 2022 at 16:24 | comment | added | DMGregory♦ |
When you get a key press event you store downButtonPressed = true and when you get a key released event you store downButtonPressed = false and then in your game's main update loop you check the value of downButtonPressed to decide whether to move the character down one frame's worth. Where did you run into trouble with this?
|
|
| Jan 3, 2022 at 16:00 | comment | added | Command_Blocks | @DMGregory I understood your first line, but what do you mean by "Presumably you considered storing a variable that says "the down key is currently held" and then using that variable to apply movement every frame in your main update loop?" ? all I want is the y-axis of hero to change continuously while the down / up key is held down and it should become stationary when released. | |
| Jan 3, 2022 at 15:54 | history | edited | Command_Blocks | CC BY-SA 4.0 |
Added full code for clarity
|
| Jan 3, 2022 at 15:16 | comment | added | DMGregory♦ | Right now you're only reacting when a new key down signal comes from the OS (ie. when the key is first pressed, and then at an OS-configurable key repeat rate after that). Presumably you considered storing a variable that says "the down key is currently held" and then using that variable to apply movement every frame in your main update loop? | |
| S Jan 3, 2022 at 15:08 | review | First questions | |||
| Jan 5, 2022 at 17:34 | |||||
| S Jan 3, 2022 at 15:08 | history | asked | Command_Blocks | CC BY-SA 4.0 |