Asteroid Belt
Book: Computer Spacegames
Author: Usborne Publishing
Translator: Marcus Medina
Story
You are on a trip through the Asteroid Belt. To avoid crashing into the asteroids, you must destroy them and the force required for doing this depends on their size.
Asteroids appear on your computer screen as groups of stars. To destroy them you must press the number key corresponding to the number of stars. Be prepared – asteroids come at you thick and fast.
Pseudocode
START INITIALIZE score to 0 FOR 10 ASTEROIDS DISPLAY asteroid size and position PROMPT user for input IF user input matches asteroid size THEN INCREASE score DISPLAY 'Asteroid destroyed' ELSE DISPLAY 'Crashed into asteroid' END GAME END IF END FOR DISPLAY final scoreENDFlowchart
flowchart TD
Start[Start] --> Init[Initialize score to 0]
Init --> Loop[For 10 asteroids]
Loop --> Display[Display asteroid size and position]
Display --> Input[Prompt user for input]
Input --> Check{Input matches asteroid size?}
Check -->|Yes| Increase[Increase score]
Check -->|No| Crash[Display 'Crashed into asteroid']
Crash --> End[End]
Increase --> Destroyed[Display 'Asteroid destroyed']
Destroyed --> Loop
Loop --> Final[Display final score]
Final --> End
Code
Pages


ZX-81 BASIC
10 PRINT "ASTEROID BELT"20 LET S=030 FOR G=1 TO 1040 CLS50 LET A=INT(RND*18+1)60 LET D=INT(RND*12+1)70 LET N=INT(RND*9+1)80 FOR I=1 TO D90 PRINT100 NEXT I110 FOR I=1 TO N120 IF I<4 AND I<7 THEN GOTO 150130 PRINT TAB(A);140 PRINT "*";150 NEXT I160 GOTO 170170 FOR I=1 TO 10180 LET Q=VAL(INKEY$)190 IF Q<>0 THEN GOTO 240200 NEXT I210 PRINT "CRASHED INTO ASTEROID"220 GOTO 300230 LET S=S+1240 IF Q=N THEN GOTO 260250 PRINT "YOU DESTROYED IT"260 LET S=S+1270 IF Q<N THEN PRINT "NOT STRONG ENOUGH"280 IF Q>N THEN PRINT "TOO STRONG"290 FOR I=1 TO 50300 NEXT I310 GOTO 30320 PRINT "YOU HIT ";S;" OUT OF 10"330 STOPC#
using System;
class AsteroidBelt{ static void Main() { Random rnd = new Random(); int score = 0;
Console.WriteLine("Asteroid Belt");
for (int g = 0; g < 10; g++) { Console.Clear(); int position = rnd.Next(1, 19); int rowsDown = rnd.Next(1, 12); int numStars = rnd.Next(1, 9);
for (int i = 0; i < rowsDown; i++) Console.WriteLine();
Console.SetCursorPosition(position, Console.CursorTop); for (int i = 0; i < numStars; i++) Console.Write("*");
Console.WriteLine(); Console.Write("Enter number of stars: ");
if (int.TryParse(Console.ReadLine(), out int input) && input == numStars) { Console.WriteLine("You destroyed it!"); score++; } else { Console.WriteLine("Crashed into asteroid"); break; } }
Console.WriteLine($"You hit {score} out of 10"); }}Python
import random
def main(): score = 0
print("Asteroid Belt")
for _ in range(10): print("\n" * random.randint(1, 12), end="") position = random.randint(1, 18) num_stars = random.randint(1, 9)
print(" " * position + "*" * num_stars)
try: guess = int(input("Enter number of stars: ")) if guess == num_stars: print("You destroyed it!") score += 1 else: print("Crashed into asteroid") break except ValueError: print("Invalid input. Crashed into asteroid.") break
print(f"You hit {score} out of 10")
if __name__ == "__main__": main()Java
import java.util.*;
public class AsteroidBelt { public static void main(String[] args) { Random rnd = new Random(); Scanner scanner = new Scanner(System.in); int score = 0;
System.out.println("Asteroid Belt");
for (int g = 0; g < 10; g++) { System.out.println(); int position = rnd.nextInt(18) + 1; int rowsDown = rnd.nextInt(12) + 1; int numStars = rnd.nextInt(9) + 1;
for (int i = 0; i < rowsDown; i++) System.out.println();
for (int i = 0; i < position; i++) System.out.print(" "); for (int i = 0; i < numStars; i++) System.out.print("*");
System.out.println(); System.out.print("Enter number of stars: ");
try { int input = scanner.nextInt(); if (input == numStars) { System.out.println("You destroyed it!"); score++; } else { System.out.println("Crashed into asteroid"); break; } } catch (InputMismatchException e) { System.out.println("Invalid input. Crashed into asteroid."); break; } }
System.out.println("You hit " + score + " out of 10"); }}Go
package main
import ( "fmt" "math/rand" "time")
func main() { rand.Seed(time.Now().UnixNano()) score := 0
fmt.Println("Asteroid Belt")
for g := 0; g < 10; g++ { fmt.Println() position := rand.Intn(18) + 1 rowsDown := rand.Intn(12) + 1 numStars := rand.Intn(9) + 1
for i := 0; i < rowsDown; i++ { fmt.Println() }
for i := 0; i < position; i++ { fmt.Print(" ") } for i := 0; i < numStars; i++ { fmt.Print("*") }
fmt.Println() fmt.Print("Enter number of stars: ") var input int if _, err := fmt.Scan(&input); err == nil { if input == numStars { fmt.Println("You destroyed it!") score++ } else { fmt.Println("Crashed into asteroid") break } } else { fmt.Println("Invalid input. Crashed into asteroid.") break } }
fmt.Printf("You hit %d out of 10\n", score)}C++
#include <iostream>#include <cstdlib>#include <ctime>
using namespace std;
int main() { srand(time(0)); int score = 0;
cout << "Asteroid Belt" << endl;
for (int g = 0; g < 10; g++) { cout << endl; int position = rand() % 18 + 1; int rowsDown = rand() % 12 + 1; int numStars = rand() % 9 + 1;
for (int i = 0; i < rowsDown; i++) { cout << endl; }
for (int i = 0; i < position; i++) { cout << " "; } for (int i = 0; i < numStars; i++) { cout << "*"; }
cout << endl; cout << "Enter number of stars: "; int input; cin >> input;
if (input == numStars) { cout << "You destroyed it!" << endl; score++; } else { cout << "Crashed into asteroid" << endl; break; } }
cout << "You hit " << score << " out of 10" << endl; return 0;}Explanation
In Asteroid Belt, the player must observe the number of stars (asteroid size) displayed on the screen and input the correct number to destroy the asteroid. The game ends either after 10 asteroids or if the player inputs incorrect values.
Challenges
- Adjust Speed: Modify the game to allow more or less time for user input.
- Scoring System: Change the scoring to award points based on asteroid size.
- Additional Levels: Add more rounds with increasing difficulty.
Copyright
These programs are adaptations of the original Usborne Computer Guides published in the 1980s. The books are free to download for personal or educational use from Usborne’s Computer and Coding Books. Programs and adaptations may not be used for commercial purposes.
Return to Computer Spacegames.