---
title: "console-space"
year: "2019"
motto: "Console Space Shooter Game"
description: "Windows console space shooter video-game based on space invaders."
stack: ["cpp"]
website: "https://github.com/RolandoAndrade/console-space"
url: "https://rolandoandrade.me/projects/console-space/"
author: "Rolando Andrade"
---

# console-space

> Console Space Shooter Game

Windows console space shooter video-game based on space invaders.

Year: 2019. Stack: cpp. Website: https://github.com/RolandoAndrade/console-space

Repositories:

- [github](https://github.com/RolandoAndrade/console-space)
- [gitlab](https://gitlab.com/RolandoAndrade/console-space)

Textbook examples of data structures are always the same. Add to a list, remove from a list, walk through a list. You read about nodes, pointers and memory allocation, and none of it sticks.

I wanted a project where a doubly linked list was the base of real gameplay, not an exercise. A Space Invaders clone for the Windows console looked like the right challenge.

The result was Console Space, a simple shooter that runs in a command prompt window. It has no graphics libraries and no game engine. It is only ASCII characters, keyboard input and a lot of pointer work.

## Shoot before they arrive

You fly a ship on the left side of the screen. A grid of enemies advances from the right. You shoot, your bullets hit enemies, and the enemies disappear. The game runs until you win or the enemies reach you.

The ship is a special ASCII character. Enemies are small squares. Bullets are short horizontal lines that cross the screen. Everything updates about ten times per second.

The arrow keys move the ship, and the spacebar fires. The game reads the keyboard without blocking, which means it never stops to wait for a key. You can move and shoot at the same time.

## Objects come and go

A game must track groups of objects that appear and disappear. Bullets appear when you fire. They vanish when they hit something or leave the screen. Enemies start in a formation and get removed one by one as you destroy them.

Arrays would work, but they waste memory on objects that are not in use. You either reserve space for the most bullets possible, or you write complex bookkeeping to reuse empty slots.

Linked lists solve this cleanly. A linked list is a chain of nodes where each node points to the next. Each bullet is a node. Firing creates a new node and links it. A hit removes that node and frees its memory. The list grows and shrinks as the game runs.

For enemies, I used a doubly linked list. Each enemy node knows both its previous and its next neighbor. That makes removal simpler. When an enemy dies, you update two pointers and you are done. There are no elements to shift and no gaps to track.

## One frame, six steps

The main loop runs forever. Each pass does the same steps in the same order.

1. It erases the ship from its old position and checks the keyboard. Arrow keys change the ship's coordinates. The spacebar calls the firing function.
2. It moves the bullets. Every bullet advances one position to the right. A bullet that leaves the visible area is removed from the list.
3. It moves the enemies. They shift up and down in a pattern and slowly advance toward the player. A movement variable tracks the direction and starts the march to the left.
4. It checks for collisions. If a bullet and an enemy share the same coordinates, the game removes both from their lists.
5. It draws everything: the ship, every bullet and every enemy.
6. It sleeps for 100 milliseconds before the next pass.

## Drawing with a cursor

Drawing in a Windows console means moving a cursor and printing characters. The gotoxy function puts the cursor at any coordinates. Printing a character places it there.

To erase a position, you move to it and print a space. Erase the old spot, print at the new spot, and the object looks like it moved.

The console is a grid of 51 by 51 characters. Each game object only needs x and y coordinates in that space. There is no pixel math and no sprite sheet, only row and column numbers.

## Bullets in a chain

Bullets use a singly linked list, where each node only points forward. Each node stores x and y coordinates and a pointer to the next bullet.

Firing creates a new node, sets its position to the ship's location and links it at the end of the list. A guard blocks rapid fire. It checks that the last bullet has traveled far enough first.

Updating bullets is recursive. The function walks the list. For each bullet, it erases the old position, moves the x coordinate forward and draws the bullet in its new place.

When a bullet goes off screen, it is unlinked and deleted. The check happens before the update, so a bullet is never drawn outside the play area.

## Twenty five marching enemies

Enemies use a doubly linked list. The extra pointer to the previous node makes removal simpler. When an enemy dies, its neighbors are changed to point past it.

The game starts with 25 enemies in a 5 by 5 formation. Each node gets coordinates that place it on the right side of the screen. The list builds from front to back, and each new enemy links to the one before it.

The enemies move in a zigzag. They shift up or down for a few frames, then advance one column toward the player. A counter tracks when to switch direction. This creates the classic Space Invaders march.

## Every bullet against every enemy

Finding hits means comparing every bullet with every enemy. The check function walks the enemy list and looks for a matching position.

When a bullet and an enemy share the same coordinates, both are removed. The bullet leaves its list, the enemy leaves its list, and their memory is freed.

The removal code handles the edge cases. An enemy might be first in the list, last in the list or somewhere in the middle. The doubly linked structure makes all three cases manageable.

## Pointers you can see

This game taught me more about pointers than any classroom exercise. When your ship stops shooting because you forgot to update a link, you learn to trace memory carefully. When enemies disappear without a reason, you learn to debug how you walk a list.

Simple building blocks create complex behavior. A doubly linked list is a basic idea. It makes removing enemies smooth, and smooth removal makes the game fun to play. The choice of data structure matters.

Working without a game engine taught me a lot too. Modern tools hide much of this work. I wrote a game loop by hand, read input without libraries and drew everything with print statements. Now I understand what frameworks do for me.

Console Space was a small project with a narrow scope. It turned an abstract idea into something I could watch. Every time a bullet hit an enemy, I saw the linked list at work.

[Book a call with Rolando](https://rolandoandrade.me/pricing/)
