|
| 1 | +# golang-sample-with-snake-game |
| 2 | + |
| 3 | +This repository is learn how to use ebiten to build a snake game |
| 4 | + |
| 5 | +## game logic |
| 6 | + |
| 7 | +```golang |
| 8 | +package internal |
| 9 | + |
| 10 | +import ( |
| 11 | + "image/color" |
| 12 | + "math/rand" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/hajimehoshi/ebiten/v2" |
| 16 | + "github.com/hajimehoshi/ebiten/v2/text/v2" |
| 17 | + "github.com/hajimehoshi/ebiten/v2/vector" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + dirUp = Point{x: 0, y: -1} |
| 22 | + dirDown = Point{x: 0, y: 1} |
| 23 | + dirLeft = Point{x: -1, y: 0} |
| 24 | + dirRight = Point{x: 1, y: 0} |
| 25 | + MplusFaceSource *text.GoTextFaceSource |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + gameSpeed = time.Second / 6 |
| 30 | + ScreenWidth = 640 |
| 31 | + ScreenHeight = 480 |
| 32 | + gridSize = 20 |
| 33 | +) |
| 34 | + |
| 35 | +type Point struct { |
| 36 | + x, y int |
| 37 | +} |
| 38 | + |
| 39 | +type Game struct { |
| 40 | + snake []Point |
| 41 | + direction Point |
| 42 | + lastUpdate time.Time |
| 43 | + food Point |
| 44 | + randGenerator *rand.Rand |
| 45 | + gameOver bool |
| 46 | +} |
| 47 | + |
| 48 | +func (g *Game) Update() error { |
| 49 | + if g.gameOver { |
| 50 | + return nil |
| 51 | + } |
| 52 | + // handle key |
| 53 | + if ebiten.IsKeyPressed(ebiten.KeyW) { |
| 54 | + g.direction = dirUp |
| 55 | + } else if ebiten.IsKeyPressed(ebiten.KeyS) { |
| 56 | + g.direction = dirDown |
| 57 | + } else if ebiten.IsKeyPressed(ebiten.KeyA) { |
| 58 | + g.direction = dirLeft |
| 59 | + } else if ebiten.IsKeyPressed(ebiten.KeyD) { |
| 60 | + g.direction = dirRight |
| 61 | + } |
| 62 | + // slow down |
| 63 | + if time.Since(g.lastUpdate) < gameSpeed { |
| 64 | + return nil |
| 65 | + } |
| 66 | + g.lastUpdate = time.Now() |
| 67 | + |
| 68 | + g.updateSnake(&g.snake, g.direction) |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +// isBadCollision - check if snake is collision |
| 73 | +func (g Game) isBadCollision( |
| 74 | + newHead Point, |
| 75 | + snake []Point, |
| 76 | +) bool { |
| 77 | + // check if out of bound |
| 78 | + if newHead.x < 0 || newHead.y < 0 || |
| 79 | + newHead.x >= ScreenWidth/gridSize || newHead.y >= ScreenHeight/gridSize { |
| 80 | + return true |
| 81 | + } |
| 82 | + // is newhead collision |
| 83 | + for _, snakeBody := range snake { |
| 84 | + if snakeBody == newHead { |
| 85 | + return true |
| 86 | + } |
| 87 | + } |
| 88 | + return false |
| 89 | +} |
| 90 | + |
| 91 | +// updateSnake - update snake with direction |
| 92 | +func (g *Game) updateSnake(snake *[]Point, direction Point) { |
| 93 | + head := (*snake)[0] |
| 94 | + |
| 95 | + newHead := Point{ |
| 96 | + x: head.x + direction.x, |
| 97 | + y: head.y + direction.y, |
| 98 | + } |
| 99 | + |
| 100 | + // check collision for snake |
| 101 | + if g.isBadCollision(newHead, *snake) { |
| 102 | + g.gameOver = true |
| 103 | + return |
| 104 | + } |
| 105 | + // check collision with food |
| 106 | + if newHead == g.food { |
| 107 | + *snake = append( |
| 108 | + []Point{newHead}, |
| 109 | + *snake..., |
| 110 | + ) |
| 111 | + g.SpawnFood() |
| 112 | + } else { |
| 113 | + *snake = append( |
| 114 | + []Point{newHead}, |
| 115 | + (*snake)[:len(*snake)-1]..., |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +// drawGameOverText - draw game over text on screen |
| 122 | +func (g *Game) drawGameOverText(screen *ebiten.Image) { |
| 123 | + face := &text.GoTextFace{ |
| 124 | + Source: MplusFaceSource, |
| 125 | + Size: 48, |
| 126 | + } |
| 127 | + title := "Game Over!" |
| 128 | + w, h := text.Measure(title, |
| 129 | + face, |
| 130 | + face.Size, |
| 131 | + ) |
| 132 | + op := &text.DrawOptions{} |
| 133 | + op.GeoM.Translate(ScreenWidth/2-w/2, ScreenHeight/2-h/2) |
| 134 | + op.ColorScale.ScaleWithColor(color.White) |
| 135 | + text.Draw( |
| 136 | + screen, |
| 137 | + title, |
| 138 | + face, |
| 139 | + op, |
| 140 | + ) |
| 141 | +} |
| 142 | + |
| 143 | +// Draw - handle screen update |
| 144 | +func (g *Game) Draw(screen *ebiten.Image) { |
| 145 | + for _, p := range g.snake { |
| 146 | + vector.DrawFilledRect( |
| 147 | + screen, |
| 148 | + float32(p.x*gridSize), |
| 149 | + float32(p.y*gridSize), |
| 150 | + gridSize, |
| 151 | + gridSize, |
| 152 | + color.White, |
| 153 | + true, |
| 154 | + ) |
| 155 | + } |
| 156 | + vector.DrawFilledRect( |
| 157 | + screen, |
| 158 | + float32(g.food.x*gridSize), |
| 159 | + float32(g.food.y*gridSize), |
| 160 | + gridSize, |
| 161 | + gridSize, |
| 162 | + color.RGBA{255, 0, 0, 255}, |
| 163 | + true, |
| 164 | + ) |
| 165 | + if g.gameOver { |
| 166 | + g.drawGameOverText(screen) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { |
| 171 | + return ScreenWidth, ScreenHeight |
| 172 | +} |
| 173 | + |
| 174 | +// SpawnFood - generate new food |
| 175 | +func (g *Game) SpawnFood() { |
| 176 | + g.food = Point{ |
| 177 | + x: g.randGenerator.Intn(ScreenWidth / gridSize), |
| 178 | + y: g.randGenerator.Intn(ScreenHeight / gridSize), |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +// NewGame - create Game |
| 183 | +func NewGame() *Game { |
| 184 | + return &Game{ |
| 185 | + snake: []Point{{ |
| 186 | + x: ScreenWidth / gridSize / 2, |
| 187 | + y: ScreenHeight / gridSize / 2, |
| 188 | + }}, |
| 189 | + direction: Point{x: 1, y: 0}, |
| 190 | + randGenerator: rand.New(rand.NewSource(time.Now().UnixNano())), |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +## game screen |
| 196 | + |
| 197 | + |
0 commit comments