๐ฎ Galaxy Defender Game (C++ OpenGL/GLUT)
๐งพ Overview
๐ This is a 2D space shooting game made in C++ using OpenGL/GLUT.
The player controls a rocket, shoots enemies, and survives as long as possible while increasing score.
โ๏ธ Game Setup
- ๐ฅ๏ธ A game window is created using GLUT
- ๐
SetCanvasSize() defines screen size
- ๐จ OpenGL handles all drawing (shapes, colors, text)
๐ง Game Variables
- ๐ Player position:
rx, ry
- ๐พ Enemies:
ex[], ey[]
- ๐ซ Bullets: arrays for position + active state
- โค๏ธ Lives:
lives = 3
- ๐งฎ Score:
score = 0
- ๐ฎ Game states: start, pause, game over
๐ฅ๏ธ Display Function (Rendering Engine)
๐ฏ This function draws everything on screen every frame:
๐ฌ Start Screen
- Enter player name ๐ง
- Press ENTER to start โจ๏ธ
๐ Game Over Screen
- Shows final score ๐งฎ
- Shows player name ๐ง
- Stops game
๐ Gameplay Screen
- ๐ Draws rocket
- ๐พ Draws enemies
- ๐ซ Draws bullets
- ๐ Background stars
- โค๏ธ Shows lives
- ๐งฎ Shows score
โจ๏ธ Controls
๐ Movement
- โฌ
๏ธ Left arrow โ move left
- โก๏ธ Right arrow โ move right
๐ซ Actions
- SPACE โ shoot bullet
- P โ pause โธ๏ธ
- R โ resume โถ๏ธ
- ENTER โ start game
โฑ๏ธ Timer Function (Game Engine)
โก This is the MOST IMPORTANT part of the game loop.
๐ซ Bullet System
- Bullets move upward โฌ๏ธ
- Deactivate when out of screen
๐พ Enemy System
- Enemies move downward โฌ๏ธ
- Reset position when hit or out of screen
๐ฅ Collision Detection
-
If bullet touches enemy:
- ๐ฅ Enemy destroyed
- ๐งฎ Score +5
- ๐พ Enemy respawns
โค๏ธ Life System
๐ Difficulty Increase
-
As score increases:
- ๐พ Enemy speed increases
๐ Game Over
-
When lives = 0:
- Game ends
- Score saved to file ๐พ (
data.txt)
๐พ File Handling
๐ Stores player results:
Example:
๐ Game Loop Flow
๐ The game runs continuously like this:
๐ฅ๏ธ Display โ ๐ฎ Input โ โก Timer โ ๐ Repeat
๐งฉ Main Function
- Initializes GLUT ๐ฅ๏ธ
- Creates window ๐ฆ
- Registers functions ๐ฎ
- Starts infinite loop ๐ (
glutMainLoop())
๐ Summary
๐ฏ Galaxy Defender is a real-time arcade shooter game where:
- ๐ Player controls a rocket
- ๐พ Enemies fall from top
- ๐ซ Player shoots bullets
- ๐ฅ Collision gives score
- โค๏ธ Lives decrease on miss
- ๐ Game ends at zero lives
- ๐พ Score is saved automatically