Original Post
Hi all! As in my previous post, I have this little game with two people moving... But how can I make so one cant go into the other and the other cant go into the first? I tried letting the first move only if its height(i think) doesnt equal the other's height.. but that also restricted the first from going under or over the other! Here's my code: Any help and examples are apreciated. I'm using Dev-CPP.
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
#include "SDL_image.h"
SDL_Surface *screen;
void Draw(int x, int y, SDL_Surface *sprite);
int main(int nArgs, char **args)
{
Uint8* keys;
SDL_Surface *bg;
bool isRunning;
int height = 0, width = 0;
int height2 = 0, width2 = 780;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
atexit(SDL_Quit);
screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL)
return 0;
bg = IMG_Load("bg.bmp");
if (bg == NULL)
return 0;
SDL_Surface *mario1;
mario1 = IMG_Load("mario1.bmp");
if (mario1 == NULL)
return 0;
SDL_Surface *mario2;
mario2 = IMG_Load("mario2.bmp");
if (mario2 == NULL)
return 0;
SDL_Surface *luigi1;
luigi1 = IMG_Load("luigi1.bmp");
if (luigi1 == NULL)
return 0;
SDL_Surface *luigi2;
luigi2 = IMG_Load("luigi2.bmp");
if (luigi2 == NULL)
return 0;
bool MARIO1 = true, LUIGI1 = true;
isRunning = true;
SDL_SetColorKey(mario1, SDL_SRCCOLORKEY, SDL_MapRGB(mario1->format, 0, 255, 255));
SDL_SetColorKey(luigi1, SDL_SRCCOLORKEY, SDL_MapRGB(luigi1->format, 0, 255, 255));
SDL_SetColorKey(mario2, SDL_SRCCOLORKEY, SDL_MapRGB(mario2->format, 0, 255, 255));
SDL_SetColorKey(luigi2, SDL_SRCCOLORKEY, SDL_MapRGB(luigi2->format, 0, 255, 255));
while(isRunning)
{
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
isRunning = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
isRunning = false;
break;
}
break;
}
keys = SDL_GetKeyState(NULL);
if (keys[SDLK_UP])
{
if (height > 0)
height -= 5;
}
if (keys[SDLK_DOWN])
{
if (height < 380)
height += 5;
}
if (keys[SDLK_LEFT])
{
if (width > 0)
width -= 5;
MARIO1 = false;
}
if (keys[SDLK_RIGHT])
{
if (width < 780)
width += 5;
MARIO1 = true;
}
if (keys[SDLK_KP8])
{
if (height2 > 0)
height2 -= 4;
}
if (keys[SDLK_KP5])
{
if (height2 < 380)
height2 += 4;
}
if (keys[SDLK_KP4])
{
if (width2 > 0)
width2 -= 4;
LUIGI1 = false;
}
if (keys[SDLK_KP6])
{
if (width2 < 780)
width2 += 4;
LUIGI1 = true;
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
Draw(0, 0, bg);
if (MARIO1)
Draw(width, height, mario1);
else
Draw(width, height, mario2);
if (LUIGI1)
Draw(width2, height2, luigi1);
else
Draw(width2, height2, luigi2);
SDL_Flip(screen);
}
SDL_FreeSurface(bg);
SDL_FreeSurface(mario1);
SDL_FreeSurface(luigi1);
SDL_FreeSurface(mario2);
SDL_FreeSurface(luigi2);
return 0;
}
void Draw(int x, int y, SDL_Surface *sprite)
{
SDL_Rect source;
SDL_Rect dest;
source.x = 0;
source.y = 0;
source.w = sprite->w;
source.h = sprite->h;
dest.x = x;
dest.y = y;
dest.w = source.w;
dest.h = source.h;
SDL_BlitSurface(sprite, &source, screen, &dest);
return;
}