-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
301 lines (267 loc) · 7.25 KB
/
main.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"fmt"
_ "image/png"
"math"
"math/rand"
"os"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"golang.org/x/image/colornames"
"golang.org/x/image/font/basicfont"
ss "github.com/zkry/golang-tetris/spritesheet"
)
// BoardRows is the height of the game board in terms of blocks
const BoardRows = 22
// BoardCols is the width of the game board in terms of blocks
const BoardCols = 10
// Point represents a coordinate on the game board with Point{row:0, col:0}
// representing the bottom left
type Point struct {
row int
col int
}
// Board is an array containing the entire game board pieces.
type Board [22][10]Block
// Block represents the color of the block
type Block int
// Different values a point on the grid can hold
const (
Empty Block = iota
Goluboy
Siniy
Pink
Purple
Red
Yellow
Green
Gray
GoluboySpecial
SiniySpecial
PinkSpecial
PurpleSpecial
RedSpecial
YellowSpecial
GreenSpecial
GraySpecial
)
// Piece is a constant for a shape of piece. There are 7 classic pieces like L, and O
type Piece int
// Various values that the pieces can be
const (
IPiece Piece = iota
JPiece
LPiece
OPiece
SPiece
TPiece
ZPiece
)
// Shape is a type containing four points, which represents the four points
// making a contiguous 'piece'.
type Shape [4]Point
const levelLength = 60.0 // Time it takes for game to speed up
const speedUpRate = 0.1 // Every new level, the amount the game speeds up by
var gameBoard Board
var activeShape Shape // The shape that the player controls
var currentPiece Piece
var gravityTimer float64
var baseSpeed float64 = 0.8
var gravitySpeed float64 = 0.8
var levelUpTimer float64 = levelLength
var gameOver bool = false
var leftRightDelay float64
var moveCounter int
var score int
var nextPiece Piece
var blockGen func(int) pixel.Picture
var bgImgSprite pixel.Sprite
var gameBGSprite pixel.Sprite
var nextPieceBGSprite pixel.Sprite
func main() {
pixelgl.Run(run)
}
// run is the main code for the game. Allows pixelgl to run on main thread
func run() {
// Initialize the window
windowWidth := 765.0
windowHeight := 450.0
cfg := pixelgl.WindowConfig{
Title: "Blockfall",
Bounds: pixel.R(0, 0, windowWidth, windowHeight),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
// Load Various Resources:
// Matriax on opengameart.org
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
blockGen, err = ss.LoadSpriteSheet(pwd+"/blocks.png", 2, 8)
if err != nil {
panic(err)
}
// Background image, by ansimuz on opengameart.org
bgPic, err := ss.LoadPicture(pwd + "/parallax-mountain-bg.png")
if err != nil {
panic(err)
}
bgImgSprite = *pixel.NewSprite(bgPic, bgPic.Bounds())
// Game Background
blackPic := ss.GetPlayBGPic()
gameBGSprite = *pixel.NewSprite(blackPic, blackPic.Bounds())
// Next Piece BG
nextPiecePic := ss.GetNextPieceBGPic()
nextPieceBGSprite = *pixel.NewSprite(nextPiecePic, nextPiecePic.Bounds())
nextPiece = Piece(rand.Intn(7))
gameBoard.addPiece() // Add initial Piece to game
last := time.Now()
for !win.Closed() && !gameOver {
// Perform time processing events
dt := time.Since(last).Seconds()
last = time.Now()
gravityTimer += dt
levelUpTimer -= dt
// Time Functions:
// Gravity
if gravityTimer > gravitySpeed {
gravityTimer -= gravitySpeed
didCollide := gameBoard.applyGravity()
if !didCollide {
if gameBoard.isTouchingFloor() {
gravityTimer -= gravitySpeed // Add extra time when touching floor
}
} else {
score += 10
}
}
// Delay for left/right movement. When a key is pressed and holded
// it should first move when pressed, then after a short wait,
// it will continuously move. Like when a key is pressed in a text editor
if leftRightDelay > 0.0 {
leftRightDelay = math.Max(leftRightDelay-dt, 0.0)
}
// Speed up
if levelUpTimer <= 0 {
if baseSpeed > 0.2 {
baseSpeed = math.Max(baseSpeed-speedUpRate, 0.2)
}
levelUpTimer = levelLength
gravitySpeed = baseSpeed
}
// Keypress Functions
if win.Pressed(pixelgl.KeyRight) && leftRightDelay == 0.0 {
gameBoard.movePiece(1)
if moveCounter > 0 {
leftRightDelay = 0.1
} else {
leftRightDelay = 0.5
}
moveCounter++
}
if win.Pressed(pixelgl.KeyLeft) && leftRightDelay == 0.0 {
gameBoard.movePiece(-1)
if moveCounter > 0 {
leftRightDelay = 0.1
} else {
leftRightDelay = 0.5
}
moveCounter++
}
if win.JustPressed(pixelgl.KeyDown) {
gravitySpeed = 0.08 // TODO: Code could result in bugs if game pause functionality added
if gravityTimer > 0.08 {
gravityTimer = 0.08
}
}
if win.JustReleased(pixelgl.KeyDown) {
gravitySpeed = baseSpeed // TODO: Code could result in bugs if game pause functionality added
}
if win.JustPressed(pixelgl.KeyUp) {
gameBoard.rotatePiece()
if gameBoard.isTouchingFloor() {
gravityTimer = 0 // Make gravity more forgiving when moving pieces
}
}
if win.JustPressed(pixelgl.KeySpace) {
gameBoard.instafall()
score += 12
}
if !win.Pressed(pixelgl.KeyRight) && !win.Pressed(pixelgl.KeyLeft) {
moveCounter = 0
leftRightDelay = 0
}
// Display Functions
win.Clear(colornames.Black)
displayBG(win)
displayText(win)
gameBoard.displayBoard(win)
win.Update()
}
}
func displayText(win *pixelgl.Window) {
// Text Generator
scoreTextLocX := 500.0
scoreTextLocY := 400.0
basicAtlas := text.NewAtlas(basicfont.Face7x13, text.ASCII)
scoreTxt := text.New(pixel.V(scoreTextLocX, scoreTextLocY), basicAtlas)
fmt.Fprintf(scoreTxt, "Score: %d", score)
scoreTxt.Draw(win, pixel.IM.Scaled(scoreTxt.Orig, 2))
nextPieceTextLocX := 142.0
nextPieceTextLocY := 285.0
nextPieceTxt := text.New(pixel.V(nextPieceTextLocX, nextPieceTextLocY), basicAtlas)
fmt.Fprintf(nextPieceTxt, "Next Piece:")
nextPieceTxt.Draw(win, pixel.IM)
}
func displayBG(win *pixelgl.Window) {
// Display various background images
bgImgSprite.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
gameBGSprite.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
nextPieceBGSprite.Draw(win, pixel.IM.Moved(pixel.V(182, 225)))
// Display next block
baseShape := getShapeFromPiece(nextPiece)
pic := blockGen(block2spriteIdx(piece2Block(nextPiece)))
sprite := pixel.NewSprite(pic, pic.Bounds())
boardBlockSize := 20.0
scaleFactor := float64(boardBlockSize) / pic.Bounds().Max.Y
shapeWidth := getShapeWidth(baseShape) + 1
shapeHeight := 2
for i := 0; i < 4; i++ {
r := baseShape[i].row
c := baseShape[i].col
x := float64(c)*boardBlockSize + boardBlockSize/2
y := float64(r)*boardBlockSize + boardBlockSize/2
sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, scaleFactor).Moved(pixel.V(x+182-(float64(shapeWidth)*10), y+225-(float64(shapeHeight)*10))))
}
}
// block2spriteIdx associates a blocks color (b Block) with its index in the sprite sheet.
func block2spriteIdx(b Block) int {
return int(b) - 1
}
// piece2Block associates a pieces shape (Piece) with it's color/image (Block).
func piece2Block(p Piece) Block {
switch p {
case LPiece:
return Goluboy
case IPiece:
return Siniy
case OPiece:
return Pink
case TPiece:
return Purple
case SPiece:
return Red
case ZPiece:
return Yellow
case JPiece:
return Green
}
panic("piece2Block: Invalid piece passed in")
return GraySpecial // Return strange value value
}