10

An alternative way (Josh's approach is great too!) would be to setup an boolean on SDL_KEYDOWN, and possibly also ignoring all repeated key events. That you can do by checking the repeat member of the key event. Then you could implement your own timer, which doesn't have to be anything fancy, and implement key repetition your self. You could either trigger ...


9

Your problem is the fact that you're only looking at KEYDOWN events. What you need to do is toggle a boolean value when a key is pressed or released. Something like this would work: # event loop for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: # check for key ...


8

Firstly, you need to use a switch case statement and decide which direction overrides the others, for example, if they press all four buttons, which button should be listened to for input? This gets put in order within the switch statements. Edit: For clarity about the above statement. You do need to use a switch case statement (or similar structural logic) ...


6

Possible cause : From your code, no visible sources of lag are present. The lag may come from the way they are accessed from the game loop thread. Dont forget, the variables have to be synchronized, or you have to use locks for your app to be thread safe. The lag you experienced may be from the fact that you increment the x and y by 5 each. I am guessing ...


6

In your keyPressed and keyReleased you can use a Map<Integer, GameInput> to map the KeyEvent.VK_* to GameInput Make a new enum with the actions you want to be controllable enum GameInput{ FORWARD, LEFT, RIGHT, BACK,PAUZE,... } And in Controller you have a Map<Integer, GameInput> that you use: public void keyPressed(KeyEvent e) { game....


6

To answer your first Question "why does the Engine behave like this" I would add the following: The Engine does not actually detect the key being pressed multiple times. What you need to keep in mind here is that the "was pressed" state of the key does not reset until the next frame, and your code will be executed in a single frame. Furthermore, every if ...


6

This is an excellent question because you are experiencing a problem that's quite commonplace with less experienced programmers: attempting to solve your problem with a purely imperative approach. Luckily this problem has been encountered and solved by people much smarter than I, thanks to the mathematical concept of a finite state machine. A finite state ...


5

Accordingly to http://legacy.lwjgl.org/javadoc/org/lwjgl/input/Keyboard.html you can make this like this: while (Keyboard.next()) { bool pressed = Keyboard.getEventKeyState(); int key = Keyboard.getEventKey(); if(pressed) processKeyPress(key); else processKeyRelease(key); } For more detail, you can google "Buffered input vs Unbuffered input"...


5

This is pretty simple. Basically, you just have another key call the same function as U+K does. if((keyDown('U') && keyDown('K')) || keyDown('O')) doPowerBlock(); Basically that's saying if the U key AND the K key are pressed, OR the O key, do the action. Or depending on how you have things arranged, you could do something like: if((keyDown('U'...


5

The best way to solve your concerns for flexibility and comfort of the user is to certainly allow them to define their bindings as you have described. Whether you opt to do this through a configuration file they manually edit or an in-game screen that allows them to select the action and then press the key configurations for said binding is entirely up to ...


5

Your issues does not seems to be "How do I accept only one directional input per update?" but rather "How do I prevent my snake to go backward?" From the many comments and discussion, you seem to be having a frame-rate that is not appropriate for the game you're developing. Traditionally, Snake is a game with a very low frame-rate, the snake moved only 2-3 ...


5

It is due to your flow is confusing for engine as it will try to execute your every check in every frame. Try to implement with else or with single statement like, using UnityEngine; using System.Collections; public class TestingKeyGetRecursion : MonoBehaviour { int counter = 1; void Update () { if (Input.GetKeyDown(KeyCode.F)) ...


5

Just as a heads up, most keyboards have a finite limit of keys that can be pressed. For example, I have an Asus Transformer Book and you can only press around ~5 keys at a time. Some keyboards also won't allow more than a few keys pressed if shift or control is being pressed. (Like my Transformer Book...) And yes, most of the time, this is hardware limited, ...


5

From your recapitulation this might be an key rollover problem. The term key rollover describes a tradeoff in keyboard design complexity for reduced production cost. Many standard keyboards have issues detecting more than three keys being pressed simultaneously. As you are using Swing, that means you are using java. So in order to find out if it is a ...


4

I don't know python, but have you tried setting a bool to true when the key is pressed, and changing the speed in an if statement based on that bool. When the key gets released you just have to set the bool back to false.


4

Behind the scenes, typically input is polled anyway, just by the os behind your back ;) Regardless, even if you poll you're going to need to package state changes up in some way in order to use that information elsewhere in your game. And odds on you're going to package that up in an event. So realistically, going straight to events is going to save you ...


4

Polling mechanism The software check for a condition repeated times until something is ready. E.g. Childs in a car: Child: Did we arrived yet? Mom: No; Child: Did we arrived yet? Mom: No; .... (repeat many times) Child: Did we arrived yet? Mom: Yes Events The condition itself is able to inform the software when it is ready. E.g. ...


4

You may be dealing with keyboard ghosting, the phenomenon where certain keys cannot be pressed down simultaneously. This differs between different keyboards, but you can test it out in the link I've provided, which has an interactive program for testing key presses. You may be able to fix this problem by finding different keys that can be pressed together, ...


4

A summary Instead of mapping the input direcly to the action (and using enums will keep doing that because you would still have to map the key to the enums), you should make the input consume a method it holds. If it was an X-Box, for example, the X button can perfom an action defined by the player Use the inferface above to create something like bellow ...


4

Try this: @Override public void keyboardFocusChanged(FocusEvent event, Actor actor, boolean focused) { super.keyboardFocusChanged(event, actor, focused); if (!focused) setOnscreenKeyboardVisible(false); } Otherwise you could go for a better work around. You place a big clickable transparent actor behind everything. When you click anywhere ...


4

Good news! This code: bool ctrl = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl); already respects the user's OS settings for sticky keys and key remapping on Windows and macOS at least. And this works whether you specify the key by KeyCode, by a "Conventional Game Input" string, or by a remappable GetButton("Some Custom ...


3

I don't know python or pygame, but assuming you're using a game library there should be a way to poll the state of the key, such as if it's currently down or not instead of if it was pressed since last update. Use that for checking and updating movement. The next problem you will run into is it will update movement as fast as your logic update interval is ...


3

Your problem is that you are doing the update logic in keyPressed() method. When you press a key, AWT will call this method once. After a delay (typically 250-1000 milliseconds) it starts calling this method again repeatedly with a smaller delay (typically 30-400 milliseconds). When the key is released, this repetition stops and keyReleased() method is ...


3

Most big budget games (those with an international distributor) will work out of the box, using ZQSD instead of WASD for azerty layouts. A decent game will also introduce the layout in the first level, others will let the player discover it through the customisation screen. The implementation strategy can be detected by asking the OS to switch layouts. ...


3

Let the user configure the bindings from an options menu in the application. Players might be using QWERTZ (German) or AZERTY (French) layout keyboards, Dvorak layout keyboards, joysticks, etc., and they don't want to have to learn Yet Another Config File Language. But still, a lot of players will want to jump in and play without changing any settings. So ...


3

SDL2 actually generates key repeat events automatically. You need to manually filter repeated key events out by checking the repeat member of a SDL_KeyboardEvent. This is something that did not automatically happen in SDL1, so that's why the behaviour is different. See SDL_KeyboardEvent for more info.


3

You need a Listener indeed. Then you can store the pressed keys in an array, and manage it like this. bool keyPressed[256]; // Do not forget to initialize all to false if java does not do it; onKeyPressed(event e) { keyPressed[e.keyCode] = true; } onKeyReleased(event e) { keyPressed[e.keyCode] = false; } // in game loop you can then check like ...


Only top voted, non community-wiki answers of a minimum length are eligible