DECLARE SUB Initialize () DECLARE SUB DrawPlayer () DECLARE SUB MovePlayer (direction AS INTEGER) DECLARE SUB Shoot () DECLARE SUB CheckInput () DECLARE SUB InitializeEnemies () DECLARE SUB MoveEnemies () DECLARE SUB EnemyShoot () DECLARE SUB MoveEnemyBullets () DECLARE SUB DrawEnemies () DECLARE SUB CheckCollisions () DECLARE SUB GameOver () DECLARE SUB DrawGame ()
TYPE Enemy x AS INTEGER y AS INTEGER dx AS INTEGER active AS BOOLEAN END TYPE
TYPE Bullet x AS INTEGER y AS INTEGER active AS BOOLEAN END TYPE
DIM SHARED playerX AS INTEGER, playerY AS INTEGER DIM SHARED playerBullet AS Bullet DIM SHARED enemies(maxEnemies) AS Enemy DIM SHARED enemyBullets(maxEnemyBullets) AS Bullet DIM SHARED score AS INTEGER DIM SHARED gameRunning AS BOOLEAN
RANDOMIZE TIMER Initialize
DO WHILE gameRunning AND NOT INKEY$ = CHR$(27) CALL CheckInput CALL MoveEnemies CALL EnemyShoot CALL MoveEnemyBullets CALL CheckCollisions CALL DrawGame SLEEP 1 LOOP
IF NOT gameRunning THEN CALL GameOver END IF
SUB Initialize playerX = screenWidth / 2 playerY = screenHeight - 1 playerBullet.active = FALSE score = 0 gameRunning = TRUE CALL InitializeEnemies END SUB
SUB InitializeEnemies FOR i = 1 TO maxEnemies enemies(i).x = INT(RND * (screenWidth - 2)) + 2 enemies(i).y = INT(RND * 5) + 1 enemies(i).dx = IIF(RND > 0.5, 1, -1) enemies(i).active = TRUE NEXT i END SUB
SUB DrawGame CLS LOCATE playerY, playerX PRINT playerChar; IF playerBullet.active THEN LOCATE playerBullet.y, playerBullet.x PRINT bulletChar; END IF FOR i = 1 TO maxEnemies IF enemies(i).active THEN LOCATE enemies(i).y, enemies(i).x PRINT enemyChar; END IF NEXT i FOR i = 1 TO maxEnemyBullets IF enemyBullets(i).active THEN LOCATE enemyBullets(i).y, enemyBullets(i).x PRINT enemyBulletChar; END IF NEXT i LOCATE 1, 1 PRINT "Score: "; score END SUB
SUB MovePlayer (direction AS INTEGER) IF direction = -1 AND playerX > 1 THEN playerX = playerX - 1 ELSEIF direction = 1 AND playerX < screenWidth THEN playerX = playerX + 1 END IF END SUB
SUB Shoot IF NOT playerBullet.active THEN playerBullet.x = playerX playerBullet.y = playerY - 1 playerBullet.active = TRUE END IF END SUB
SUB CheckInput DIM key AS STRING key = INKEY$ SELECT CASE key CASE CHR$(75) ' left CALL MovePlayer(-1) CASE CHR$(77) ' right CALL MovePlayer(1) CASE " " ' space CALL Shoot END SELECT END SUB
SUB MoveEnemies FOR i = 1 TO maxEnemies IF enemies(i).active THEN enemies(i).x = enemies(i).x + enemies(i).dx IF enemies(i).x <= 1 OR enemies(i).x >= screenWidth THEN enemies(i).dx = -enemies(i).dx enemies(i).y = enemies(i).y + 1 END IF IF enemies(i).y >= screenHeight THEN enemies(i).y = 1 END IF END IF NEXT i END SUB
SUB EnemyShoot FOR i = 1 TO maxEnemies IF enemies(i).active AND INT(RND * 10) < 2 THEN FOR j = 1 TO maxEnemyBullets IF NOT enemyBullets(j).active THEN enemyBullets(j).x = enemies(i).x enemyBullets(j).y = enemies(i).y + 1 enemyBullets(j).active = TRUE EXIT FOR END IF NEXT j END IF NEXT i END SUB
SUB MoveEnemyBullets FOR i = 1 TO maxEnemyBullets IF enemyBullets(i).active THEN enemyBullets(i).y = enemyBullets(i).y + 1 IF enemyBullets(i).y > screenHeight THEN enemyBullets(i).active = FALSE END IF END IF NEXT i END SUB
SUB CheckCollisions IF playerBullet.active THEN FOR i = 1 TO maxEnemies IF enemies(i).active THEN IF playerBullet.x = enemies(i).x AND playerBullet.y = enemies(i).y THEN playerBullet.active = FALSE enemies(i).active = FALSE score = score + 10 EXIT FOR END IF END IF NEXT i END IF FOR i = 1 TO maxEnemyBullets IF enemyBullets(i).active THEN IF enemyBullets(i).x = playerX AND enemyBullets(i).y = playerY THEN gameRunning = FALSE END IF END IF NEXT i END SUB
SUB GameOver CLS LOCATE screenHeight / 2, (screenWidth / 2) - 5 PRINT "Game Over! Score: "; score SLEEP 5 END SUB