/
game.go
200 lines (181 loc) · 5.22 KB
/
game.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"fmt"
"mygameengine"
"mygameengine/image"
)
const IMG_BLOCK_01 string = "assets/images/block-01.png"
const IMG_BLOCK_02 string = "assets/images/block-02.png"
const IMG_BLOCK_03 string = "assets/images/block-03.png"
const IMG_BLOCK_04 string = "assets/images/block-04.png"
const IMG_BLOCK_05 string = "assets/images/block-05.png"
const IMG_BLOCK_06 string = "assets/images/block-06.png"
const BLOCK_SIZE uint = 27
const KEY_RIGHT int = 57
const KEY_LEFT int = 58
const KEY_UP int = 60
const KEY_DOWN int = 59
const KEY_CTRL_RIGHT int = 104
const KEY_CTRL_LEFT int = 100
const KEY_SPACE int = 1
func keyHandler(world *World) func(int) {
return func(key int) {
switch key {
case KEY_RIGHT:
fmt.Println("move right")
world.Right()
case KEY_LEFT:
fmt.Println("move left")
world.Left()
case KEY_DOWN:
fmt.Println("move down")
world.Down()
case KEY_SPACE:
fmt.Println("drop")
world.Drop()
case KEY_CTRL_RIGHT, KEY_UP:
fmt.Println("rotate right")
world.RotateRight()
case KEY_CTRL_LEFT:
world.RotateLeft()
}
fmt.Println("game: KEY DOWN", key)
}
}
func drawGameBackground(screen *image.Image) {
screen.DrawRectangle(0, 0, 640, 480, mygameengine.COLOR_BLACK)
}
func drawGameGrid(screen *image.Image, world *World, engine *mygameengine.MyGameEngine) {
gridWidth := world.GetGridWidth()
gridHeight := world.GetGridHeight()
blocks := world.GetGrid()
for y := uint(0); y < gridHeight; y++ {
for x := uint(0); x < gridWidth; x++ {
if blocks[y][x] != nil {
image := blockToImage(blocks[y][x], engine)
screen.BlitAt(image, int(BLOCK_SIZE+x*BLOCK_SIZE), int(y*BLOCK_SIZE))
}
}
}
}
func drawGameGridBorders(screen *image.Image, world *World, engine *mygameengine.MyGameEngine) {
gridWidth := world.GetGridWidth()
gridHeight := world.GetGridHeight()
for y := uint(0); y < gridHeight; y++ {
screen.BlitAt(engine.Assets().Get(IMG_BLOCK_05), 0, int(y*BLOCK_SIZE))
screen.BlitAt(engine.Assets().Get(IMG_BLOCK_06), int(gridWidth*BLOCK_SIZE+BLOCK_SIZE), int(y*BLOCK_SIZE))
}
for x := uint(0); x < gridWidth+2; x++ {
screen.BlitAt(engine.Assets().Get(IMG_BLOCK_06), int(x*BLOCK_SIZE), int(gridHeight*BLOCK_SIZE))
}
}
func drawGameCurrentPiece(screen *image.Image, world *World, engine *mygameengine.MyGameEngine) {
piece := world.GetPiece()
pieceBlocks := piece.GetBlocks()
pieceSize := int(piece.GetSize())
pieceY := world.GetPieceY()
pieceX := world.GetPieceX()
for y := 0; y < pieceSize; y++ {
for x := 0; x < pieceSize; x++ {
if pieceBlocks[y][x] != nil {
image := blockToImage(pieceBlocks[y][x], engine)
screen.BlitAt(
image,
int(BLOCK_SIZE+uint(pieceX+x)*BLOCK_SIZE),
int(uint(pieceY+y)*BLOCK_SIZE),
)
}
}
}
}
func drawGameNextPiece(screen *image.Image, world *World, engine *mygameengine.MyGameEngine) {
piece := world.GetNextPiece()
pieceBlocks := piece.GetBlocks()
pieceSize := piece.GetSize()
marginLeft := uint(15)
marginTop := uint(8)
width := uint(6)
// border
for y := uint(0); y < width; y++ {
for x := uint(0); x < width; x++ {
if x == 0 || y == 0 || x == 5 || y == 5 {
screen.BlitAt(
engine.Assets().Get(IMG_BLOCK_05),
int(BLOCK_SIZE*marginLeft+uint(x)*BLOCK_SIZE),
int(BLOCK_SIZE*marginTop+uint(y)*BLOCK_SIZE),
)
}
}
}
// piece
for y := uint(0); y < pieceSize; y++ {
for x := uint(0); x < pieceSize; x++ {
if pieceBlocks[y][x] != nil {
image := blockToImage(pieceBlocks[y][x], engine)
screen.BlitAt(
image,
int(BLOCK_SIZE*(marginLeft+1)+uint(x)*BLOCK_SIZE),
int(BLOCK_SIZE*(marginTop+1)+uint(y)*BLOCK_SIZE),
)
}
}
}
}
func drawGameFlash(screen *image.Image, flash int) {
white := mygameengine.COLOR_WHITE
white.A = uint8(25 * flash)
screen.DrawRectangle(0, 0, 640, 480, white)
}
func repaintHandler(engine *mygameengine.MyGameEngine, world *World) func(*image.Image) {
// flash effect setup
flash := 0
world.OnDeleted(func(lines uint) {
flash = 10
})
return func(screen *image.Image) {
drawGameBackground(screen)
drawGameGrid(screen, world, engine)
drawGameGridBorders(screen, world, engine)
drawGameCurrentPiece(screen, world, engine)
drawGameNextPiece(screen, world, engine)
// flash effect on line deletion
if flash > 0 {
drawGameFlash(screen, flash)
flash--
}
}
}
func loadAssets(engine *mygameengine.MyGameEngine) {
engine.Assets().Png(IMG_BLOCK_01)
engine.Assets().Png(IMG_BLOCK_02)
engine.Assets().Png(IMG_BLOCK_03)
engine.Assets().Png(IMG_BLOCK_04)
engine.Assets().Png(IMG_BLOCK_05)
engine.Assets().Png(IMG_BLOCK_06)
}
func blockToImage(block *Block, engine *mygameengine.MyGameEngine) *image.Image {
switch block.GetShape() {
case 1:
return engine.Assets().Get(IMG_BLOCK_01)
case 2:
return engine.Assets().Get(IMG_BLOCK_02)
case 3:
return engine.Assets().Get(IMG_BLOCK_03)
case 4:
return engine.Assets().Get(IMG_BLOCK_04)
}
return nil
}
func Game(engine *mygameengine.MyGameEngine) *mygameengine.Board {
loadAssets(engine)
gameBoard := mygameengine.NewBoard()
world := NewWorld()
world.OnGameOver(func() {
world.Stop()
engine.Boards().SetCurrent(engine.Boards().Get("intro"))
})
gameBoard.OnStart(world.Start)
gameBoard.OnKeyDown(keyHandler(world))
gameBoard.OnRepaint(repaintHandler(engine, world))
return gameBoard
}