-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.nim
514 lines (458 loc) · 16.8 KB
/
main.nim
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import pkg/[raylib]
import std/[strutils, json]
const
WIDTH = 640
HEIGHT = 420
Bg = getColor 0x191919ff
paddingTop = 30.0
padding = 5.0
playableWidth = WIDTH - (padding * 2)
playableHeight = HEIGHT - (padding + paddingTop)
paddleWidth = 100.0
paddleHeight = 20.0
paddleColor = Blue
bulletColor = RayWhite
bulletSize = 10.0
tileMargin = 3.0
tileCols = 10
tileWidth = (playableWidth - (tileMargin * tileCols)) / tileCols
tileHeight = 3 * paddleHeight / 4
steps = 0.2
tileColors = [Red, Orange, Green, Yellow]
continueGameText = "continue previous game"
newGameText = "start a new game"
fontSize = 16
bgImageFileName = "resources/bg.png"
maxSpawns = 4
type
GameStatus = enum
Intro
Paused
Playing
GameOver
Bullet = object
x: float32
y: float32
xVelocity: float32
yVelocity: float32
size = bulletSize
color = bulletColor
Paddle = object
x: float32
y: float32
width = paddleWidth
height = paddleHeight
color = paddleColor
Tile = object
pos: Vector2
points: uint8
GameState = object
score: int
highestScore: int
reSpawns: int
bullet: ref Bullet
paddle: ref Paddle
tiles: ref seq[Tile]
status: GameStatus
bgImage: ref Texture2D
heartImage: ref Texture2D
bgImageTint: Color
isResumeGame: bool
proc loadStateFromFile(fileName: string, gameState: var GameState) =
let gameStateJson = parseJson(readfile(fileName))
gameState.score = gameStateJson["score"].getInt()
gameState.highestScore = gameStateJson["highestScore"].getInt()
gameState.reSpawns = gameStateJson["reSpawns"].getInt()
gameState.status = parseEnum[GameStatus](getStr(gameStateJson["status"]))
gameState.bgImageTint.r = gameStateJson["bgImageTint"]["r"].getint.uint8
gameState.bgImageTint.g = gameStateJson["bgImageTint"]["g"].getint.uint8
gameState.bgImageTint.b = gameStateJson["bgImageTint"]["b"].getint.uint8
gameState.bgImageTint.a = gameStateJson["bgImageTint"]["a"].getint.uint8
gameState.bullet.x = gameStateJson["bullet"]["x"].getFloat()
gameState.bullet.y = gameStateJson["bullet"]["y"].getFloat()
gameState.bullet.xVelocity = gameStateJson["bullet"]["xVelocity"].getFloat()
gameState.bullet.yVelocity = gameStateJson["bullet"]["yVelocity"].getFloat()
gameState.paddle.x = gameStateJson["paddle"]["x"].getFloat()
gameState.paddle.y = gameStateJson["paddle"]["y"].getFloat()
var index: int
for tile in gameStateJson["tiles"].items:
gameState.tiles[index].pos.x = tile["xPos"].getFloat()
gameState.tiles[index].pos.y = tile["yPos"].getFloat()
gameState.tiles[index].points = tile["points"].getInt().uint8
inc index
proc initGame(gameState: var GameState) =
gameState.bullet = new Bullet
gameState.paddle = new Paddle
gameState.tiles = new seq[Tile]
gameState.bgImage = new Texture2D
gameState.heartImage = new Texture2D
gameState.tiles[] = newSeq[Tile](tileCols * tileColors.len)
const tileInitY = paddingTop + tileHeight - (tileColors.len * (tileHeight))
for index, tile in gameState.tiles[]:
let
tx = index mod tileCols
ty = (index div tileCols)
point = uint8(tileColors.len - ty)
gameState.tiles[index].points = point
gameState.tiles[index].pos = Vector2(
x: padding + (tx.float * (tileWidth + tileMargin)),
y: tileInitY + (ty.float * (tileHeight + tileMargin)),
)
gameState.paddle.x = ((padding + playableWidth) / 2) - (paddleWidth / 2)
gameState.paddle.y = (playableHeight - paddleHeight + padding)
gameState.bullet.x = gameState.paddle.x + (paddleWidth / 2)
gameState.bullet.y = gameState.paddle.y
gameState.bullet.xVelocity = 0.09
gameState.bullet.yVelocity = -0.09
gameState.bgImage[] = loadTexture(bgImageFileName)
gameState.heartImage[] = loadTexture("resources/heart.png")
gameState.reSpawns = maxSpawns
gameState.status = Intro
gameState.score = 0
proc drawScoreBoard(gameState: var GameState) =
let
scoreBoardRect = Rectangle(x: 0, y: 0, width: WIDTH, height: paddingTop)
scoreDisplay = "score: " & $gameState.score
drawRectangle(scoreBoardRect, DarkGray)
drawText(
scoreDisplay.cstring, padding.int32, (paddingTop.int32 - 15) div 2, 15, White
)
for i in 0 ..< gameState.reSpawns:
let tPadding =
(padding + gameState.heartImage.width.float) * gameState.reSpawns.float
drawTexture(
gameState.heartImage[],
Vector2(
x:
WIDTH - tPadding +
float((gameState.heartImage.width.float + padding) * i.float),
y: 0,
),
White,
)
proc drawBullet(gameState: GameState) =
drawRectangle(
Rectangle(
x: gameState.bullet.x,
y: gameState.bullet.y,
width: gameState.bullet.size,
height: gameState.bullet.size,
),
gameState.bullet.color,
)
proc handleKeyPresses(gameState: var GameState, sound: Sound) =
case gameState.status
of Playing:
if isKeyDown(Right) or isKeyDown(D):
if gameState.paddle.x <
((playableWidth + padding).float - gameState.paddle.width):
gameState.paddle.x += steps
elif isKeyDown(Left) or isKeyDown(A):
if gameState.paddle.x > padding:
gameState.paddle.x -= steps
elif isKeyPressed(Space):
playSound(sound)
gameState.status = Paused
of Paused:
if isKeyPressed(Space):
playSound(sound)
gameState.status = Playing
of Intro:
if isKeyPressed(Tab):
playSound(sound)
gameState.isResumeGame = not (gameState.isResumeGame)
if isKeyPressed(Enter):
playSound(sound)
if gameState.isResumeGame:
loadStateFromFile("prevGameState.dump.json", gameState)
else:
gameState.status = Playing
of GameOver:
if isKeyPressed(Enter):
playSound(sound)
initGame(gameState)
gameState.status = Playing
proc drawPaddle(gameState: GameState) =
drawRectangle(
Rectangle(
x: gameState.paddle.x,
y: gameState.paddle.y,
width: gameState.paddle.width,
height: gameState.paddle.height,
),
gameState.paddle.color,
)
proc drawTiles(gameState: var GameState) =
for index, tile in gameState.tiles[]:
let tileColor = tileColors[tileColors.len - tile.points]
drawRectangle(
Rectangle(
x: tile.pos.x, y: tile.pos.y, width: tileWidth, height: tileHeight
),
tileColor,
)
if gameState.status == Playing:
gameState.tiles[index].pos.y += 1e-3
proc handlePhysics(
gameState: var GameState, hitPaddleSound, hitTileSound, gameOverSound: Sound
) =
case gameState.status
of Playing:
if gameState.reSpawns <= 0:
gameState.highestScore = max(gameState.score, gameState.highestScore)
playSound(gameOverSound)
gameState.status = GameOver
else:
let
bulletRect = Rectangle(
x: gameState.bullet.x,
y: gameState.bullet.y,
width: gameState.bullet.size,
height: gameState.bullet.size,
)
paddleRect = Rectangle(
x: gameState.paddle.x,
y: gameState.paddle.y,
width: gameState.paddle.width,
height: gameState.paddle.height,
)
for index, tile in gameState.tiles[]:
let tileRect = Rectangle(
x: tile.pos.x, y: tile.pos.y, width: tileWidth, height: tileHeight
)
if bulletRect.checkCollisionRecs(tileRect):
playSound(hitTileSound)
gameState.bullet.yVelocity *= -1.0
inc gameState.score, tile.points
gameState.tiles[index] = Tile(
pos: Vector2(
x: tile.pos.x,
y:
paddingTop - (
(tileHeight + tileMargin) * tileColors.len
) - 50,
),
points: tile.points,
)
if tile.pos.y + tileHeight >= playableHeight:
gameState.highestScore =
max(gameState.score, gameState.highestScore)
playSound(gameOverSound)
gameState.status = GameOver
if bulletRect.checkCollisionRecs(paddleRect):
playSound(hitPaddleSound)
gameState.bullet.yVelocity *= -1.0
gameState.bullet.y = gameState.paddle.y - gameState.bullet.size
if gameState.bullet.x <= padding or
(gameState.bullet.x + gameState.bullet.size) >=
(playableWidth + padding):
gameState.bullet.xVelocity *= -1.0
if gameState.bullet.y <= paddingTop:
gameState.bullet.yVelocity *= -1.0
if (gameState.bullet.y + gameState.bullet.size) >=
(playableHeight + paddingTop):
playSound(gameOverSound)
dec gameState.reSpawns
gameState.bullet.x = gameState.paddle.x + (paddleWidth / 2)
gameState.bullet.y = gameState.paddle.y
gameState.bullet.y += gameState.bullet.yVelocity
gameState.bullet.x += gameState.bullet.xVelocity
else:
discard
proc drawScreen(gameState: var GameState) =
case gameState.status
of Intro:
const gameTitle = "ATARI FALLOUT"
gameState.bgImageTint = Blue
drawText(
gameTitle,
(WIDTH - measureText(gameTitle, 20)) div 2,
2 * padding.int32,
20,
White,
)
let
continueGameTextWidth = continueGameText.measureText(fontSize)
newGameTextWidth = newGameText.measureText(fontSize)
width = max(continueGameTextWidth, newGameTextWidth).float + 50
height = 50.0
marginY = 10.0
buttonRect1 = Rectangle(
x: (WIDTH - width) / 2,
y: ((HEIGHT - height) / 2) - marginY,
width: width,
height: height,
)
buttonRect2 = Rectangle(
x: (WIDTH - width) / 2,
y: ((HEIGHT + height) / 2) + marginY,
width: width,
height: height,
)
if not gameState.isResumeGame:
drawRectangleRounded(buttonRect1, 0.2, 500, Green)
drawText(
newGameText,
int32(buttonRect1.x + buttonRect1.width / 2 - newGameTextWidth / 2),
int32(buttonRect1.y + buttonRect1.height / 2 - fontSize / 2),
fontSize,
White,
)
drawText(
continueGameText,
int32(buttonRect2.x + buttonRect2.width / 2 - continueGameTextWidth / 2),
int32(buttonRect2.y + buttonRect2.height / 2 - fontSize / 2),
fontSize,
Green,
)
drawRectangleRoundedLines(buttonRect2, 0.2, 500, 2.0, White)
else:
drawText(
newGameText,
int32(buttonRect1.x + buttonRect1.width / 2 - newGameTextWidth / 2),
int32(buttonRect1.y + buttonRect1.height / 2 - fontSize / 2),
fontSize,
Green,
)
drawRectangleRoundedLines(buttonRect1, 0.2, 500, 2.0, White)
drawRectangleRounded(buttonRect2, 0.2, 500, Green)
drawText(
continueGameText,
int32(buttonRect2.x + buttonRect2.width / 2 - continueGameTextWidth / 2),
int32(buttonRect2.y + buttonRect2.height / 2 - fontSize / 2),
fontSize,
White,
)
# if gameState.isResumeGame:
of Playing:
gameState.bgImageTint = Green
gameState.drawTiles()
gameState.drawBullet()
gameState.drawPaddle()
gameState.drawScoreBoard()
of Paused:
gameState.bgImageTint = Yellow
const pausedTitle = "Game Paused"
drawText(
pausedTitle,
(WIDTH - measureText(pausedTitle, 20)) div 2,
HEIGHT div 2,
20,
Yellow,
)
gameState.drawTiles()
gameState.drawBullet()
gameState.drawPaddle()
gameState.drawScoreBoard()
of GameOver:
let
currentScore = "Score: " & $gameState.score
highestScore = "Highest score: " & $gameState.highestScore
drawText(
currentScore.cstring,
(WIDTH - measureText(currentScore.cstring, 35)) div 2,
paddingTop.int32,
35,
Green,
)
drawText(
highestScore.cstring,
(WIDTH - measureText(highestScore.cstring, 35)) div 2,
paddingTop.int32 + 50,
35,
Green,
)
gameState.bgImageTint = Red
const pausedTitle = "Game Over"
let
newGameTextWidth = newGameText.measureText(fontSize)
width = newGameTextWidth.float + 50
height = 50.0
marginY = 10.0
buttonRect1 = Rectangle(
x: (WIDTH - width) / 2,
y: HEIGHT - height - marginY,
width: width,
height: height,
)
drawText(
pausedTitle,
(WIDTH - measureText(pausedTitle, 25)) div 2,
HEIGHT div 2,
25,
Red,
)
drawRectangleRounded(buttonRect1, 0.2, 500, Green)
drawText(
newGameText,
int32(buttonRect1.x + buttonRect1.width / 2 - newGameTextWidth / 2),
int32(buttonRect1.y + buttonRect1.height / 2 - fontSize / 2),
fontSize,
White,
)
proc updateGame(
gameState: var GameState,
selectSound, hitPaddleSound, hitTileSound, gameOverSound: Sound,
) =
let bgSrc = Rectangle(
x: 0,
y: 0,
width: gameState.bgImage.width.float,
height: gameState.bgImage.height.float,
)
drawTexture(
gameState.bgImage[],
bgSrc,
Rectangle(x: 0, y: 0, width: WIDTH, height: HEIGHT),
Vector2(x: 0, y: 0),
0,
gameState.bgImageTint,
)
gameState.handleKeyPresses(selectSound)
gameState.drawScreen()
gameState.handlePhysics(hitPaddleSound, hitTileSound, gameOverSound)
proc save(gameState: GameState, storageFile: string) =
var gameStateJson = %*{}
gameStateJson["score"] = %gameState.score
gameStateJson["highestScore"] = %gameState.highestScore
gameStateJson["reSpawns"] = %gameState.reSpawns
gameStateJson["status"] = %gameState.status
gameStateJson["bgImageTint"] = %gameState.bgImageTint
gameStateJson["bullet"] =
%*{
"x": gameState.bullet.x,
"y": gameState.bullet.y,
"xVelocity": gameState.bullet.xVelocity,
"yVelocity": gameState.bullet.yVelocity,
}
gameStateJson["paddle"] = %*{"x": gameState.paddle.x, "y": gameState.paddle.y}
gameStateJson["tiles"] = %*[]
for tile in gameState.tiles[]:
gameStateJson["tiles"].add(
%*{"xPos": tile.pos.x, "yPos": tile.pos.y, "points": tile.points}
)
writeFile(storageFile, $gameStateJson)
proc playGame() =
var gameState: GameState
initAudioDevice()
let
backgroundMusic = loadMusicStream("resources/music.mp3")
selectSound = loadSound("resources/select.ogg")
hitPaddleSound = loadSound("resources/hitPaddle.ogg")
hitTileSound = loadSound("resources/hitTile.ogg")
gameOverSound = loadSound("resources/gameOver.ogg")
initWindow(WIDTH, HEIGHT, "Atari Fallout")
setConfigFlags(flags(Msaa4xHint))
initGame(gameState)
while not (windowShouldClose()):
playMusicStream(backgroundMusic)
drawing:
clearBackground(Bg)
updateGame(
gameState, selectSound, hitPaddleSound, hitTileSound, gameOverSound
)
updateMusicStream(backgroundMusic)
closeAudioDevice()
gameState.save("prevGameState.dump.json")
when isMainModule:
playGame()