; Star Catcher - a complete game ; Catch the falling stars, dodge the rocks, and grab a heart for an extra life. ; Arrows move. Space starts. Escape quits. Graphics 640, 480 SetBuffer BackBuffer() SeedRnd MilliSecs() AppTitle "Star Catcher" ; ---- the things that fall, and the sparks they make ---- Const STAR = 1, ROCK = 2, HEART = 3 Type Faller Field x#, y#, speed# Field kind End Type Type Spark Field x#, y#, dx#, dy# Field life Field r, g, b End Type ; ---- the background: dim stars drifting past ---- Dim skyX(59), skyY#(59), skySpeed#(59) For i = 0 To 59 skyX(i) = Rand(0, 639) skyY(i) = Rand(0, 479) skySpeed(i) = Rnd(0.3, 1.2) Next ; ---- the state of the game ---- Const TITLE = 0, PLAYING = 1, GAMEOVER = 2 Global state = TITLE Global catcherX#, score, lives, level, best Global nextDrop ; frames until something else falls Global flash ; frames left of the red flash when a rock hits ; ---- the main loop ---- While Not KeyHit(1) Select state Case TITLE If KeyHit(57) Then NewGame() Case PLAYING UpdateGame() Case GAMEOVER If KeyHit(57) Then NewGame() End Select MoveSky() MoveSparks() DrawGame() Flip Wend End ; Clears everything away and starts a new game. Function NewGame() For f.Faller = Each Faller Delete f Next catcherX = 280 score = 0 lives = 3 level = 1 nextDrop = 30 state = PLAYING End Function ; One frame of play: move, drop things, check for catches. Function UpdateGame() ; move the catcher, but not off the screen If KeyDown(203) Then catcherX = catcherX - 8 If KeyDown(205) Then catcherX = catcherX + 8 If catcherX < 0 Then catcherX = 0 If catcherX > 560 Then catcherX = 560 ; every so often, something new falls: more often, and more rocks, on higher levels nextDrop = nextDrop - 1 If nextDrop <= 0 f.Faller = New Faller f\x = Rand(15, 625) f\y = -20 f\speed = Rnd(2, 3) + level * 0.5 chance = Rand(1, 100) If chance <= 4 Then f\kind = HEART ElseIf chance <= 30 + level * 3 Then f\kind = ROCK Else f\kind = STAR EndIf nextDrop = Rand(20, 45) - level * 3 If nextDrop < 8 Then nextDrop = 8 EndIf ; move everything that is falling, and see what the catcher catches For f.Faller = Each Faller f\y = f\y + f\speed If RectsOverlap(catcherX, 426, 80, 26, f\x - 12, f\y - 12, 24, 24) Select f\kind Case STAR score = score + 10 Burst(f\x, f\y, 255, 220, 60) Case ROCK lives = lives - 1 flash = 8 Burst(f\x, f\y, 160, 150, 145) Case HEART If lives < 5 Then lives = lives + 1 Burst(f\x, f\y, 255, 90, 160) End Select Delete f ElseIf f\y > 500 Delete f EndIf Next ; a new level every 100 points level = 1 + score / 100 If lives <= 0 state = GAMEOVER If score > best Then best = score EndIf End Function ; A shower of sparks, in a colour, flying out from x,y. Function Burst(x#, y#, r, g, b) For i = 1 To 24 s.Spark = New Spark s\x = x s\y = y angle# = Rnd(0, 360) speed# = Rnd(1, 5) s\dx = Cos(angle) * speed s\dy = Sin(angle) * speed s\life = Rand(20, 40) s\r = r : s\g = g : s\b = b Next End Function Function MoveSparks() For s.Spark = Each Spark s\x = s\x + s\dx s\y = s\y + s\dy s\dy = s\dy + 0.15 ; gravity pulls them down s\life = s\life - 1 If s\life <= 0 Then Delete s Next End Function Function MoveSky() For i = 0 To 59 skyY(i) = skyY(i) + skySpeed(i) If skyY(i) > 480 Then skyY(i) = 0 : skyX(i) = Rand(0, 639) Next End Function ; Draws everything: background, falling things, sparks, then the words on top. Function DrawGame() If flash > 0 ClsColor 90, 0, 20 flash = flash - 1 Else ClsColor 5, 5, 25 EndIf Cls For i = 0 To 59 shade = 60 + skySpeed(i) * 120 Color shade, shade, shade Plot skyX(i), skyY(i) Next For f.Faller = Each Faller Select f\kind Case STAR : DrawStar(f\x, f\y) Case ROCK : DrawRock(f\x, f\y) Case HEART : DrawHeart(f\x, f\y) End Select Next For s.Spark = Each Spark Color s\r, s\g, s\b Rect s\x, s\y, 3, 3 Next Select state Case TITLE Color 255, 220, 60 Text 320, 150, "S T A R C A T C H E R", True, True DrawStar(200, 150) : DrawStar(440, 150) Color 255, 255, 255 Text 320, 210, "Catch the stars. Dodge the rocks.", True, True Text 320, 230, "Hearts give you an extra life.", True, True Text 320, 290, "Arrows move. Press Space to start.", True, True If best > 0 Then Text 320, 330, "Best score: " + best, True, True Default DrawCatcher(catcherX, 440) Color 255, 255, 255 Text 10, 10, "Score: " + score Text 320, 10, "Level " + level, True For i = 1 To lives DrawHeart(640 - i * 26, 18) Next If state = GAMEOVER Color 255, 90, 90 Text 320, 200, "G A M E O V E R", True, True Color 255, 255, 255 Text 320, 235, "You scored " + score + ". Best: " + best, True, True Text 320, 265, "Press Space to play again", True, True EndIf End Select End Function Function DrawStar(x, y) Color 255, 220, 60 Oval x - 7, y - 7, 14, 14 Line x - 13, y, x + 13, y Line x, y - 13, x, y + 13 Line x - 8, y - 8, x + 8, y + 8 Line x - 8, y + 8, x + 8, y - 8 End Function Function DrawRock(x, y) Color 130, 120, 115 Oval x - 13, y - 12, 26, 24 Color 95, 88, 85 Oval x - 7, y - 6, 7, 6 Oval x + 2, y + 2, 6, 5 End Function Function DrawHeart(x, y) Color 255, 90, 160 Oval x - 10, y - 8, 11, 11 Oval x - 1, y - 8, 11, 11 For row = 0 To 9 Line x - 10 + row, y - 1 + row, x + 10 - row, y - 1 + row Next End Function Function DrawCatcher(x, y) Color 90, 200, 255 Rect x, y, 80, 12 Rect x, y - 14, 8, 14 Rect x + 72, y - 14, 8, 14 Color 200, 240, 255 Rect x + 8, y + 3, 64, 3 End Function