-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
223 lines (197 loc) · 5.68 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
package main
import (
"fmt"
"time"
"github.com/benjmarshall/gopixelsnake/drawing"
"github.com/benjmarshall/gopixelsnake/game"
"github.com/benjmarshall/gopixelsnake/gametext"
"github.com/benjmarshall/gopixelsnake/scores"
"github.com/benjmarshall/gopixelsnake/snake"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
)
func main() {
pixelgl.Run(run)
}
func run() {
// Setup Window Configuration
cfg := pixelgl.WindowConfig{
Title: "Pixel Rocks!",
Bounds: pixel.R(0, 0, 1024, 768),
Resizable: false,
VSync: true,
}
// Create the window
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
// Setup Game Configuration
gameCFG := game.NewGameConfig(700, 700, 2, 10, cfg)
// Setup text structure
textStruct := gametext.NewGameText(win, gameCFG)
// Setup a scores structure
scoresTable := scores.NewScores("high_scores.csv", 10)
// Initialize a new snake
s := snake.NewSnake(gameCFG)
// Generate a berry
berry := game.GenerateRandomBerry(&gameCFG)
// Create the Game Background Shape
imdArea := imdraw.New(nil)
// Create the Game Contents Shape
imdGame := imdraw.New(nil)
// Create a berry Contents Shape
imdBerry := imdraw.New(nil)
// Create some variables
var (
frames = 0
second = time.Tick(time.Second)
gameRunning = false
gameOver = false
eaten = false
inputKeyBuffer = []snake.Direction{}
dir snake.Direction
score = 0
showScores = false
scoreName string
highScore = false
)
// Draw the initial frames
win.Clear(colornames.Darkcyan)
drawing.DrawGameBackground(win, imdArea, &gameCFG)
drawing.DrawSnakeRect(win, imdGame, &gameCFG, &s)
drawing.DrawBerry(win, imdBerry, &gameCFG, berry)
textStruct.DrawTitleText(win)
textStruct.DrawScoreText(win, score)
textStruct.DrawControlsText(win)
win.Update()
// Keep going till the window is closed
for !win.Closed() {
// Clear the screen
win.Clear(colornames.Darkcyan)
if !gameRunning && !gameOver && !showScores {
// Game is not running so wait for user to do something!
if win.JustPressed(pixelgl.KeyUp) {
s.StartOfGame(snake.UP)
gameRunning = true
} else if win.JustPressed(pixelgl.KeyDown) {
s.StartOfGame(snake.DOWN)
gameRunning = true
} else if win.JustPressed(pixelgl.KeyLeft) {
s.StartOfGame(snake.LEFT)
gameRunning = true
} else if win.JustPressed(pixelgl.KeyRight) {
s.StartOfGame(snake.RIGHT)
gameRunning = true
} else if win.JustPressed(pixelgl.KeyX) {
win.SetClosed(true)
}
if win.JustPressed(pixelgl.KeyS) {
showScores = true
}
} else if !gameRunning && !gameOver && showScores {
if win.JustPressed(pixelgl.KeyS) {
showScores = false
} else if win.JustPressed(pixelgl.KeyX) {
win.SetClosed(true)
}
}
// Do game logic only if the game is actually running!
if gameRunning {
// Catch user input
if win.JustPressed(pixelgl.KeyUp) {
inputKeyBuffer = append(inputKeyBuffer, snake.UP)
} else if win.JustPressed(pixelgl.KeyDown) {
inputKeyBuffer = append(inputKeyBuffer, snake.DOWN)
} else if win.JustPressed(pixelgl.KeyLeft) {
inputKeyBuffer = append(inputKeyBuffer, snake.LEFT)
} else if win.JustPressed(pixelgl.KeyRight) {
inputKeyBuffer = append(inputKeyBuffer, snake.RIGHT)
}
// Update the snake
select {
case <-s.GetTicker():
// Update the snake
if len(inputKeyBuffer) == 0 {
dir = snake.NOCHANGE
} else {
dir = inputKeyBuffer[0]
inputKeyBuffer = inputKeyBuffer[1:len(inputKeyBuffer)]
}
s.Update(eaten, dir)
// Check the snake is still in bounds
if !s.CheckSnakeOK(&gameCFG) {
gameOver = true
gameRunning = false
if score >= scoresTable.GetBottomScore() {
highScore = true
}
break
}
// Check if the snake has eaten
eaten = s.CheckIfSnakeHasEaten(&gameCFG, berry)
if eaten {
berry = game.GenerateRandomBerry(&gameCFG)
s.IncreaseSpeed()
}
// Update the score
score += int((s.GetSpeed() * 10))
if eaten {
score += int((1000 * s.GetSpeed()))
}
default:
}
} else if gameOver {
// Game has ended, wait for user to continue
if win.JustPressed(pixelgl.KeyEnter) {
// Submit score and reset for a new game
if highScore {
scoresTable.AddScore(score, scoreName)
}
// reset the board
scoreName = ""
gameOver = false
highScore = false
score = 0
berry = game.GenerateRandomBerry(&gameCFG)
s = snake.NewSnake(gameCFG)
} else if win.JustPressed(pixelgl.KeyBackspace) {
// Add support for deleting charaters from score name
scoreName = scoreName[0 : len(scoreName)-1]
} else if len(scoreName) < 3 {
// Capture input for score name (up to 3 chars)
scoreName = scoreName + win.Typed()
}
}
// Always draw the game
drawing.DrawGameBackground(win, imdArea, &gameCFG)
if !showScores {
// Hide game elements if high scores are being diplayed
drawing.DrawSnakeRect(win, imdGame, &gameCFG, &s)
drawing.DrawBerry(win, imdBerry, &gameCFG, berry)
}
textStruct.DrawTitleText(win)
textStruct.DrawScoreText(win, score)
textStruct.DrawControlsText(win)
if !gameRunning && !gameOver && !showScores {
// Show the start game message
textStruct.DrawStartGameText(win)
} else if gameOver {
textStruct.DrawGameOverText(win, &gameCFG, scoreName, highScore)
} else if showScores {
textStruct.DrawScoresListText(win, &gameCFG, &scoresTable)
}
// Always update the window
win.Update()
frames++
// Update FPS
select {
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
frames = 0
default:
}
}
}