Ebiten implements small cube movement (summer vacation game development)

ebiten cube move


video demo


idea

  • 1. Define a block structure
  • 2. Draw a square
  • 3. Write the logic code for the block movement

1. Define a block structure

  • A square has a starting point (x, y) and width, height, because it needs to move, so the movement speed is indispensable
type Block struct{
    
    
    x float64  //方块绘制横坐标起点
    y float64  //方块绘制纵坐标起点
    width float64 //方块的宽度
    height float64 //方块的高度
    speed float64 //方块的行走速度
}
//将方块结构体假如Game结构体当中,方便调用
type Game struct{
    
    
    block *Block
}

2. Draw a square

  • ebitenuitldraw squares using the built-in function
//绘制方块函数
func (block *Block)BlockDraw(screen *ebiten.Image){
    
    
    //调用ebitenutil中的DrawRect函数
    //color.NRGBA{R:0,G:255,B:0,A:255}表示颜色
    ebitenutil.DrawRect(screen,block.x,block.y,block.width,block.height,color.NRGBA{
    
    R:0,G:255,B:0,A:255})
}

3. Block movement logic code

  • To move up, down, left, and right, when the arrow keys are pressed, the block can be moved by modifying the coordinates of the block
func (block *Block)BlockUpdate(){
    
    
    //逻辑实现实现上下左右,斜角
    //一定要分开写,可以直接实现斜方向
    //按键检测(该控制的是键盘上的箭头键)
    if ebiten.IsKeyPressed(ebiten.KeyLeft) {
    
    
        block.x-=block.speed
    } else if ebiten.IsKeyPressed(ebiten.KeyRight) {
    
    
        block.x+=block.speed
    } else {
    
    
        block.x+=0
    }
    if ebiten.IsKeyPressed(ebiten.KeyUp) {
    
    
        //由于屏幕左边左上角是(0,0)
        //所以往上走应该是减少的
        block.y-=block.speed
    } else if ebiten.IsKeyPressed(ebiten.KeyDown) {
    
    
        block.y+=block.speed
    } else {
    
    
        block.x+=0
    }
}

Initialize data (the data in the Game structure needs to be initialized before calling)

  • Initialize the defined data
func NewGame()*Game{
    
    
    return &Game{
    
    
        &Block{
    
    100, 100, 5, 5, 1},    
    }
}

full code

type Block struct {
    
    
	x      float64 //方块绘制横坐标起点
	y      float64 //方块绘制纵坐标起点
	width  float64 //方块的宽度
	height float64 //方块的高度
	speed  float64 //方块的行走速度
}

// Game 将方块结构体假如Game结构体当中,方便调用
type Game struct {
    
    
	block *Block
}

// BlockDraw 绘制方块函数
func (block *Block) BlockDraw(screen *ebiten.Image) {
    
    
	//调用ebitenutil中的DrawRect函数
	//color.NRGBA{R:0,G:255,B:0,A:255}表示颜色
	ebitenutil.DrawRect(screen, block.x, block.y, block.width, block.height, color.NRGBA{
    
    R: 0, G: 255, B: 0, A: 255})
}
func (block *Block) BlockUpdate() {
    
    
	//逻辑实现实现上下左右,斜角
	//一定要分开写,可以直接实现斜方向
	//按键检测(该控制的是键盘上的箭头键)
	if ebiten.IsKeyPressed(ebiten.KeyLeft) {
    
    
		block.x -= block.speed
	} else if ebiten.IsKeyPressed(ebiten.KeyRight) {
    
    
		block.x += block.speed
	} else {
    
    
		block.x += 0
	}
	if ebiten.IsKeyPressed(ebiten.KeyUp) {
    
    
		block.y -= block.speed
	} else if ebiten.IsKeyPressed(ebiten.KeyDown) {
    
    
		block.y += block.speed
	} else {
    
    
		block.x += 0
	}
}

func (g *Game) Update() error {
    
    
	g.block.BlockUpdate()
	return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
    
    
	g.block.BlockDraw(screen)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
    
    
	//如果和设置窗口的数据一致,则不缩放
	//如果各个数据比设置窗口时小,则游戏内容放大
	//如果各个数据比设置窗口时大,则游戏内容缩小
	//一般设置为窗口数据的一半
	return 480, 320
}

func NewGame() *Game {
    
    
	return &Game{
    
    
		&Block{
    
    100, 100, 5, 5, 1},
	}
}
func main() {
    
    
	ebiten.SetWindowTitle("边界限制")  //窗口标题
	ebiten.SetWindowSize(960, 640) //窗口大小
	game := NewGame()
	if err := ebiten.RunGame(game); err != nil {
    
    
		log.Fatal("启动失败:", err)
	}
}

Guess you like

Origin blog.csdn.net/JUIU9527/article/details/131262967
Recommended