Hello, World!

As the first Ebitengine game, let's show the message 'Hello, World!'.

Before executing the program, you need to install Ebitengine. See the instruction to install Ebitengine.

Code

Create a file main.go like this:


Then, execute this Go program by go run:

go run main.go

If you see the screen with the text 'Hello, World!', congratulations, your program succeeded to work!:

How the code works


  

Imports Ebitengine packages. This program imports two packages: github.com/hajimehoshi/ebiten/v2 and github.com/hajimehoshi/ebiten/v2/ebitenutil.

github.com/hajimehoshi/ebiten/v2 is the core package of Ebitengine, which provides functions for drawing and inputs.

github.com/hajimehoshi/ebiten/v2/ebitenutil is the utility package of Ebitengine. In this program, this package is used to print a debug message on the screen.

There are other several packages in Ebitengine. See the packages page for more details.


  

Defines Game struct. Game implements ebiten.Game interface. ebiten.Game has necessary functions for an Ebitengine game: Update, Draw and Layout. Let's see them one by one.


  

Defines (*Game).Update function, that is called every tick. Tick is a time unit for logical updating. The default value is 1/60 [s], then Update is called 60 times per second by default (i.e. an Ebitengine game works in 60 ticks-per-second).

Update updates the game's logical state. This hello-world example doesn't have any state and then this function does nothing.

Update returns an error value. In this code, Update always returns nil. In general, when updating function returns a non-nil error, the Ebitengine game suspends. As this program never returns a non-nil error, the Ebitengine game never stops unless the user closes the window.


  

Defines (*Game).Draw function, that is called every frame. Frame is a time unit for rendering and this depends on the display's refresh rate. If the monitor's refresh rate is 60 [Hz], Draw is called 60 times per second.

Draw takes an argument screen, which is a pointer to an ebiten.Image. In Ebitengine, all images like images created from image files, offscreen images (temporary render target), and the screen are represented as ebiten.Image objects. screen argument is the final destination of rendering. The window shows the final state of screen every frame.


  

ebitenutil.DebugPrint is a utility function to render a debug message on an image. In this case, screen is passed as the render target of the debug printing. As screen is reset (or cleared in another word) whenever Draw is called, DebugPrint should be called every time.


  

Defines (*Game).Layout function. Layout accepts an outside size, which is a window size on desktop, and returns the game's logical screen size. This code ignores the arguments and returns the fixed values. This means that the game screen size is always same, whatever the window's size is. Layout will be more meaningful e.g., when the window is resizable.


  

Defines the main function, which is the entry point of this program.


  

ebiten.SetWindowSize sets the window's size. Without calling this, the default window size is used.


  

ebiten.SetWindowTitle sets the window's title.


  

ebiten.RunGame is the function to run the main loop of an Ebitengine game. The argument is a Game object, that implements ebiten.Game. ebiten.RunGame returns a non-nil error value when an error happen. In this program, when a non-nil error is returned, this error is logged as a fatal error.

你好,世界!

作为您的第一个Ebiten游戏, 让我们先显示一个'Hello, World!'.

在开发前, 您需要先安装Ebiten:安装教程

编写代码

创建一个类似这样的 main.go 文件:


  

然后,用go run运行代码:

go run main.go

如果您看到 'Hello, World!'显示在左上角, 恭喜, 您的游戏已经运行起来了:

工作原理


    

导入Ebiten包. 这个程序需要两个包: github.com/hajimehoshi/ebiten/v2github.com/hajimehoshi/ebiten/v2/ebitenutil.

github.com/hajimehoshi/ebiten/v2 is the core package of Ebiten, which provides functions for drawing and inputs.

github.com/hajimehoshi/ebiten/v2/ebitenutil is the utility package of Ebiten. In this program, this package is used to print a debug message on the screen.

There are other several packages in Ebiten. See the packages page for more details.


    

定义 Game 结构体. Game implements ebiten.Game interface. ebiten.Game has necessary functions for an Ebiten game: Update, Draw and Layout. Let's see them one by one.


    

定义 (*Game).Update 函数, 每一tick都会调用这个函数. Tick is a time unit for logical updating. The default value is 1/60 [s], then Update is called 60 times per second by default (i.e. an Ebiten game works in 60 ticks-per-second).

Update updates the game's logical state. This hello-world example doesn't have any state and then this function does nothing.

Update returns an error value. In this code, Update always returns nil. In general, when updating function returns a non-nil error, the Ebiten game suspends. As this program never returns a non-nil error, the Ebiten game never stops unless the user closes the window.


    

定义 (*Game).Draw 函数, 每一帧都会调用这个函数. Frame is a time unit for rendering and this depends on the display's refresh rate. If the monitor's refresh rate is 60 [Hz], Draw is called 60 times per second.

Draw takes an argument screen, which is a pointer to an ebiten.Image. In Ebiten, all images like images created from image files, offscreen images (temporary render target), and the screen are represented as ebiten.Image objects. screen argument is the final destination of rendering. The window shows the final state of screen every frame.


    

ebitenutil.DebugPrint is a utility function to render a debug message on an image. In this case, screen is passed as the render target of the debug printing. As screen is reset (or cleared in another word) whenever Draw is called, DebugPrint should be called every time.


    

定义 (*Game).Layout 函数. Layout accepts an outside size, which is a window size on desktop, and returns the game's logical screen size. This code ignores the arguments and returns the fixed values. This means that the game screen size is always same, whatever the window's size is. Layout will be more meaningful e.g., when the window is resizable.


    

定义 main 函数, 作为入口函数.


    

ebiten.SetWindowSize 设置窗口大小. 若没有设置则使用默认窗口大小.


    

ebiten.SetWindowTitle 设置窗口标题.


    

ebiten.RunGame is the function to run the main loop of an Ebiten game. The argument is a Game object, that implements ebiten.Game. ebiten.RunGame 当错误发生时,函数将返回一个非空的error. 在这段程序中, 当error非空时, 错误日志将会以fatal error输出.