Created Your first game in Ebiten! (markdown)

Yami Odymel 2016-10-25 17:42:26 +08:00
parent c2db4e3ce7
commit d4a13b0187

@ -0,0 +1,111 @@
# What is Ebiten?
[Ebiten海老天](https://github.com/hajimehoshi/ebiten) is a 8-bit game library written in Golang, and you might wondering what is "海老天", Hmm.. it's actually fried shrimp in Japanese 🍤.
![](http://i.imgur.com/NPbjFJp.png)
And Ebiten is a cross-platform library, which means you can run Ebiten on these platforms:
* Desktop: Windows, Mac OS X, Linux
* Mobile: Android, iOS
* Web browser: Chrome and Firefox (GopherJS)
There're already [some examples available on the website](https://hajimehoshi.github.io/ebiten/#examples), and you can also found the [documentation of Ebiten by clicking here](https://godoc.org/github.com/hajimehoshi/ebiten).
# Get Ebiten package
Before we started, you would need to `go get` the Ebiten package:
```bash
$ go get github.com/hajimehoshi/ebiten
```
# Write your first game with Ebiten
Once you got the Ebiten package, you can start to write your own game with Ebiten!
## Import Ebiten
We'll use Ebiten in our program, so the first thing is to `import` the Ebiten package which we `go get` before, so put this code at the top of your program.
```go
package main
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
)
```
## Screen update and display
Anything in the game is **always been updating**(or refreshing), sometimes you can heard people talked about FPS, it means **frames per second**, we would have to **clean the previous screen** and **refresh the screen** each time **we move an element**, so **the element won't leave a long track** on the screen like a snake 🐍.
If we want to display a text on the screen, **it will be disappeared before we see it**, Why? Because we didn't **keep it on the screen**, to do this, we'll need to call a `update()` function **per frame**, then put our code inside of the function, so the text will keep on the screen.
To display a text, we can use `ebitenutil.DebugPrint(image, text)`, it's a function for **displaying a debug information**, but we can use it to display a text also.
```go
func update(screen *ebiten.Image) error {
ebitenutil.DebugPrint(screen, "Our first game in Ebiten!")
return nil
}
```
`screen *ebiten.Image` is **the main canvas in Ebiten** (You can think it as the **main layer of the screen**), and we are going to use `ebitenutil.DebugPrint` to print a `Our first game in Ebiten!` text on our screen.
But hey! it's not done yet, because **you'll need to tell Ebiten to run your `update()` function per frame**.
## Initialize Ebiten
It's time to configure Ebiten thoguh `ebiten.Run(update, width, height, scale, title)`, it tells **what function to loop**, what title should our game be, and the size of the window.
* `update` Is the parameter that you would need to pass a function which will **always be called at the each of the frame**, and yes! **We will pass our `update()` function though here**.
* `width` and `height` are the **width** and the **height** of the game window.
* `scale` is the value that will scale your game window, for example if you set it as `2` and draw a `16x16` canvas on the screen it'll be rendered as 32x32 (twice larger).
* `title` is the title of the game window.
So what are we waiting for? Let's write the following code **in your `main()` function**!
```go
func main() {
ebiten.Run(update, 320, 240, 2, "Hello world!")
}
```
This will generate a **320x240 game window** and **it will be scaled twice larger** (so it's acutally a 640x480 window), and the window **will be titled as `Hello world!`**.
# Review
Here's the fully example, it's short, isn't it?
```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!")
}
```
# Run
Your first game with Ebiten written in Golang is quietly done! Now execute it though the following command in **your terminal**.
```go
go run ./main.go
```
And you can see the result.
![](http://i.imgur.com/53c2pJ6.png)