Chapter 4. Control Freak

Most games are designed to be interactive. This means that the player must have some way to control what happens during the game. In the last chapter, you wrote code that displayed the robot and moved him across the screen. Now, you will control the robot!

This chapter will explain how to implement an input system to control the game's character, and interact with the game. Topics will include:

  • Types of input: There are many ways to interact with your game. Typically, games written for the PC depended on the mouse and keyboard. Direct touch input has now become the standard for mobile and tablet devices, and soon every PC will also have a touch-enabled display. We will cover the most common methods to receive input in your game.
  • Using the mouse and keyboard: In this section, you will write code to receive input from the mouse and keyboard to control both the game and our friendly robot.
  • Creating the user interface: In addition to controlling our robot, we also need a way to interact with the game. You will learn how to create an onscreen interface that allows you to control the game and choose the game options.
  • Controlling the character: We want our robot to be able to walk, run, jump, and play! You will learn how to use the mouse and keyboard to control how your robot moves about on the screen.

A penny for your input

It's likely that at some point in your life, you have been part of a conversation that seemed one-sided. The other party was talking and talking, and it didn't seem you could get a word in. After a while, such a conversation becomes quite boring!

The same would happen with a computer game that didn't allow any input. Input is a set of techniques that allows you to control the game. There are many ways to implement an input system, and we will cover them here.

The keyboard input

The most common form of input for most computers is the keyboard. Obviously, the keyboard can be used to enter text, but the keyboard can also be used to directly control the game.

Some examples of this include the following:

  • Using the right arrow, left arrow, up arrow, and down arrow keys to control the character (we'll be using this)
  • Using the W, A, S, and D keys as to move the character (these keys almost form a cross on the keyboard, making them a good substitute to move up, left, down, and right, respectively)
  • Using certain keys to perform predefined actions, such as:
    • Using the Esc key or Q to quit
    • Using the Spacebar or Enter key to fire a projectile

These are just a few examples. In fact, there are some games that seem to use every key on the keyboard!

Using the mouse

The mouse has been around for a long time, so it makes sense that the mouse is used in many games. The mouse can be used in several ways:

  • The left and right mouse buttons can perform specific actions.
  • The wheel can be pushed and used as a third button.
  • The mouse wheel can be used to scroll.
  • The position of the mouse pointer can be tracked and used in conjunction with any of the previous actions. We will use a combination of the left mouse button and the mouse pointer position to click onscreen buttons when we design our user interface.

Touch

More and more devices now respond to touch. Many input systems treat touch very similarly to the mouse:

  • A single touch is equivalent to using the left mouse button
  • A single touch that is held is equivalent to using the right mouse button
  • The position of the finger can be used in the same way as the mouse pointer

However, there are many features of touch that cannot be easily equated to the mouse. For example, most touch interfaces allow several touches to be handled simultaneously. This feature is known as multitouch. This has led to many standard gestures, including:

  • The swipe or flick (moving one or more fingers quickly across the screen)
  • The pinch (moving two fingers together)
  • The zoom (moving two fingers apart)

Unfortunately, we won't be implementing touch in this game because the target device for this book is the PC.

Other inputs

The advent of mobile devices was followed by an explosion of input techniques. Some of the more common ones include:

  • The accelerometer, which can be used to track the physical motion of the device
  • Geolocation, which can be used to detect the physical location of the device
  • The compass, which can be used to detect the orientation of the device
  • The microphone, which can be used to accept voice input

There are many other input techniques, and there is a lot of overlap. For example, most PCs have a microphone. Again, while many games in the mobile market are taking advantage of these alternative input methods, our game will be limited to the keyboard and mouse.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.100.40