Hot answers tagged

85

First of all, XNA also uses C# too so it's the same programming language. And although the underlying API might have some differences from DirectX, that has nothing to do with jumping, so the same tutorials or answers should apply here. Also, there are numerous ways to implement this, depending on a lot of factors. What I'll describe below is just one of the ...


42

Yes, this is normal for a real-time game to try to use 100% CPU to perform as fast and good as it can. So that player sees as much frames per second or as good physics simulation or anything else as his PC can provide. In your case - No, this looks like an inefficient design, to take a thread and make it poll for events in a loop (while true do ...


37

Yes, when you want your game to run on PC, you should think from an early design stage on how to best utilize the mouse. The same applies to the input features of all other platforms you consider targeting. One of the main differences between a good port and a bad port to a different platform is how much effort you invested into accommodating the different ...


34

Although you are targeting desktops, there will be players on (gaming) laptops and for some of them, it will be an inconvenience to get a mouse before being able to play your game. It would sound like a good thing to me, if you were to support alternative control schemes or customizable controls. This is not a very "sciency" answer - I've just run into this ...


31

To simulate a time lag, use a circular buffer to store the last N frames' mouse positions. Store the current mouse position each frame. In your control calculations, use the oldest mouse position from the buffer instead of the current mouse position.


29

You can't. At least, not as a game developer. As a gamer, you can purchase more expensive keyboards with "anti-ghosting" features, but otherwise the limitation is part of the hardware itself, so there's nothing you can do in software to solve it. Check out this demo page to see how keyboard ghosting works, plus a demo: https://web.archive.org/web/...


24

While you can implement this using SDL_WarpCursor(), I've run into problems with that method on some platforms. I've had real problems with some platforms not reliably performing the WarpCursor() action, particularly when I've been calling it every frame. Also, remember that on many platforms, the cursor is handled at a higher frequency than your app. ...


22

Split this into several layers. At the lowest layer you have raw input events from the OS. SDL keyboard input, mouse input, joystick input, etc. You might have several platforms (SDL is a least-common-denominator lacking several input forms, for instance, which you might later care about). You can abstract these with a very low-level custom event type, ...


22

It really depends what you mean by "assume". Are you making this assumption at the point of designing your gameplay mechanics? Or at the point of deciding whether or not to implement fully customizable key bindings? You could mean "I assume real gamers have a 3 button mouse, therefore I don't need to offer the option to rebind bayonet-thrust to a keyboard ...


19

Keyboards have hardware limitations on how many keys can be pressed and recognized at the same time. Your code is not the problem here, it's most likely the design of the keyboard.


16

If you stop calling it "pushing forward" on the joystick/mouse, and start calling it "pushing up" (which is the way that most players think of it -- particularly the ones who don't play flight simulators), then the "invert Y axis" name makes complete sense, since pushing up causes the player to look down. Edit: The fundamental issue here is people's mental ...


14

What is often used is an intermediate Intent System which abstracts the input and keeps track of the context and relevant gamestates. The Intent system will stop transmitting inputs when the simulation is paused for example. It also handles the mapping between controller events and intents (move in direction, run, shoot, reload...). This way your other ...


12

Since asked by the thread starter, I elaborate on event managers. I think this is a good way to handle input in a game. An event manager is a global class which allows to both register callback functions to keys and to fire off those callbacks. The event manager stores registered functions in a private list grouped by their key. Each time a key gets fired, ...


11

An SDL_KEYDOWN event is only sent when the key is first pressed. You will receive an SDL_KEYUP event when it's released. You'll want to handle moving in code which gets called every frame, not in response to an event. Inside Avatar::handle_input, you'll instead want to set variables to tell you whether each key is up or down, and update those variables as ...


11

Keyboards have a key matrix, where the buttons have been arranged into something roughly square, with the keyswitches each being tied to one row and one column. The keyboard activates each row and then reads the columns. If you sketch this out you will see that some button combinations must activate "phantom" keys. You can fix this with one diode per button (...


10

I wrote a series of articles about building a platform game from the ground up using modern technologies and it includes how I handled the simple physics: http://www.wildbunny.co.uk/blog/2011/12/14/how-to-make-a-2d-platform-game-part-2-collision-detection/ However if you want something more high-tech it's entirely possible to approach this using full a ...


10

The XNA Keys struct is not marked with the Flags Attribute which means that it's not legal to | a load of keys together like that. Keys keyCombo = Keys.Left | Keys.A; Console.WriteLine(keyCombo); Will print NumPad5, which is clearly not what you want here! You've never been able to do this in any version of XNA, I think the book is just wrong here :O ...


10

How about you use a Queue<T>? There are 4 significant members of Queue<T> you can use: Queue.Enqueue(T item) - put your item at the end of the queue. T Queue.Peek() - look at the first item without removing it. T Queue.Dequeue() - get and remove the first item. Queue.Count - check how many items are in queue. Sample usage Here's what could ...


10

This is actually really simple. All you have to do is add another ClickListener which listens for Right Clicks (the default only listens to left clicks). To do this all you have to do is this: someButton.addListener(new ClickListener(Buttons.RIGHT) { @Override public void clicked(InputEvent event, float x, float y) { //do whatever } }...


10

Many laptops lack a middle button, especially those with a trackpad, and you need special software to emulate it. Mac laptops have only one button. Right-click is pretty easy (two finger click) and not uncommon in Mac games, and the two finger drag to scroll isn't bad, but only in slower paced games. However, there is no concept of a middle click in the ...


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 ...


9

Moving the joystick in a complete circle gives you the upper bounds of the joystick range, but the at rest (i.e. no input) position of the joystick can't be accurately determined from those values. You could assume that that rest position is the exact center of the available range but that is only true in ideal circumstances. Over time controllers wear in ...


9

A game with a constant active window refresh will mostly be ran in full-screen mode and be the only (big) program of the machine managed by the OS. So it is totally acceptable to use 100% of CPU, because nothing else should need it. However, that doesn't means of course you have to always use 100% whatever your game state. Most game loops use a sleep method ...


9

There are a few drawbacks of aiming to use all available CPU time in a PC or mobile game. System requirements: Even if a game is playable on the PC where you develop your game, it might not be playable on a weaker PC owned by someone who bought your game. Limiting CPU use will keep a game usable on machines that more people are likely to already have. If ...


8

The network layer needs to have an agreed-upon clock. They can agree upon a clock value early in the game (and resync it periodically in case of drift) so the host knows how long any particular packet took to actually arrive and when the client did the action, and vice versa. See this article for one possible way to synchronize clocks in games. There are ...


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) ...


7

The first method is an implementation of the observer pattern, the second is not. The observed object (your input interface) should not need to know what the observing object (your game engine) will do with the notification, only that it wants to be notified. However, I am not sure why you have decided to use the observer pattern here. Unless you are going ...


7

On Nintendo and Sony hardware we sometimes polled the input at a higher rate than we drew frames; use a timer-driven thread to look at the controller I/O at 120hz, for example, even when we were rendering at 30hz. By "a timer-driven thread" I mean there was a hardware timer that poked an interrupt that caused a particular function to get called in realtime. ...


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