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;
}
}
|