Created Handles the user inputs (markdown)

Yami Odymel 2016-10-30 00:51:35 +08:00
parent c095461ee6
commit 502447714e

227
Handles-the-user-inputs.md Normal file

@ -0,0 +1,227 @@
A game would be extremely boring if the user cannot interactive with it. And this time, you'll learn things about how to handle the keyboard, mouse and the gamepad inputs, so your user can interactive with your game.
Let's back to the example of the our first tutorial, that would enough for this chapter:
```go
package main
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
)
func update(screen *ebiten.Image) error {
ebitenutil.DebugPrint(screen, "Our first game in Ebiten!")
return nil
}
func main() {
ebiten.Run(update, 320, 240, 2, "Hello world!")
}
```
# Keyboard inputs
Ebiten **has defined a lot of the buttons for you to use**, almost **all of the keyboard buttons**, and the **three buttons of the mouse** (*left*, *right*, *middle* button), and the **fifteen buttons on the gamepad**, they can be found [in the documentation](https://godoc.org/github.com/hajimehoshi/ebiten#pkg-constants).
The following example will only used the **UP**, **DOWN**, **LEFT**, and the **RIGHT** arrow buttons on the keyboard, remember! you can always find **the other keys in the documentation which is mentioned above**.
```go
ebiten.KeyUp, // Up
ebiten.KeyDown, // Down
ebiten.KeyLeft, // Left
ebiten.KeyRight // Right
```
## Detect the buttons
Now you knew what buttons can Ebiten handle, so what should we do next is to **check if a button has been pressed or not**, Ebiten provides the `ebiten.IsKeyPressed(key Key)` function for the detection, the `key` parameter accepts the button which we mentioned above, for example `ebiten.KeyUp` is mapping to the "up arrow key" on the keyboard, the function returns `true` when **the button has been pressed**.
Rewrite your **`update()` function**, and we'll use the `DebugPrint()` function to print the texts **when some of the buttons are pressed**.
```go
func update(screen *ebiten.Image) error {
// When the "up arrow key" is pressed..
if ebiten.IsKeyPressed(ebiten.KeyUp) {
ebitenutil.DebugPrint(screen, "You're pressing the 'UP' button.")
}
// When the "down arrow key" is pressed..
if ebiten.IsKeyPressed(ebiten.KeyDown) {
ebitenutil.DebugPrint(screen, "\nYou're pressing the 'DOWN' button.")
}
// When the "left arrow key" is pressed..
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
ebitenutil.DebugPrint(screen, "\n\nYou're pressing the 'LEFT' button.")
}
// When the "right arrow key" is pressed..
if ebiten.IsKeyPressed(ebiten.KeyRight) {
ebitenutil.DebugPrint(screen, "\n\n\nYou're pressing the 'RIGHT' button.")
}
return nil
}
```
By the way, we used `\n` in our example, That's called **new line symbol**, it prevents the texts being stack in the same line. Save the file, and run your game, then press the arrow keys on your keyboard to see if any changes.
![](http://i.imgur.com/igVYeA2.png)
# Mouse inputs
Ebiten also provides the detection of the three buttons of the mouse for you to use. The function for detecting the mouse input is **a little different with the keyboard one**, but **the usage is quietly the same**, you'll see it in the following example.
```go
ebiten.MouseButtonLeft, // Left button of the mouse
ebiten.MouseButtonRight, // Right button of the mouse
ebiten.MouseButtonMiddle // Middle button of the mouse
```
And rewrite your `update()` function again, what should be noticed is that **we've replaced the `IsKeyPressed()` function to `IsMouseButtonPressed()`** function this time.
```go
func update(screen *ebiten.Image) error {
// When the "left mouse button" is pressed...
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
ebitenutil.DebugPrint(screen, "You're pressing the 'LEFT' mouse button.")
}
// When the "right mouse button" is pressed...
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) {
ebitenutil.DebugPrint(screen, "\nYou're pressing the 'RIGHT' mouse button.")
}
// When the "middle mouse button" is pressed...
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonMiddle) {
ebitenutil.DebugPrint(screen, "\n\nYou're pressing the 'MIDDLE' mouse button.")
}
return nil
}
```
Execute your game, and try to press the mouse button.
![](http://i.imgur.com/26wjYjE.png)
## Cursor position
Sometimes you will want to get the position of the cursor in the game, The implementation would be very simple with Ebiten, you can use `ebiten.CursorPosition()` function which returns the `x, y` position of the cursor.
**You will need to import the `fmt` package** for the example by replacing your `import` section to this:
```go
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"fmt"
)
```
Then implement the `ebiten.CursorPosition()` function in the `update()`:
```go
func update(screen *ebiten.Image) error {
// Get the x, y position of the cursor from the CursorPosition() function
x, y := ebiten.CursorPosition()
// Display the information with "X: xx, Y: xx" format
ebitenutil.DebugPrint(screen, fmt.Sprintf("X: %d, Y: %d", x, y))
return nil
}
```
And run your game then try to move your mouse to see the result.
![](http://i.imgur.com/miYSEdK.png)
Remember to **remove the `fmt` package from the `import` section once you don't need it**.
# Controller inputs
If you own a PlayStation 3 controller... congratulations! You can now connect it to your computer and prepare to use it later.
## Key maps
What should be noticed is that **the key maps might be different** based on the **controllers**, **browsers** and the **platforms**.
And here's the key map with the PlayStation 3 controller on **Firefox** and **macOS native**.
![](http://i.imgur.com/NQ9enHJ.png)
With **Chrome**:
![](http://i.imgur.com/ANyqjT9.png)
I would highly recommend you to make a custom configuration for the players to customize their own key maps if you tried to make a game that supports the gamepad...
## Controller example
Let's handle the arrow buttons on the controller **based on the Chrome key map** with Ebiten. Here're the keys that will be used later:
```go
ebiten.GamepadButton12, // Up button
ebiten.GamepadButton13, // Down button
ebiten.GamepadButton14, // Left button
ebiten.GamepadButton15, // Right button
```
And then change your `update()` function to the following code:
```go
func update(screen *ebiten.Image) error {
// When the gamepad "up button" is pressed ...
if ebiten.IsGamepadButtonPressed(0, ebiten.GamepadButton12) {
ebitenutil.DebugPrint(screen, "You're pressing the 'UP' gamepad button.")
}
// When the gamepad "down button" is pressed ...
if ebiten.IsGamepadButtonPressed(0, ebiten.GamepadButton13) {
ebitenutil.DebugPrint(screen, "\nYou're pressing the 'DOWN' gamepad button.")
}
// When the gamepad "left button" is pressed ...
if ebiten.IsGamepadButtonPressed(0, ebiten.GamepadButton14) {
ebitenutil.DebugPrint(screen, "\n\nYou're pressing the 'LEFT' gamepad button.")
}
// When the gamepad "right button" is pressed ...
if ebiten.IsGamepadButtonPressed(0, ebiten.GamepadButton15) {
ebitenutil.DebugPrint(screen, "\n\n\nYou're pressing the 'RIGHT' gamepad button.")
}
return nil
}
```
Now run your game and try with **the arrow keys on your controller**.
![](http://i.imgur.com/BbzejDz.png)
## Axis
You can even handle the axis (Which look like two *mushrooms*) of the controller with Ebiten, Here's the key map for it.
![](http://i.imgur.com/Jg1aAcy.png)
We'll use `ebiten.GamepadAxis(0, axis int)` function to get the values of the axis, for example, based on the key map above, `ebiten.GamepadAxis(0, 0)` returns **the horizontal value** of the **left axis**.
To be more clearly, `GamepadAxis()` returns the floating points with the base value `0.0`, it will be `1.0` when the axis reached the end of a direction, and if you push it to the end of the another direction, it'll be `-1.0` instead.
Let's modify the `update()` function to the following code:
```go
func update(screen *ebiten.Image) error {
axes := []float64{
ebiten.GamepadAxis(0, 0), // 0The horizontal value of the left axis
ebiten.GamepadAxis(0, 1), // 1The vertical value of the left axis
ebiten.GamepadAxis(0, 2), // 2The horizontal value of the right axis
ebiten.GamepadAxis(0, 3)} // 3The vertical value of the right axis
ebitenutil.DebugPrint(screen, fmt.Sprintf("0: %0.6f, 1: %0.6f",
axes[0],
axes[1]))
ebitenutil.DebugPrint(screen, fmt.Sprintf("\n2: %0.6f, 3: %0.6f",
axes[2],
axes[3]))
return nil
}
```
Save your file, Run your game and play with your controller axis, and you'll see the following result.
![](http://i.imgur.com/xbDbTWL.png)