Do you know how to make a space invader game in c++?

I've been trying to make a space invader game day and night for three days straight having little to no sleep but I just can't do it. I'm at my wit's end and I don't know anyone who's willing to help me out on this.

Here's what I've done so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

bool gameOver;

const int width = 40;
const int height = 10;

int x, y, enemyX, enemyY, bulletx[100], bullety[100];

enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN, ATTACK};
eDirecton dir;

void Setup()
{
    gameOver = false;
    dir = STOP;
    
    x = width / 2; // so that the player starts on the middle
    y = height / 2;
}
void Draw()
{
    system("cls"); //system("clear");
    
    for (int i = 0; i < width+2; i++) // top cover
    
        cout << "1";
   		cout << endl;
 
    for (int i = 0; i < height; i++) // system print of what you see
    {
        for (int j = 0; j < width; j++) 
        {
            if (j == 0) //left side of the map
                cout << "2";
                
            if (i == y && j == x)
                cout << "O";
            else
            {
                bool print = false;
                if (!print)
                    cout << " ";				
            }
            if (j == width - 1) //right side of the map
                cout << "3";
        }
        cout << endl;
    }
 
    for (int i = 0; i < width+2; i++) // bottom cover
        cout << "4";
    cout << endl;
}
void Input()
{
    if (_kbhit()) // how the program knows which key to input and what direction it will go
    {
        switch (_getch())
        {
        case 'a':
            dir = LEFT;
            break;
        case 'd':
            dir = RIGHT;
            break;
        case 'w':
            dir = UP;
            break;
        case 's':
            dir = DOWN;
        case 'l':
        	dir = ATTACK;
            break;
        case 'x':
            gameOver = true;
            break;
        }
    }
}

void Logic()
{
    switch (dir)// how the program knows what the directions are
    {
    case LEFT:
        x-=1;
        break;
        
    case RIGHT:
        x+=1;
        break;
        
    case UP:
        y-=1;
        break;
        
    case DOWN:
        y+=1;
        break;
    
    case ATTACK:
    	// can't figure this part out
        break;
        
    default:
        break;
    }
      
    if (x >= width) x = 0; 
    else if (x < 0) x = width - 1;// pass through walls horizontal
    
    if (y >= height) y = 0; 
    else if (y < 0) y = height - 1; // pass through walls vertical

    if (x == enemyX && y == enemyY)
    {
        enemyX = rand() % width;
        enemyY = rand() % height;
    }
}


int main()
{
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
        Sleep(30); //sleep(10);
    }
    return 0;
}


What the end product should be like:

A. The player must be able to shoot
B. Enemies going down from the map
C. When the player hits the enemies they die and the player gains a point
D. The enemy shoots back and when hit the player loses health (health and damage can be of any amount)
E. There is a score system

note: for some reason the player can't move down form the map
Last edited on
With your current design it's not so easy to do.
Once you include bullets that need to move and do all the collision checking it will be very complex.

It would be much easier if you use a kind of game engine.
Some time ago I read this book and found it quite useful. It comes with a simple game engine and and some nice little games.
http://www.amazon.com/Beginning-Game-Programming-Michael-Morrison/dp/0672326590/ref=la_B000AQ2H3C_1_2?s=books&ie=UTF8&qid=1458126294&sr=1-2
closed account (48bpfSEw)
Hi gameguy8888, you can use my simple shotgame as basic implementation for a space invader games. https://processing8.wordpress.com/2016/02/28/einfaches-ballerspiel/

the source is written in simple java code. the main class is the turtle-class based on the language TURTLE. it is very easy to think in circles and lines.

some commands of the turtle class:

lf : turn left
rt : turn right
fw : forward
bw : backward
dc : draw circle
ic : is point in circle (collision detection)
la : look at point or turtle

have fun
@Necip,

why do you think he understands German?
closed account (48bpfSEw)
@Tom, I do not think, I am coding! ^^ Why do you think he does not understand German?
closed account (48bpfSEw)
The idea of the turtle class is : you have a turtle with a pen on the white paper and you can give him commands like "go forward 10 pixels", "turn left 45 degree", "look at a turtle" or "look at a point x/y". It's like using a circle and a ruler to create your scetches on paper.


Source:

https://www.dropbox.com/sh/94bii0pq4selq6y/AADDnmwp3izZSFSWy8oUSTo4a?dl=0



Here are some code snippes:

// Creates an instance of the turtle class. In java is no delete or free necessary.
Turtle t = new TTurtle();

// dg abbr. degree : sets the absolute degree of the turtle. 0 is bottom, 90 is to right, 180 top and 270 to left. of the screen.
t.dg(10);

// deliveres the current orientation
float f = t.dg();

// Sets the turtle on the given point
t.xy (10, 20);

// Get the x, y position of the turtle
float x = t.x();
float y = t.y();

// Every turtle has a radius. dc() draws a circle in the size of the radius. the turtle is in the middle of the circle
t.ra (50);

// Get the radius
float ra = t.ra();

// Pen up! In this case the turtle don't draws a line if the commands forward (fw) or backward (bw) are used. It moves!
t.pu ();

// Pen down! If fw() or bw() is used the turtles draws a line during motion.
t.pd ();

// Turns the turtle 10 degrees to left from it's current orientation
t.lt (10);

// Turns the turtle 10 degrees to right from it's current orientation
t.rt (10);

// Forward: The turtle moves 100 pixels on the way of it's orientation (degree)
t.fw (100);

// Backward: The turtle moves 100 pixels backwards against it's orientation (degree)
t.bw (100);

// Center: Centers the turtle to the middle of the screen and degree = 0
t.ct ();

// LookAt : The turtle should look to another turtle. New orientation point!
t.la (t2);

// LookAt : The turtle should look to a point. New orientation point!
t.la (p);

// PlaceTo: Sets the turtle to the position of another turtle. Degree is the same as before.
t.pt (t2);

// PlaceTo: Sets the turtle to the position. Degree is not touched.
t.pt (p);

// Draw Circle: Draws a circle arround the turtle with the radius set with rd()
t.dc ();

// Draw Circle: Sie soll ein Teilkreis von/bis ihres eigenen Radius zeichnen
t.dc2 (45,90);

// Dot: Draw a point. pu() is not considered.
t.dt ();

// Distract: Distracts or attracts to another turtle. The border is the radius of the turtles.
t.dt (t2);

// Distance: Get the distance between two turtles.
float d = t.di(p);

// IsInCircle: Is true if the point is within the area of the turtle's circle
boolean b = t.ic(p);

// IsInCircle: Is true if the turtle is within the are of another turtles circle
boolean b = t.ic(t2);
Last edited on
Topic archived. No new replies allowed.