Asteroids - QBasic Source Code

Back to: Main Programming Page

A game where you shoot falling asteroids before they hit the ground- not exactly like the original asteroids, but something similar.

 

Source Code:

' мллллм  мллллллм лллллллл ллллллллл ллллллм   млллллм  лл ллллллм  мллллллм
'лл    лл ллп  ппп    лл    лл        лл   плл ллп   плл лл лл   плл ллп  ппп
'лл    лл пллм        лл    лл        лл   млл лл     лл лл лл    лл пллм
'лллллллл   пллм      лл    лллллл    ллллллп  лл     лл лл лл    лл   пллм
'лл    лл     пллм    лл    лл        лл   лл  лл     лл лл лл    лл     пллм
'лл    лл ллм  млл    лл    лл        лл    лл ллм   млл лл лл   млл ллм  млл
'лл    лл  пллллп     лл    ллллллллл лл    лл  плллллп  лл ллллллп   пллллп
'
'This is a one person game.  The object is to shoot the falling asteroids and
'keep them from hitting the surface.   Use the left and right arrow keys to
'move your gun and the up key to fire.  The "P" button pauses the game, and
'the "Q" button ends it.  When a game is paused, hit space to make it run
'again.  Scoring is based on the level you're on.  The higher the level, the
'more points for hit asteroids, and the less points taken away for missed
'asteroids.  While you play, you may notice the screen flickering badly, or
'the action to fast.  To correct this, make the number after Flicker.Control
'(shown below) bigger.  If the game goes too slow, make the number smaller.
Flicker.Control = 500   ' Now press "F5" to play.
'
'
'

SCREEN 7       '320 X 200 resolution
RANDOMIZE TIMER

movement = 0

playagain:
CLS
LOCATE 12, 1
COLOR 15
INPUT ; "What level do you want to start at"; level
LOCATE 12, 1
PRINT "           "
gun = 165
missile.x = -5000
missile.y = 5000
totalhit = 0
misses = 0
totaldropped = 0

newasteroid:
asteroid.x = 319 * RND + 1
asteroid.y = 1
totaldropped = totaldropped + 1

a:

COLOR 0
WHILE movement = 1
LINE (gun2 - 20, 170)-(gun2 + 20, 170)
LINE (gun2, 155)-(gun2 - 20, 170)
LINE (gun2, 155)-(gun2 + 20, 170)
LINE (gun2 - 3, 147)-(gun2 + 3, 170), , BF
movement = 0
WEND

COLOR 15
LINE (gun - 20, 170)-(gun + 20, 170)
LINE (gun, 155)-(gun - 20, 170)
LINE (gun, 155)-(gun + 20, 170)
LINE (gun - 3, 147)-(gun + 3, 170), , BF
LINE (asteroid.x - 10, asteroid.y - 6)-(asteroid.x + 10, asteroid.y + 6), 6, BF
CIRCLE (missile.x, missile.y), 3, 12
LINE (1, 171)-(319, 171), 9

FOR count = 1 TO Flicker.Control
NEXT count


asteroid.x2 = asteroid.x
asteroid.y2 = asteroid.y
missile.x2 = missile.x
missile.y2 = missile.y

COLOR 0
LINE (asteroid.x2 - 10, asteroid.y2 - 6)-(asteroid.x2 + 10, asteroid.y2 + 6), 0, BF
CIRCLE (missile.x2, missile.y2), 3, 0



LOCATE 23, 1
COLOR 9
PRINT "Shot:"; totalhit
LOCATE 23, 11
PRINT "Level:"; level
LOCATE 23, 21
PRINT "Score:"; score

missile.y = missile.y - 5

IF missile.y < 1 THEN
missile.y = 5000
END IF

asteroid.y = asteroid.y + level * .75

IF asteroid.y > 170 THEN
CIRCLE (asteroid.x, asteroid.y), 30, 14
CIRCLE (asteroid.x, asteroid.y), 30, 0
misses = misses + 1
IF misses = 10 THEN
GOTO gameover
END IF
score = score - INT(10 / level)
GOTO newasteroid
END IF

IF missile.x - 3 < asteroid.x + 10 AND missile.x + 3 > asteroid.x - 10 AND missile.y + 3 < asteroid.y + 6 THEN
totalhit = totalhit + 1
IF totalhit / 10 = INT(totalhit / 10) THEN
level = level + 1
misses = 0
END IF
score = score + 10 * level
FOR ctr = 1 TO 7
  radius = 15 * RND + 5
  offset.x = 40 * RND - 20
  offset.y = 40 * RND - 20
  circlecolor = 16 * RND
  CIRCLE (asteroid.x + offset.x, asteroid.y + offset.y), radius, circlecolor
  FOR ctr2 = 1 TO 50
  NEXT ctr2
  CIRCLE (asteroid.x + offset.x, asteroid.y + offset.y), radius, 0
NEXT ctr
missile.y = 5000
GOTO newasteroid
END IF

move$ = INKEY$

SELECT CASE move$
CASE CHR$(0) + "K"
gun2 = gun
gun = gun - 5
movement = 1
CASE CHR$(0) + "M"
gun2 = gun
gun = gun + 5
movement = 1
CASE CHR$(0) + "H"
missile.x = gun
missile.y = 147
CASE "q"
GOTO quit
CASE "p"
COLOR 15
LINE (gun - 20, 170)-(gun + 20, 170)
LINE (gun, 155)-(gun - 20, 170)
LINE (gun, 155)-(gun + 20, 170)
LINE (gun - 3, 147)-(gun + 3, 170), , BF
LINE (asteroid.x - 10, asteroid.y - 6)-(asteroid.x + 10, asteroid.y + 6), 6, BF
CIRCLE (missile.x, missile.y), 3, 12
LOCATE 12, 4
PRINT "Game Paused Hit Space to continue"
startpause:
  a$ = INKEY$
  SELECT CASE a$
  CASE " "
  LOCATE 12, 4
  PRINT "              "
  GOTO endpause
  END SELECT
GOTO startpause
END SELECT

endpause:

GOTO a

gameover:

LINE (1, 171)-(319, 171), 9
LOCATE 23, 1
COLOR 9
PRINT "Shot:"; totalhit
LOCATE 23, 11
PRINT "Level:"; level
LOCATE 23, 21
PRINT "Score:"; score
COLOR 15

LINE (gun - 20, 170)-(gun + 20, 170)
LINE (gun, 155)-(gun - 20, 170)
LINE (gun, 155)-(gun + 20, 170)
LINE (gun - 3, 147)-(gun + 3, 170), , BF

COLOR 4
LOCATE 12, 1
PRINT "E        O"
LOCATE 12, 1
PRINT "ME      OV"
LOCATE 12, 1
PRINT "AME              OVE"
LOCATE 12, 1
PRINT "GAME            OVER"
LOCATE 12, 1
PRINT " GAME          OVER "
LOCATE 12, 1
PRINT "  GAME        OVER "
LOCATE 12, 1
PRINT "   GAME      OVER "
LOCATE 12, 1
PRINT "    GAME    OVER "
LOCATE 12, 1
PRINT "     GAME            OVER "
LOCATE 12, 1
PRINT "      GAME          OVER "
LOCATE 12, 1
PRINT "       GAME        OVER "
LOCATE 12, 1
PRINT "        GAME      OVER "
LOCATE 12, 1
PRINT "         GAME    OVER "
LOCATE 12, 1
PRINT "          GAME  OVER "
LOCATE 12, 1
PRINT "           GAME          OVER "
LOCATE 12, 1
PRINT "  GAME        OVER "
LOCATE 12, 1
PRINT "   GAME      OVER "
LOCATE 12, 1
PRINT "    GAME    OVER "
LOCATE 12, 1
PRINT "     GAME  OVER "

FOR ctr = 1 TO 500
NEXT ctr

LOCATE 13, 13
PRINT "Play Again? y/n"
playagain.y.n:
y.n$ = INKEY$
SELECT CASE y.n$
CASE "n"
GOTO quit
CASE "y"
GOTO playagain
END SELECT
GOTO playagain.y.n

quit: