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
|
#include <SDL.h>
class Cube
{
public:
Cube(int x,int y,int w,int h) { xPos = x; yPos = y; width = w; height = h; box.x = xPos; box.y = yPos; box.w = width; box.h = height;}
SDL_Rect box;
private:
int xPos;
int yPos;
int width;
int height;
};
class Enemy
{
public:
Enemy(int x,int y) { xPos = x; yPos = y; width = 25; height = 15; box.x = xPos; box.y = yPos; box.w = width; box.h = height;}
SDL_Rect box;
void SetX(int x) { xPos = x; }
void SetY(int x) { yPos = x; }
private:
int xPos;
int yPos;
int width;
int height;
};
bool collision(SDL_Rect* rect1)
{
if(rect1->x <= 0)
return 1;
if(rect1->x + rect1->w >= 640)
return 1;
if(rect1->y <= 0)
return 1;
if(rect1->y + rect1->h >= 480)
return 1;
return 0;
}
//bool collision(SDL_Rect* rect1, SDL_Rect* rect2)
//{
// if(rect1->x <= rect2->x + rect2->w && rect1->x + rect1->w >= rect2->x && rect1->y <= rect2->y + rect2->h && rect1->y + rect1->h >= rect2->y)
// return 1;
// return 0;
//}
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *screen;
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
bool running = true;
bool b[4] = {0,0,0,0};
Uint32 start;
int unsigned second = 1000;
int unsigned fps = 30;
SDL_Event event;
Uint32 blue = SDL_MapRGB(screen->format,0,0,255);
Uint32 black = SDL_MapRGB(screen->format,0,0,0);
int unsigned enemyX = 150;
int unsigned enemyY = 150;
Cube * Box = new Cube(25,25,50,50);
Enemy * Enemy[1];
for(int unsigned i = 0; i < 1;i++)
{
Enemy[i] = new Enemy(enemyX, enemyY);
}
while(running)
{
start = SDL_GetTicks();
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = false;
break;
case SDLK_LEFT:
b[0] = 1;
break;
case SDLK_RIGHT:
b[1] = 1;
break;
case SDLK_UP:
b[2] = 1;
break;
case SDLK_DOWN:
b[3] = 1;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_LEFT:
b[0] = 0;
break;
case SDLK_RIGHT:
b[1] = 0;
break;
case SDLK_UP:
b[2] = 0;
break;
case SDLK_DOWN:
b[3] = 0;
break;
}
break;
}
}
if(b[0] && collision(&Box->box) == 0){
Box->box.x -= 5;
if(collision(&Box->box) /*| collision(&Box->box,&Box1->box)*/)
Box->box.x += 5;
}
if(b[1] && collision(&Box->box) == 0){
Box->box.x += 5;
if(collision(&Box->box))
Box->box.x -= 5;
}
if(b[2] && collision(&Box->box) == 0){
Box->box.y -= 5;
if(collision(&Box->box))
Box->box.y += 5;
}
if(b[3] && collision(&Box->box) == 0){
Box->box.y += 5;
if(collision(&Box->box))
Box->box.y -= 5;
}
SDL_FillRect(screen,&screen->clip_rect,black);
SDL_FillRect(screen,&Box->box,blue);
for(int unsigned i = 0; i < 1; i++)
{
SDL_FillRect(screen,&Enemy[i]->box,blue);
}
SDL_Flip(screen);
if(second/fps>(SDL_GetTicks()-start))
SDL_Delay(second/fps-(SDL_GetTicks()-start));
}
SDL_QUIT;
return 0;
}
|