diff --git a/Tutorial:Handle-user-inputs.md b/Tutorial:Handle-user-inputs.md index 624ac64..da53e78 100644 --- a/Tutorial:Handle-user-inputs.md +++ b/Tutorial:Handle-user-inputs.md @@ -1,4 +1,4 @@ -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. +A game would be 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: @@ -26,9 +26,9 @@ func main() { # 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, and 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). +Ebiten defines a lot of the buttons for you to use. Almost all of the keyboard buttons, and the three buttons of the mouse (left, right, and 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. +The following example will only use the UP, DOWN, LEFT, and the RIGHT arrow buttons on the keyboard. You can always find the other keys in the documentation which is mentioned above. ```go ebiten.KeyUp, // Up @@ -39,7 +39,7 @@ 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. +Now you know what buttons can Ebiten handle, so what we should 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 is pressed. Rewrite your `update()` function, and we'll use the `DebugPrint()` function to print the texts when some of the buttons are pressed. @@ -65,13 +65,13 @@ func update(screen *ebiten.Image) error { } ``` -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. +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. +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 quite the same. You'll see it in the following example. ```go ebiten.MouseButtonLeft, // Left button of the mouse @@ -79,7 +79,7 @@ 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. +Rewrite your `update()` function again. We've replaced the `IsKeyPressed()` function to `IsMouseButtonPressed()` function this time. ```go func update(screen *ebiten.Image) error { @@ -105,15 +105,16 @@ Execute your game, and try to press the mouse button. ## 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. +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 ( + "fmt" + "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/ebitenutil" - "fmt" ) ``` @@ -125,7 +126,9 @@ func update(screen *ebiten.Image) error { x, y := ebiten.CursorPosition() // Display the information with "X: xx, Y: xx" format - ebitenutil.DebugPrint(screen, fmt.Sprintf("X: %d, Y: %d", x, y)) + if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("X: %d, Y: %d", x, y)); err != nil { + return err + } return nil } @@ -135,17 +138,13 @@ 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. +If you own a gamepad, you can now connect it to your computer and prepare to use it later. In out example, we use a Playstation 3 controller. ## 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. +Note that the key maps might be different based on the controllers, browsers and the platforms. Here's the key map with the PlayStation 3 controller on Firefox and macOS native. ![](http://i.imgur.com/NQ9enHJ.png) @@ -153,7 +152,7 @@ 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... +It would highly be recommended 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 @@ -205,13 +204,13 @@ Now run your game and try with the arrow keys on your controller. ## Axis -You can even handle the axis (Which look like two *mushrooms*) of the controller with Ebiten, Here's the key map for it. +You can even handle the axes (which looks 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. +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. +`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: @@ -238,6 +237,6 @@ func update(screen *ebiten.Image) error { } ``` -Save your file, Run your game and play with your controller axis, and you'll see the following result. +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) \ No newline at end of file