2021SC@SDUSC Ebiten(一) 基础框架及图像填充代码详解

2021SC@SDUSC

Go语言的简单2D游戏库-Ebiten   

 

目录

Go语言的简单2D游戏库-Ebiten   

1、入门:Hello World! 

2、图像填充


1、入门:Hello World! 

代码

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

package main

import (
	"log"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)

type Game struct{}

func (g *Game) Update() error {
	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	ebitenutil.DebugPrint(screen, "Hello, World!")
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
	return 320, 240
}

func main() {
	ebiten.SetWindowSize(640, 480)
	ebiten.SetWindowTitle("Hello, World!")
	if err := ebiten.RunGame(&Game{}); err != nil {
		log.Fatal(err)
	}
}

然后,通过go run以下方式执行这个 Go 程序:

go run main.go

如果您看到带有“Hello, World!”文本的屏幕,恭喜您,您的程序成功运行!:

2021SC@SDUSC Ebiten(一) 基础框架及图像填充代码详解_第1张图片

代码如何工作

package main

import (
	"log"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)

进口 Ebiten 包。该程序导入两个包:github.com/hajimehoshi/ebiten/v2github.com/hajimehoshi/ebiten/v2/ebitenutil.

github.com/hajimehoshi/ebiten/v2 是Ebiten的核心包,提供绘图和输入功能。

github.com/hajimehoshi/ebiten/v2/ebitenutil是 Ebiten 的实用程序包。在这个程序中,这个包用于在屏幕上打印调试信息。

Ebiten 中还有其他几个包。有关更多详细信息,请参阅包页面。

type Game struct{}

定义Game结构。Game实现ebiten.Game接口。ebiten.Game具有 Ebiten 游戏的必要函数:UpdateDrawLayout。让我们一一看看。

func (g *Game) Update() error {
	return nil
}

定义(*Game).Update函数,每个刻度都会调用。Tick 是逻辑更新的时间单位。默认值为 1/60 [s],然后Update默认每秒调用 60 次(即 Ebiten 游戏以每秒 60 次滴答运行)。

Update更新游戏的逻辑状态。这个 hello-world 示例没有任何状态,然后这个函数什么都不做。

Update返回错误值。在此代码中,Update始终返回nil. 一般来说,当更新函数返回非零错误时,Ebiten 游戏暂停。由于这个程序从不返回非零错误,除非用户关闭窗口,否则 Ebiten 游戏永远不会停止。

func (g *Game) Draw(screen *ebiten.Image) {

定义(*Game).Draw函数,即每帧调用一次。帧是渲染的时间单位,这取决于显示器的刷新率。如果显示器的刷新率为 60[Hz],Draw则称为每秒 60 次。

Draw接受一个参数screen,它是一个指向 an 的指针ebiten.Image。在 Ebiten 中,所有图像,如从图像文件创建的图像、屏幕外图像(临时渲染目标)和屏幕都被表示为ebiten.Image对象。screen参数是渲染的最终目的地。该窗口显示screen每一帧的最终状态。

	ebitenutil.DebugPrint(screen, "Hello, World!")
}

ebitenutil.DebugPrint是一个实用函数,用于在图像上呈现调试消息。在这种情况下,screen作为调试打印的渲染目标传递。由于screen复位(或其他字清零)时Draw被调用,DebugPrint应该叫每次。

func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
	return 320, 240
}

定义(*Game).Layout功能。Layout接受外部尺寸,即桌面上的窗口尺寸,并返回游戏的逻辑屏幕尺寸。此代码忽略参数并返回固定值。这意味着无论窗口大小如何,游戏屏幕大小始终相同。Layout将更有意义,例如,当窗口可调整大小时。

func main() {

定义main函数,它是这个程序的入口点。

	ebiten.SetWindowSize(640, 480)

ebiten.SetWindowSize设置窗口的大小。不调用它,使用默认的窗口大小。

	ebiten.SetWindowTitle("Hello, World!")

ebiten.SetWindowTitle 设置窗口的标题。

	if err := ebiten.RunGame(&Game{}); err != nil {
		log.Fatal(err)
	}
}

ebiten.RunGame是运行 Ebiten 游戏主循环的函数。参数是一个Game对象,它实现ebiten.Gameebiten.RunGame发生错误时返回非零错误值。在这个程序中,当返回一个非 nil 错误时,这个错误被记录为一个致命错误。

2、图像填充

代码

package main

import (
	"image/color"
	"log"

	"github.com/hajimehoshi/ebiten/v2"
)

type Game struct{}

func (g *Game) Update() error {
	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	screen.Fill(color.RGBA{0xff, 0, 0, 0xff})
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
	return 320, 240
}

func main() {
	ebiten.SetWindowSize(640, 480)
	ebiten.SetWindowTitle("Fill")
	if err := ebiten.RunGame(&Game{}); err != nil {
		log.Fatal(err)
	}
}

结果将是这样的:

2021SC@SDUSC Ebiten(一) 基础框架及图像填充代码详解_第2张图片

代码如何工作

	screen.Fill(color.RGBA{0xff, 0, 0, 0xff})

(*ebiten.Image).Fill是一个用指定颜色填充图像的函数。参数colorcolor.Color在标准image/color包中实现接口的值。在此示例中,颜色值为红色(红色和 alpha 部分为0xff.)

你可能感兴趣的:(Ebiten,go,go语言)