-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
262 lines (228 loc) · 5.6 KB
/
main.go
File metadata and controls
262 lines (228 loc) · 5.6 KB
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
// w4 bundle build/cart.wasm --title "Snake" --html release/snake.html --windows release/snake.exe --mac release/snake-mac --linux release/snake-linux
package main
import (
"cart/difficulty"
"cart/state"
"cart/w4"
"math/rand"
"strconv"
)
const winningScore = 100
type Direction uint8
const (
Up Direction = iota
Down
Left
Right
)
var (
snake = &Snake{}
frameCounter = 0
timeoutCounter = 0 // Used for menu animations
prevState uint8
prevDir = Right
fruit = Point{X: 10, Y: 10}
fruitSprite = [16]byte{0x00, 0xa0, 0x02, 0x00, 0x0e, 0xf0, 0x36, 0x5c, 0xd6, 0x57, 0xd5, 0x57, 0x35, 0x5c, 0x0f, 0xf0}
rnd func(int) int
speed = 15 // 12, 10, 6
score = 0
difficultyLevel = difficulty.Easy
gameState = state.Start
inputBuffer = make([]Direction, 0, 60)
bip = 0 // countdown for music
)
//go:export start
func start() {
w4.PALETTE[0] = 0xfbf7f3
w4.PALETTE[1] = 0xe5b083
w4.PALETTE[2] = 0x426e5d
w4.PALETTE[3] = 0x20283d
snake.Reset()
}
//go:export update
func update() {
frameCounter++
switch gameState {
case state.Win:
winScreen()
case state.Start:
startScreen()
case state.GameOver:
gameOver()
case state.Playing:
playing()
}
}
func winScreen() {
timeoutCounter += 1
w4.Text("Wow. I am impressed", 0, 70)
if bip == 0 {
w4.Tone(34+12, 250|(1<<24)|(4<<16)|(60<<8), 50, w4.TONE_NOTE_MODE|w4.TONE_PULSE2|w4.TONE_MODE3)
bip += 1
}
if timeoutCounter > 60 {
w4.Text("Well done", 0, 90)
if bip == 1 {
w4.Tone(34+7+12, 250|(2<<24)|(4<<16)|(60<<8), 50, w4.TONE_NOTE_MODE|w4.TONE_PULSE1|w4.TONE_MODE2)
bip += 1
}
}
if timeoutCounter > 101 {
w4.Text("Take a break :)", 0, 110)
if bip == 2 {
w4.Tone(34+12+12, 255|(1<<24)|(4<<16)|(1<<8), 100, w4.TONE_NOTE_MODE|w4.TONE_TRIANGLE|w4.TONE_MODE2)
bip += 1
}
}
if timeoutCounter > 215 {
if bip == 3 {
w4.Tone(34+12+12+12, 170|(33<<24)|(4<<16)|(10<<8), 30, w4.TONE_NOTE_MODE|w4.TONE_PULSE1|w4.TONE_MODE1)
bip += 1
}
}
}
func startScreen() {
w4.Text("SNAKE", 30, 30)
if bip == 0 {
w4.Tone(490|(720<<16), 126|(134<<24)|(160<<16)|(100<<8), 19|(48<<8), w4.TONE_NOISE|w4.TONE_MODE4)
bip += 1
}
timeoutCounter += 1
if timeoutCounter >= 90 {
w4.Text("Made by me", 20, 90)
w4.Text("github.com", 0, 110)
w4.Text("/", 75, 120)
w4.Text("0riginaln0", 80, 130)
startGameOnInput()
}
}
func gameOver() {
w4.Text("GAME OVER", 30, 70)
if bip == 0 {
w4.Tone(34, 120|(1<<24)|(15<<16)|(60<<8), 50, w4.TONE_NOTE_MODE|w4.TONE_PULSE2|w4.TONE_MODE2)
bip += 1
}
timeoutCounter += 1
if timeoutCounter >= 60 {
if bip == 1 {
w4.Tone(41, 230|(60<<8), 50, w4.TONE_NOTE_MODE|w4.TONE_PULSE1|w4.TONE_MODE4)
bip += 2
}
w4.Text("good luck next time", 5, 90)
}
if timeoutCounter >= 100 {
if bip == 3 {
w4.Tone(30, 255|(1<<24)|(15<<16)|(60<<8), 50, w4.TONE_NOTE_MODE|w4.TONE_PULSE2|w4.TONE_MODE4)
bip += 1
}
w4.Text(":)", 75, 120)
}
if timeoutCounter >= 160 {
startGameOnInput()
}
}
func startGameOnInput() {
justPressed := *w4.GAMEPAD1 & (*w4.GAMEPAD1 ^ prevState)
anyButtonPressed := justPressed&w4.BUTTON_UP != 0 || justPressed&w4.BUTTON_DOWN != 0 ||
justPressed&w4.BUTTON_LEFT != 0 || justPressed&w4.BUTTON_RIGHT != 0
if anyButtonPressed && (gameState == state.Start || gameState == state.GameOver) {
timeoutCounter = 0
gameState = state.Playing
bip = 0
}
prevState = *w4.GAMEPAD1
}
func playing() {
takeInput()
w4.Text(
"Score:"+
strconv.FormatInt(int64(score), 10)+
"/"+
strconv.FormatInt(int64(winningScore), 10),
4, 4)
w4.Text(difficultyLevel.String(), 92, 148)
if frameCounter%speed == 0 {
if len(inputBuffer) != 0 {
switch inputBuffer[0] {
case Up:
snake.Up()
case Down:
snake.Down()
case Left:
snake.Left()
case Right:
snake.Right()
}
inputBuffer = inputBuffer[1:]
}
snake.Update()
if snake.IsDead() {
difficultyLevel.Reset()
snake.Reset()
speed = 15
score = 0
frameCounter = 0
gameState = state.GameOver
bip = 0
}
if snake.Body[0] == fruit {
w4.Tone(490|(720<<16), 10|(8<<24)|(12<<8), 100, w4.TONE_TRIANGLE)
//w4.Trace("am nyam")
score += 1
snake.Body = append(snake.Body, snake.Body[len(snake.Body)-1])
generateNewApple()
switch score {
case 20:
speed = 12
difficultyLevel = difficulty.StillEz
case 40:
speed = 10
difficultyLevel = difficulty.Medium
case 60:
speed = 6
difficultyLevel = difficulty.Hard
case winningScore:
gameState = state.Win
}
}
}
snake.Draw()
*w4.DRAW_COLORS = 0x4320
w4.Blit(&fruitSprite[0], fruit.X*8, fruit.Y*8, 8, 8, w4.BLIT_2BPP)
}
func takeInput() {
justPressed := *w4.GAMEPAD1 & (*w4.GAMEPAD1 ^ prevState)
if *w4.GAMEPAD1 != 0 && rnd == nil {
rnd = rand.New(rand.NewSource(int64(frameCounter))).Intn
}
if justPressed&w4.BUTTON_UP != 0 && prevDir != Down && prevDir != Up {
inputBuffer = append(inputBuffer, Up)
prevDir = Up
} else if justPressed&w4.BUTTON_DOWN != 0 && prevDir != Down && prevDir != Up {
inputBuffer = append(inputBuffer, Down)
prevDir = Down
} else if justPressed&w4.BUTTON_LEFT != 0 && prevDir != Left && prevDir != Right {
inputBuffer = append(inputBuffer, Left)
prevDir = Left
} else if justPressed&w4.BUTTON_RIGHT != 0 && prevDir != Left && prevDir != Right {
inputBuffer = append(inputBuffer, Right)
prevDir = Right
}
prevState = *w4.GAMEPAD1
}
func generateNewApple() {
foundNewPlace := false
p := Point{}
for !foundNewPlace {
foundNewPlace = true
p.X = rnd(20)
p.Y = rnd(20)
for _, body_part := range snake.Body {
if body_part == p {
foundNewPlace = false
break
}
}
}
fruit = p
}