SDL simple game change level

Hey guys,


have been learning c++ for about half a year and have decided to create a simple 2d game using the SDL library. Although the code is pretty messy and not really structured into classes, I have been able to create an extremely simple box game where you have to get to the end of the level and can touch other blocks to apply certain attributes to yourself, such as going smaller or bigger. I have also added a collision engine and simple sounds which change when you change size. My only issue is that when I reach the end of the level and touch the block I am not sure how to delete all of the previous rectangles and draw on new one's to symbolize a new level.

Here is my code, but it is quite messy:

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
#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"


//collision engine function
bool collision(SDL_Rect* rect1, SDL_Rect* rect2)
{
    if(rect1->y >= rect2-> y + rect2->h)
      return false;
    if(rect1->x >= rect2->x + rect2->w)
    return false;

   if(rect1->y + rect1->h <= rect2->y)
   return false;
   if(rect1->x + rect1->w <= rect2->x)
   return false;

  return true;
}



int main(int argc, char* argv[])
{

//initialise SDL
SDL_Init(SDL_INIT_EVERYTHING);

//initialise screen surface
SDL_Surface *screen;

Mix_Music* music;
Mix_Music* music2;
Mix_Music* music3;

Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096);

music = Mix_LoadMUS("atmospace_jungle.wav");
music2 = Mix_LoadMUS("partytrain.wav");
music3 = Mix_LoadMUS("lost_in_euroland.wav");

Mix_PlayMusic(music,-1);





//set screen attributes (res,bpp etc.)
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);


//set name of program in top right corner
SDL_WM_SetCaption("Block Maze", NULL);

//set running to true
bool running = true;


  //states of the player sorted by size
  bool normal = true;
  bool shrunk = false;
  bool huge = false;


  //set game FPS
  int FPS = 90;

//set 'start' to an integer
  Uint32 start;

//bool variable for whether or not a key is pressed
bool b[7] = {0,0,0,0,0,0,0};

bool destroyed[2] = {1,0};


//rectangle position and size for the player
  SDL_Rect rect;
  rect.x = 10;
  rect.y = 10;
  rect.w = 20;
  rect.h = 20;


 //position and size for the block which shrinks the player
  SDL_Rect shrinkblock;
  shrinkblock.x = 20;
  shrinkblock.y = 110;
  shrinkblock.w = 20;
  shrinkblock.h = 20;

//position and size for the invisible line which makes the player bigger
  SDL_Rect enlargeblock;
  enlargeblock.x = 300;
  enlargeblock.y = 200;
  enlargeblock.w = 10;
  enlargeblock.h = 50;

//position and size for the block that enlarges the player 10x10
  SDL_Rect supersize;
  supersize.x = 20;
  supersize.y = 215;
  supersize.w = 20;
  supersize.h = 20;

//size and position for the wall that cannot be passed unless the player is super size
  SDL_Rect destructiblewall;
  destructiblewall.x = 600;
  destructiblewall.y = 250;
  destructiblewall.w = 39;
  destructiblewall.h = 50;


//array to hold the normal white blocks in the game
  SDL_Rect rectarray[6];

//block arrays
  rectarray[0] = {0,40, 600, 50};
  rectarray[1] = {550, 150, 140, 50};
  rectarray[2] = {0, 150, 539, 50};
  rectarray[3] = {0, 250, 600, 50};
  rectarray[4] = {639,250,1,50};
  rectarray[5] = {50, 315, 590, 50};

  bool changesong[1] = {0};








//black -> screen
  Uint32 color = SDL_MapRGB(screen->format,0x00,0x00,0x00);

//white -> white walls
  Uint32 color2 = SDL_MapRGB(screen->format,0xFF,0xFF,0xFF);

//Dark Red -> player
Uint32 color3 = SDL_MapRGB(screen->format,125,40,60);

//random purple -> shrink block
Uint32 color4 = SDL_MapRGB(screen->format,150,25,225);

//Light green -> destructive wall
Uint32 color5 = SDL_MapRGB(screen->format,150,255,100);

//Dark blue -> super size block
Uint32 color6 = SDL_MapRGB(screen->format,10,2,100);

Uint32 color7 = SDL_MapRGB(screen->format,150,0,0);





//for loop display the walls on the screen
  for(int i = 0; i < 6; i++){
  SDL_FillRect(screen,&rectarray[i],color2);
  }



//start of game loop
  while(running)
  {

      //start is equal to the time
      start = SDL_GetTicks();

      //name event
      SDL_Event event;

      if(changesong[0])
      {

          changesong[0] = 0;

  if(normal)
      Mix_PlayMusic(music,-1);

      if(shrunk)
      Mix_PlayMusic(music2,-1);

      if(huge)
      Mix_PlayMusic(music3,-1);
      }



      //while there is an event to handle
      while(SDL_PollEvent(&event))
      {

          //switch event types
          switch(event.type)
          {

              //if quit is pressed
              case SDL_QUIT:
                //set running to false -> exits game loop
              running = false;
              break;


            //if a key is pressed
            case SDL_KEYDOWN:
            switch(event.key.keysym.sym)
            {

                //if up set b[0] to true
                case SDLK_UP:
                b[0] = 1;
                break;

                //if left set b[1] to true
                case SDLK_LEFT:
                b[1] = 1;
                break;

                //if down set b[2] to true
                case SDLK_DOWN:
                b[2] = 1;
                break;

                //if right set b[3] to true
                case SDLK_RIGHT:
                b[3] = 1;
                break;

                //if r set b[4] to true
                case SDLK_r:
                b[4] = 1;
                break;

                //if f set b[5] to true
                case SDLK_f:
                b[5] = 1;
                break;

                case SDLK_h:
                b[6] = 1;
                break;



            }
            break;


         //when a key is un-pressed set the keys to false
        case SDL_KEYUP:
        switch(event.key.keysym.sym)
        {
              case SDLK_UP:
                b[0] = 0;
                break;

                case SDLK_LEFT:
                b[1] = 0;
                break;

                case SDLK_DOWN:
                b[2] = 0;
                break;

                case SDLK_RIGHT:
                b[3] = 0;
                break;

                case SDLK_r:
                b[4] = 0;
                break;

                 case SDLK_f:
                b[5] = 0;
                break;

                case SDLK_h:
                b[6] = 0;
                break;


        }
        break;


          }


      }


 



Thanks for all your help and sorry but, due to the word count, i had to split the code in half :(
Last edited on
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
    


      //make the player the native color
      SDL_FillRect(screen,&rect,color);







    //if up is held down
    if(b[0])
    {

       //move square up
        rect.y--;
       //check for collision
          for(int i = 0; i < 6; i++)
          if(collision(&rect,&rectarray[i]))
          rect.y++;


        //if collision with the shrink block
        if(collision(&rect,&shrinkblock))
          {
              rect.y++;

              //if the player is of normal size, shrink him/her
              if(normal)
              {

             rect.w -= 10;
             rect.h -= 10;
             normal = 0;
             shrunk = 1;
             changesong[0] = 1;
              }

          }


        //if collision with the enlargeblock
        if(collision(&rect,&enlargeblock))
{

            //if player is small, make them bigger
              if(shrunk)
              {
                  if(rect.y >= 225)
                  {
                      rect.y = 220;
                  }
                  rect.w += 10;
             rect.h += 10;
             shrunk = 0;
             normal = 1;

              changesong[0] = 1;



              }

}

   //if collision with the super size block
   if(collision(&rect,&supersize))
   {
       rect.y++;

       //if the player is of normal size, make them bigger else leave the same
       if(normal)
       {
           if(rect.y >= 220)
                  {
                      rect.y = 215;
                  }
           rect.w += 10;
           rect.h += 10;
           normal = 0;
           huge = 1;

            changesong[0] = 1;
       }
   }

   //if collision with destructible wall

      if(collision(&rect,&destructiblewall))
   {

       //if player is normal or shrunk, dont let them pass
       if((normal) || (shrunk))
       if(destroyed[0])
       rect.y++;

         else if(huge)
       {
           destroyed[0] = 0;
           destroyed[1] = 1;
       }

   }


        else if (rect.y <= -1 )
        rect.y++;

    }

    if(b[1])
    {
          rect.x--;
          for(int i = 0; i < 6; i++)
          if(collision(&rect,&rectarray[i]))
          rect.x++;

       if(collision(&rect,&shrinkblock))
       {
           rect.x++;
           if(normal)
           {

             rect.w -= 10;
             rect.h -= 10;
             normal = 0;
             shrunk = 1;

             changesong[0] = 1;
           }



         }

                 if(collision(&rect,&enlargeblock))
{
 if(shrunk)
              {

                  if(rect.y >= 225)
                  {
                      rect.y = 220;
                  }
                  rect.w += 10;
             rect.h += 10;
             shrunk = 0;
             normal = 1;
              changesong[0] = 1;
              }
}

   if(collision(&rect,&supersize))
   {
       rect.x++;
       if(normal)
       {
           if(rect.y >= 220)
                  {
                      rect.y = 215;
                  }
           rect.w += 10;
           rect.h += 10;
           normal = 0;
           huge = 1;
            changesong[0] = 1;
       }
   }
      if(collision(&rect,&destructiblewall))
   {
       if((normal) || (shrunk))
       if(destroyed[0])
       rect.x++;

         else if(huge)
       {
           destroyed[0] = 0;
           destroyed[1]= 1;
       }

   }

        else if(rect.x <= -1 )
        rect.x++;

    }

    if(b[2])
    {
         rect.y++;


       for(int i = 0; i < 6; i++)
          if(collision(&rect,&rectarray[i]))
          rect.y--;

         if(collision(&rect,&shrinkblock))
         {
                rect.y--;
             if(normal)
             {

             rect.w -= 10;
             rect.h -= 10;
             normal = 0;
             shrunk = 1;

             changesong[0] = 1;

         }
         }

            if(collision(&rect,&enlargeblock))
{

            if(shrunk)
              {
                  if(rect.y >= 225)
                  {
                      rect.y = 220;
                  }
            rect.w += 10;
             rect.h += 10;
             shrunk = 0;
             normal = 1;
              changesong[0] = 1;
              }
}

   if(collision(&rect,&supersize))
   {
       rect.y--;

       if(normal)
       {
           if(rect.y >= 220)
                  {
                      rect.y = 215;
                  }
           rect.w += 10;
           rect.h += 10;
           normal = 0;
           huge = 1;
            changesong[0] = 1;
       }
   }

 if(collision(&rect,&destructiblewall))
   {
       if((normal) || (shrunk))
       if(destroyed[0])
       rect.y--;

         else if(huge)
       {
             destroyed[0]= 0;
           destroyed[1] = 1;

       }

   }

        else if(rect.y + rect.h >= 480 )
        rect.y--;

 }




    if(b[3])
   {
        rect.x++;
  for(int i = 0; i < 6; i++)
          if(collision(&rect,&rectarray[i]))
          rect.x--;

         if(collision(&rect,&shrinkblock))
{

    rect.x--;
          if(normal)
          {

             rect.w -= 10;
             rect.h -= 10;
             shrunk = 1;
             normal = 0;

             changesong[0] = 1;
          }
}

        if(collision(&rect,&enlargeblock))
{

             if(shrunk)
              {
                  if(rect.y >= 225)
                  {
                      rect.y = 220;
                  }
                  rect.w += 10;
             rect.h += 10;
             shrunk = 0;
             normal = 1;
              changesong[0] = 1;
              }
}

   if(collision(&rect,&supersize))
   {
       rect.x--;
       if(normal)
       {
           if(rect.y >= 225)
                  {
                      rect.y = 220;
                  }
           rect.w += 10;
           rect.h += 10;
           normal = 0;
           huge = 1;
            changesong[0] = 1;
       }
   }

   if(collision(&rect,&destructiblewall))
   {
       if((normal) || (shrunk))
       {
        if(destroyed[0])
       rect.x--;
       }

       else if(huge)
       {
           destroyed[0] = 0;
           destroyed[1] = 1;
       }

   }

         else if(rect.x + rect.w >= 641 )
        rect.x--;
   }

   if(b[4])
   FPS += 10;

   if(b[5])
   if(FPS > 10)
   FPS -= 10;

   if(b[6])
   {
       rect.x = 10;
       rect.y = 10;
       rect.w = 20;
       rect.h = 20;

       normal = 1;
       shrunk = 0;
       huge = 0;
        changesong[0] = 1;


   }


   if(destroyed[0])
   SDL_FillRect(screen,&destructiblewall, color5);

   else if(destroyed[1])
      SDL_FillRect(screen,&destructiblewall, color7);


SDL_FillRect(screen,&supersize, color6);
          SDL_FillRect(screen,&shrinkblock,color4);


    SDL_FillRect(screen,&rect,color3);



    SDL_Flip(screen);
    if(1000/FPS>SDL_GetTicks()-start)
    SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  }

  Mix_FreeMusic(music);
  Mix_CloseAudio();

  SDL_Quit();

  return 0;
}


Thanks again
Last edited on
Please, I need help :D
I don't think you are going to find anyone willing to read 700 lines of code. Try reducing your code to about 20 lines explaining just how you make the current level and we can go from there.
Ok thanks for that Strewbong, will end the post and repost later on when I have enough time to display the general structure of the program.

:D
Hmmm, a 700-line main function and you're finding it difficult to add new features... ;)
I think Bjarne himself would be in the same situation.
The first thing to do is to get this sorted out into smaller functions or preferably some classes, then you might find that the solution presents itself.
Good luck!
Topic archived. No new replies allowed.