Need help with space shooter in SDL!

Pages: 123
closed account (D80DSL3A)
I tried your game. Very nice!

Lines 19 and 20 from your code above prove why you really should learn the language before you try making a game!
1
2
const int myBullet.box.w = BULLET_WIDTH; // This is what you told me to do
const int myBullet.box.h = BULLET_HEIGHT; // It doesn't work, why? 

myBullet is a variable which exists only in the main() function. At line 20 the compiler doesn't even know what a Bullet is yet!

I handled these assignments by writing a parametrized constructor for the 3 classes in your game. You'll notice that in the code to follow.

I tried to modify your code as little as possible. Others may see many design flaws, but I figure the goal here is to just make it work.

Since I don't have SDL installed I had to comment out all the SDL related code when I worked on it. This should be OK since all of that code was already working fine. I had to modify a few lines of the SDL code. Hopefully I did it correctly.
The code (without any SDL code in it) compiles with no errors.
I really hope it works!

See the next post for the code, due to post length restriction.
closed account (D80DSL3A)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include "SDL.h"
#include "SDL_image.h"
#include <string>
#include "Classes.h" // I split my code up into header files
#include "Graphics.h" // But all just put all my code together here
#include "Timer.h"
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int FRAMES_PER_SECOND = 20;
const int SQUARE_WIDTH = 16;
const int SQUARE_HEIGHT = 12;
const int LEVEL_WIDTH = 640;
const int LEVEL_HEIGHT = 480;
const int BULLET_HEIGHT = 5;
const int BULLET_WIDTH = 5;
const int ENEMY_HEIGHT = 11;
const int ENEMY_WIDTH = 13;

SDL_Surface *square = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *bullet = NULL;
SDL_Surface *enemy = NULL;
SDL_Event event;

class Enemy
{
public:
    SDL_Rect Box;
	int xVel, yVel;
	bool alive;
	Enemy(int w, int h);
	void move();
    void show();
};
class Square
{
public:
    SDL_Rect Box;
	int xVel, yVel;
	bool alive;
    Square(int w, int h);
    void handle_input();
    void move();
    void show();
};
class Bullet
{
public:
    SDL_Rect Box;
	int xVel, yVel;
	bool alive;
	Bullet(int w, int h);
	void handle_input(int xFire, int yFire );
	void move();
	void show();
};


class Timer
{
    private:
    int startTicks;
    int pausedTicks;
    bool paused;
    bool started;

    public:
    Timer();
    void start();
    void stop();
    void pause();
    void unpause();
    int get_ticks();
    bool is_started();
    bool is_paused();
};

SDL_Surface *load_image( std::string filename )
{
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
    loadedImage = IMG_Load( filename.c_str() );
    if( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );
        SDL_FreeSurface( loadedImage );
        if( optimizedImage != NULL )
        {
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY,

SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }
    return optimizedImage;
}

bool init()
{
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT,

SCREEN_BPP, SDL_SWSURFACE );
    if( screen == NULL )
    {
        return false;
    }
    SDL_WM_SetCaption( "Move the Spaceship", NULL );
    return true;
}

bool load_files()
{
    square = load_image( "spaceship.png" );
    background = load_image( "bg.png" );
	bullet = load_image( "bullet.png" );
	enemy = load_image( "enemy.png" );
    if( square == NULL || background == NULL || bullet == NULL ||

enemy == NULL )
    {
        return false;
    }
    return true;
}

void clean_up()
{
    SDL_FreeSurface( square );
    SDL_FreeSurface( background );
	SDL_FreeSurface( bullet );
	SDL_FreeSurface( enemy );
    SDL_Quit();
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface*

destination, SDL_Rect* clip = NULL )
{
SDL_Rect offset;

    offset.x = x;
    offset.y = y;
    SDL_BlitSurface( source, clip, destination, &offset );
}

Timer::Timer()
{
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
}

void Timer::start()
{
    started = true;
    paused = false;
    startTicks = SDL_GetTicks();
}

void Timer::stop()
{
    started = false;
    paused = false;
}

void Timer::pause()
{
    if( ( started == true ) && ( paused == false ) )
    {
        paused = true;
        pausedTicks = SDL_GetTicks() - startTicks;
    }
}

void Timer::unpause()
{
    if( paused == true )
    {
        paused = false;
        startTicks = SDL_GetTicks() - pausedTicks;
        pausedTicks = 0;
    }
}

int Timer::get_ticks()
{
    if( started == true )
    {
        if( paused == true )
        {
            return pausedTicks;
        }
        else
        {
            return SDL_GetTicks() - startTicks;
        }
    }
    return 0;
}

bool Timer::is_started()
{
    return started;
}

bool Timer::is_paused()
{
    return paused;
}

Bullet::Bullet(int w, int h)
{
    alive = false;
    Box.x = Box.y = 0;
    Box.w = w;
    Box.h = h;
    xVel = 0;
    yVel = -3;

}
void Bullet::handle_input(int xFire, int yFire )
{
	if( alive == true ) return;
    if( event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE )
    {
        Box.x = xFire;
        Box.y = yFire;
        alive = true;
    }
}
void Bullet::move()
{
	Box.x += xVel;
	Box.y += yVel;
    if( alive == false ) return;
    if( ( Box.x < 0 ) || ( Box.x + Box.w > LEVEL_WIDTH ) )
    {
		alive = false;
    }
    if( ( Box.y < 0 ) || ( Box.y + Box.h > LEVEL_HEIGHT ) )
    {
       alive = false;
	}
}
void Bullet::show()
{
	if(alive == true)
	{
        apply_surface( Box.x, Box.y, bullet, screen );
	}
}
Enemy::Enemy(int w, int h)
{
	alive = true;
	Box.x = 0;
	Box.y = 80;
	Box.w = w;
	Box.h = h;
	yVel = 0;
	xVel = 1;
}
void Enemy::show()
{
	if(alive == true)
	{
        apply_surface( Box.x, Box.y, enemy, screen );
	}
}

void Enemy::move()
{
	if(alive == false) return;
	Box.x += xVel;
	Box.y += yVel;
    if( ( Box.x < 0 ) || ( Box.x + Box.w > LEVEL_WIDTH ) )
    {
		alive = false;
    }
    if( ( Box.y < 0 ) || ( Box.y + Box.h > LEVEL_HEIGHT ) )
    {
       alive = false;
	}
}

Square::Square(int w, int h)
{
	alive = true;
    Box.x = 0;
    Box.y = 0;
    Box.w = w;
    Box.h = h;
    xVel = 0;
    yVel = 0;
}

void Square::handle_input()
{
	if(squarealive == false) return;
    if( event.type == SDL_KEYDOWN )
    {
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel -= SQUARE_HEIGHT / 2; break;
            case SDLK_DOWN: yVel += SQUARE_HEIGHT / 2; break;
            case SDLK_LEFT: xVel -= SQUARE_WIDTH / 2; break;
            case SDLK_RIGHT: xVel += SQUARE_WIDTH / 2; break;
        }
    }
    else if( event.type == SDL_KEYUP )
    {
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel += SQUARE_HEIGHT / 2; break;
            case SDLK_DOWN: yVel -= SQUARE_HEIGHT / 2; break;
            case SDLK_LEFT: xVel += SQUARE_WIDTH / 2; break;
            case SDLK_RIGHT: xVel -= SQUARE_WIDTH / 2; break;
        }
    }
}

void Square::move()
{
	if(alive == false) return;
    Box.x += xVel;
    if( ( Box.x < 0 ) || ( Box.x + Box.w > LEVEL_WIDTH ) )
    {
        Box.x -= xVel;
    }
    Box.y += yVel;
    if( ( Box.y < 0 ) || ( Box.y + Box.h > LEVEL_HEIGHT ) )
    {
        Box.y -= yVel;
    }
}

void Square::show()
{
	if(alive == false) return;

    apply_surface( x, y, square, screen );
}

bool check_collision(SDL_Rect a, SDL_Rect b)
{
    // miss conditions
    if( a.x + a.w < b.x ) return false;// a left of b
    if( a.x > b.x + b.w ) return false;// a right of b

    if( a.y + a.h < b.y ) return false;// a is above b
    if( a.y > b.y + b.h ) return false;// a is below b

    // it's a hit!
    return true;
}

int main( int argc, char* args[] )
{
    bool quit = false;
    Square mySquare(SQUARE_WIDTH, SQUARE_HEIGHT);
	Bullet myBullet(BULLET_WIDTH, BULLET_HEIGHT);
	Enemy myEnemy(ENEMY_WIDTH, ENEMY_HEIGHT);
    Timer fps;
    if( init() == false )
    {
        return 1;
    }
    if( load_files() == false )
    {
        return 1;
    }
    while( quit == false )
    {
      fps.start();
       while( SDL_PollEvent( &event ) )
        {

            mySquare.handle_input();
	    myBullet.handle_input(mySquare.Box.x, mySquare.Box.y);

            if( event.type == SDL_QUIT )
            {
                quit = true;
            }
        }

        mySquare.move();
	myBullet.move();
	myEnemy.move();

	if( check_collision(myBullet.Box, myEnemy.Box) )
	{
		myBullet.alive = false;
		myEnemy.alive = false;
	}

        apply_surface( 0, 0, background, screen );
        mySquare.show();
	myEnemy.show();
	myBullet.show();

        if( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
    }
    clean_up();

    return 0;
}
Last edited on
Hi fun2code!
I tried your game. Very nice!
Why thank you!
About that myBullet being a const int i was actually thinking why are you telling me to do this if it's myBullet, it hasn't even been initialized and it's the object for the bullet class, i misread what you posted thinking that you wanted me to make it a const int (umm i'm not making excuses or anything...).

Also by the way it worked! Yay! Well kinda... The bullet hits the ufo and it dissapears, so does the bullet and if i miss the ship well, i miss but if i hit the ufo it and the bullet will dissapear but if i fire another bullet after the ufo is destroyed it will dissapear when it comes to the line where the ufo was. I can't really explain the problem because i'm not really sure about it maybe download the game if you wanted to look at it here: http://tiny.cc/laurencegame
Try destroying the ufo with the bullet then firing again without moving.

Thank you!


closed account (D80DSL3A)
I'm quite glad that worked. The flaw you mentioned makes sense. The ufo still exists after it's hit, it just isn't being moved or drawn anymore. This is because we wrote for it to be moved and drawn only if alive == true.

The flaw is an oversight. The collision check should also be done only if the ufo is alive. Replace line 396 with this to fix it:
if( myEnemy.alive && check_collision(myBullet.Box, myEnemy.Box) )

I'm sorry, I didn't say where to make the assignments for myBullet.Box.w and .h did I? My bad. I had meant for that to be done in main() just after declaring Bullet myBullet; but this is is no longer necessary. The assignments are made in the Bullet constructor now. I hope you can see how the new code works.
closed account (N36fSL3A)
^That makes sense. My way I'd do it is set the object's variables all to NULL.
Thanks it works really well now!
Now i just need to make the ufo respawn when it's destroyed, i'm not sure how i could make it spawn at a random position on the side of the screen like the game randomly selects one of say four points where the ufo spawns maybe something like this?
1
2
3
4
5
class Enemy
{
public:
	int spawnpoint1;
        int spawnpoint2;


1
2
3
4
5
6
7
8
9
10
11
12
Enemy::Enemy(int w, int h)
{
	alive = true;
	Box.x = 0;
	Box.y = 80;
	Box.w = w;
	Box.h = h;
	yVel = 0;
	xVel = 1;
	spawnpoint1 = Box.x = 200, Box.y = 200;
        spawnpoint2 = Box.x = 100, Box.y = 100;
}


I'm assuming that x, y have been replaced with box.x and box.y, is this true?

Then i'd have to either put something in check_collision or make a new function to set enemy alive to true and make it spawn at one of these points a few seconds after it's destroyed.
What do you think?

EDIT: I accidently left the code in while compiling and the ufo spawns at spawnpoint1!
Last edited on
closed account (D80DSL3A)
I thought that would work. Changing the Enemy constructor as you proposed won't make the ufo respawn. Please keep the constructor as it was.

I'm assuming that x, y have been replaced with box.x and box.y, is this true?
Yes. Box.x and Box.y have replaced x and y everywhere.

Since your game ends when the single ufo is either shot down or crosses the screen it would be nice to have it respawn. Many solutions are possible. To keep things simple I'll go with a global function. You can just add in a bit of code and leave the classes as they are. The following function will make the ufo reappear at a fixed time after it is killed or finishes crossing the screen. I'm going to ask you to study c++ a bit more, until you can modify the function so that Box.y is varied from launch to launch. I guarantee you that any time spent studying how to program will pay you back 10 fold on the time investment. You won't be able to do much yourself without it.

This function will respawn the ufo at the same y value each time.

1) Place this function just before the main() begins (at line 361 in the code above).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void spawn_enemy( Enemy& e )
{
    if( e.alive ) return;

    int spawnDelay = 100;// adjust to change respawn time
    static int spawnTime = 0;// static variable retains its value between function calls
    if( ++spawnTime >= spawnDelay )// time to spawn
    {
        e.Box.x = 0;
        e.Box.y = 80;
        e.alive = true;
        spawnTime = 0;// reset for next time
    }
}


2) Add this at line 391 in above code (ie before mySquare.move(); )
spawn_enemy( myEnemy );

You'll be wanting to add many features to your game. We can only do so much in one thread. This is my favorite type of thread though - one in which the guy who started it responds regularly!
Last edited on
Thanks!
Firstly, i added a function i called square_check_collision so when the ship touches the ufo both disappear now!
EDIT: It still fires the bullet though... I fixed it so that if enemy alive == false the bullet can't spawn.
Also i'm not sure why but the respawn enemy doesn't work i'm not sure why i'll keep trying to fix it if i can! i changed the respawn time and it works, turns out i wasn't patient enough... Is there any way i could cause it to spawn at different locations (predefined not random)?

I guarantee you that any time spent studying how to program will pay you back 10 fold on the time investment.

You're right, i did it!

1
2
e.Box.x = rand() % 100;
e.Box.y = rand() % 200;


It's very very rare but sometimes the ufo spawns ontop of me :P I'm sure i can make it exclude the area that the ship's collision box is at.
Last edited on
Hi again,
I'm trying to make multiple ufos but it's not working i've looked everywhere,
do you know how i could do it?
I tried this but it doesn't work:
Enemy myEnemy[5](ENEMY_WIDTH, ENEMY_HEIGHT);

EDIT: I've got this now: std::vector<Enemy> enemies
but i still don't know what to do?
Thanks!
Last edited on
closed account (D80DSL3A)
Good job on the use of rand() in spawn_enemy().
std::vector<Enemy> is a good way to go for multiple ufos.
Adding code for multiple ufos should still be pretty simple. I'll give some code for this which I think will work. I wish I could test it, but again I don't have SDL installed so I have to just hope it's right!

1) Change 1 line in the Enemy constructor. make alive = false; instead of true (line 260 above). The spawn_enemy() function will have to spawn the 1st ufo. If you left alive = true, then all of the ufos would spawn at once when the program starts. It would look like only 1 ufo spawned, but it would actually be all 10 ufos drawn on top of each other. You would notice it takes 10 hits to kill it, but you're actually killing one ufo with each hit.

2) I'll give all other affected code here. The spawn_enemy() function is new and there are a bunch of changes to the main() function (lines marked as NEW). Save your existing code in case the new code doesn't work!
This code should spawn a ufo every 50 frames. Adjust the value of spawnDelay in spawn_enemies() to change how often they appear.
Up to 10 ufos can be in play at once. Make it more at line 27 if 10 isn't enough (they will stop spawning if they're all in play).

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
void spawn_enemy( std::vector<Enemy>& eVec )
{
    const int spawnDelay = 50;// adjust to change respawn time
    static int spawnTime = 0;// static variable retains its value between function calls
    if( ++spawnTime >= spawnDelay )// time to spawn
    {
        for(unsigned i=0; i<eVec.size(); ++i)// look for ufo to spawn
        {
            if( eVec[i].alive == false )// found one!
            {
                eVec[i].Box.x = rand() % 100;
                eVec[i].Box.y = rand() % 200;
                eVec[i].alive = true;
                break;// quit looking
            }
        }
        spawnTime = 0;// reset for next time
    }
}

int main( int argc, char* args[] )
{
    bool quit = false;
    unsigned int i = 0;// for looping
    Square mySquare(SQUARE_WIDTH, SQUARE_HEIGHT);
    Bullet myBullet(BULLET_WIDTH, BULLET_HEIGHT);
    std::vector<Enemy> enemies( 10, Enemy(ENEMY_WIDTH, ENEMY_HEIGHT) );// NEW
    Timer fps;
    if( init() == false )
    {
        return 1;
    }
    if( load_files() == false )
    {
        return 1;
    }   
    while( quit == false )
    {
        fps.start();
       while( SDL_PollEvent( &event ) )
        {

            mySquare.handle_input();
	    myBullet.handle_input(mySquare.Box.x, mySquare.Box.y);

            if( event.type == SDL_QUIT )
            {
                quit = true;
            }
        }
        
        spawn_enemy( enemies );// NEW
        mySquare.move();
	myBullet.move();
	for(i=0; i<enemies.size(); ++i) enemies[i].move();// NEW

        if( myBullet.alive )// New to line 64
            for(i=0; i<enemies.size(); ++i)
                if( enemies[i].alive && check_collision(myBullet.Box, enemies[i].Box) )
                {
                    myBullet.alive = false;
                    enemies[i].alive = false;
                    break;// only 1 Bullet. Why keep looping?
                }

        apply_surface( 0, 0, background, screen );
        mySquare.show();
	for(i=0; i<enemies.size(); ++i) enemies[i].show();// NEW
	myBullet.show();

        if( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
    }
    clean_up();

    return 0;
}

EDIT: Corrected line 59. Added lines 57 and 63.
Last edited on
Thanks so much fun2code!
Sorry for taking so long to reply...
All of the code in main works but spawn_enemy doesn't here's the compiler errors:

1>c:\users\user\desktop\c++ projects\games\games\Classes.h(427): error C2039: 'vector' : is not a member of 'std'
1>c:\users\user\desktop\c++ projects\games\games\Classes.h(427): error C2065: 'vector' : undeclared identifier
1>c:\users\user\desktop\c++ projects\games\games\Classes.h(427): error C2275: 'Enemy' : illegal use of this type as an expression
1> c:\users\user\desktop\c++ projects\games\games\Classes.h(49) : see declaration of 'Enemy'
1>c:\users\user\desktop\c++ projects\games\games\Classes.h(427): error C2065: 'eVec' : undeclared identifier
1>c:\users\user\desktop\c++ projects\games\games\Classes.h(428): error C2448: 'spawn_enemy' : function-style initializer appears to be a function definition
1>main.cpp(74): fatal error C1004: unexpected end-of-file found


Also i'm working on a menu system and i've changed my code alot so that might make a difference, i'll post all of my code here (also the background doesn't work it's just black):

I'll put it in the next post:
EDIT: I'm going to have to send it to you in a file or have threee seperate posts... (It's in the split up header files)
http://tiny.cc/laurencesrc
Last edited on
closed account (N36fSL3A)
1
2
3
#include <vector>
using std::vector;
// This should do the trick. 


Fixed my post. Sorry, I posted this before I actually reviewed your code.
Last edited on by Fredbill30
closed account (D80DSL3A)
You should either move the spawn_enemy() function back to Main.cpp or #include<vector> in Classes.h.
Also, when you modified my function you broke it!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void spawn_enemy( std::vector<Enemy>& eVec )
{
	if(singleplayer == false) return// you need a ; here
	if(singleplayer == true)
	{         // you forgot to close this brace
    const int spawnDelay = 50;// adjust to change respawn time
    static int spawnTime = 0;// static variable retains its value between function calls
    if( ++spawnTime >= spawnDelay )// time to spawn
    {
        for(unsigned i=0; i<eVec.size(); ++i)// look for ufo to spawn
        {
            if( eVec[i].alive == false )// found one!
            {
                eVec[i].Box.x = rand() % 100;
                eVec[i].Box.y = rand() % 200;
                eVec[i].alive = true;
                break;// quit looking
            }
        }
        spawnTime = 0;// reset for next time
    }
}

You actually don't need the 2nd if condition. This should do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void spawn_enemy( std::vector<Enemy>& eVec )
{
	if(singleplayer == false) return;

    const int spawnDelay = 50;// adjust to change respawn time
    static int spawnTime = 0;// static variable retains its value between function calls
    if( ++spawnTime >= spawnDelay )// time to spawn
    {
        for(unsigned i=0; i<eVec.size(); ++i)// look for ufo to spawn
        {
            if( eVec[i].alive == false )// found one!
            {
                eVec[i].Box.x = rand() % 100;
                eVec[i].Box.y = rand() % 200;
                eVec[i].alive = true;
                break;// quit looking
            }
        }
        spawnTime = 0;// reset for next time
    }
}

Adding #include<vector> to Classes.h should fix most of the compiler errors.
The missing } probably caused the last error
1>main.cpp(74): fatal error C1004: unexpected end-of-file found

I won't try to troubleshoot your background issue due to my unfamiliarity with SDL. Hopefully Fredbill will help you with that.

I'll also point out that your square_check_collision() is identical to the check_collision(). Renaming the 2nd function parameter makes no difference.
There's no reason to have both functions.

EDIT: I'm willing to help with the basic game elements but by adding menus, buttons and single/multi-player modes it becomes more complex than I'm willing to help with here.
Perhaps you could maintain 2 projects. One version stays simple, remaining close to the last code displayed in this thread. The other version uses working code from the simple version and includes all your new features. OK?
Last edited on
Thank you so much fun2code, sorry it took so long to reply!
I've now got a working menu, a hardmode (enemies spawn faster), a multiplayer and loads of other stuff!
You can play the updated version here: http://tiny.cc/laurencegame
Topic archived. No new replies allowed.
Pages: 123