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
|
#include <sdl.h>
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *sprite = NULL;
SDL_Surface *sprite2 = NULL;
SDL_Surface *sprite3 = NULL;
SDL_Surface *sprite4 = NULL;
SDL_Surface *sprite5 = NULL;
SDL_Surface *sprite6 = NULL;
//Integers
// Health Bar
int hwidth = 140;
int hheight = 30;
int hwidth2 = 140;
int hheight2 = 30;
int hwidth3= 140;
int healthx1 = 400;
int healthx2 = 100;
int healthy = 400;
// Gun right
int width = 200;
int height = 200;
int srcX = 0;
int srcY = 0;
int dstX = 530;
int dstY = 150;
// Bullet 1
int widthb1 = 20;
int heightb1 = 20;
int bullet1startX;
int bullet1startY;
// Gun Left
int width2 = 200;
int height2 = 200;
int srcX2 = 0;
int srcY2 = 0;
int dstX2 = -80;
int dstY2 = 150;
// Bullet 2
int widthb2 = 20;
int heightb2 = 20;
int bullet2startX;
int bullet2startY;
int bulletspeed = 1;
// Making an Image for the background
bool run = true;
SDL_Event event;
void surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
SDL_BlitSurface (source, NULL, destination, &rect);
}
void draw_sprite(int srcX,int srcY,
int dstX,int dstY,
int width,int height,
SDL_Surface *source, SDL_Surface *destination)
{
SDL_Rect src;
src.x = srcX;
src.y = srcY;
src.w = width;
src.h = height;
SDL_Rect dst;
dst.x = dstX;
dst.y = dstY;
dst.w = width;
dst.h = height;
SDL_BlitSurface(source, &src, destination, &dst);
}
int main ( int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption ("Gun Fight",NULL); // Sets caption
//Load
screen = SDL_SetVideoMode (640,440,32,SDL_SWSURFACE); //Establishes Screen
background = SDL_LoadBMP("background.bmp"); // Loads the bmp
sprite = SDL_LoadBMP("sprite.bmp"); // Loads the bmp
sprite2 = SDL_LoadBMP("sprite2.bmp"); // Loads the bmp
sprite3 = SDL_LoadBMP("sprite3.bmp"); // Loads the bmp
sprite4 = SDL_LoadBMP("sprite4.bmp"); // Loads the bmp
sprite5 = SDL_LoadBMP("sprite5.bmp"); // Loads the bmp
sprite6 = SDL_LoadBMP("sprite6.bmp"); // Loads the bmp
SDL_SetColorKey (sprite,SDL_SRCCOLORKEY,SDL_MapRGB (sprite ->format, 255, 255, 255)); //takes away any of set color
SDL_SetColorKey (sprite2,SDL_SRCCOLORKEY,SDL_MapRGB (sprite2 ->format, 255, 255, 255)); //takes away any of set color
SDL_SetColorKey (sprite6,SDL_SRCCOLORKEY,SDL_MapRGB (sprite6 ->format, 255, 255, 255));
bool keys[323] = {false};
bool shoot1 = {false};
bool shoot2 = {false};
while (run)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
run = false;
}
if(event.type == SDL_KEYDOWN) //If any key is pressed down
{
keys[event.key.keysym.sym] = true;
}
if(event.type == SDL_KEYUP)
{
keys[event.key.keysym.sym] = false;
}
}
// Key presses
// Right Gun
if (keys[SDLK_UP])
{
dstY -= 1;
}
if (keys[SDLK_DOWN])
{
dstY += 1;
}
if ( keys[SDLK_LEFT] & shoot1 == false )
{
shoot1 = true;
bullet1startX = dstX;
bullet1startY = dstY;
}
// Left Gun
if ( keys[SDLK_w] )
{
dstY2 -= 1;
}
if ( keys[SDLK_s] )
{
dstY2 += 1;
}
if ( keys[SDLK_d] & shoot2 == false )
{
shoot2 = true;
bullet2startX = dstX2;
bullet2startY = dstY2;
}
// Screen Edges
if (dstY < 0)
{
dstY = 0;
}
if (dstY > 360)
{
dstY = 360;
}
if (dstY2 < 0)
{
dstY2 = 0;
}
if (dstY2 > 360)
{
dstY2 = 360;
}
surface(0,0,background,screen);
if ( shoot1 == true )
{
if ( bullet1startX > 0 - widthb1 )
{
draw_sprite(srcX,srcY,
bullet1startX,bullet1startY,
widthb1,heightb1,
sprite4, screen);
bullet1startX -= bulletspeed;
}
else
{
shoot1 = false;
}
}
if ( shoot2 == true )
{
if ( bullet2startX < 640 - widthb1 )
{
draw_sprite(srcX2,srcY2,
bullet2startX,bullet2startY,
widthb2,heightb2,
sprite3, screen);
bullet2startX += bulletspeed;
}
else
{
shoot2 = false;
}
}
draw_sprite(srcX,srcY,
healthx1,healthy,
hwidth3,hheight,
sprite5, screen);
draw_sprite(srcX,srcY,
healthx1,healthy,
hwidth,hheight,
sprite6, screen);
draw_sprite(srcX,srcY,
healthx2,healthy,
hwidth2,hheight2,
sprite5, screen);
draw_sprite(srcX,srcY,
healthx2,healthy,
hwidth,hheight,
sprite6, screen);
draw_sprite(srcX,srcY,
dstX,dstY,
width,height,
sprite, screen);
draw_sprite(srcX2,srcY2,
dstX2,dstY2,
width2,height2,
sprite2, screen);
// Hit Detection
if ( bullet2startX + widthb2 > dstX && dstY < bullet2startY && dstY + heightb2 > bullet2startY && shoot2 == true )
{
if (hwidth3 == 28)
{
hwidth3 = 0;
}
if (hwidth3 == 56)
{
hwidth3 = 28;
}
if (hwidth3 == 84)
{
hwidth3 = 56;
}
if (hwidth3 == 112)
{
hwidth3 = 84;
}
if (hwidth3 == 140)
{
hwidth3 = 112;
}
bullet2startX = 1000;
}
if ( bullet1startX + widthb1 > dstX2 && dstY2 < bullet1startY && dstY2 + heightb1 > bullet1startY && shoot1 == true )
{
if (hwidth2 == 28)
{
hwidth2 = 0;
}
if (hwidth2 == 56)
{
hwidth2 = 28;
}
if (hwidth2 == 84)
{
hwidth2 = 56;
}
if (hwidth2 == 112)
{
hwidth2 = 84;
}
if (hwidth2 == 140)
{
hwidth2 = 112;
}
bullet1startX = -1000;
}
SDL_Flip (screen);
}
SDL_Quit();
return 0;
}
|