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
|
//--------------------------ENEMY CLASS--------------------------------//
class Enemy{
public:
Enemy(int x,int y)
{
xPos = x;
yPos = y;
width = 50;
height = 15;
box.x = xPos;
box.y = yPos;
box.w = width;
box.h = height;
}
SDL_Rect box;
int getVisible() { return visible; }
void setVisible(int x) { visible = x; }
private:
int xPos;
int yPos;
int width;
int height;
bool visible = true;
};
//-----------------------COLLISION DETECTION---------------------------//
bool collision(SDL_Rect* rec1, SDL_Rect* rec2)
{
if(rec1->y >= rec2->y + rec2->h)
{
return false;
}
if(rec1->y + rec1->h <= rec2->y)
{
return false;
}
if(rec1->x >= rec2->x + rec2->w)
{
return false;
}
if(rec1->x + rec1->w <= rec2->x)
{
return false;
}
return true;
}
//-----------------------GLOBAL VARIABLES------------------------------//
void arrayMove();
void moveDown();
void velSwap(unsigned int * X, unsigned int * Y);
int unsigned const opponents = 18;
int unsigned tmpEnemyVel;
int unsigned tmpNEnemyVel;
Enemy * opponent[opponents];
int unsigned const lastOpponent = (opponents - 1);
bool movedown = false;
int unsigned tickCount = 2;
//-----------------------------MAIN------------------------------------//
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *screen, *ship, *enemy, *bullet;
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
ship = SDL_DisplayFormat(SDL_LoadBMP("ship1.bmp"));
SDL_SetColorKey(ship, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
enemy = SDL_DisplayFormat(SDL_LoadBMP("enemy2.bmp"));
SDL_SetColorKey(enemy, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
bullet = SDL_DisplayFormat(SDL_LoadBMP("bullet.bmp"));
SDL_SetColorKey(bullet, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
bool running = true;
bool b[3] = {0,0,0};
bool spent = false;
bool shot = false;
Uint32 start;
int unsigned second = 1000;
int unsigned fps = 60;
SDL_Event event;
Uint32 black = SDL_MapRGB(screen->format,0,0,0);
Cube * Box = new Cube(125,450,50,35);
SDL_Rect ammo; ammo.x = ((Box->box.x + (Box->box.w / 2)) - 6); ammo.y = Box->box.y; ammo.w =9; ammo.h =10;
int tmpTimer = fps;
int unsigned enemyVel = 5;
int unsigned NEnemyVel = -5;
tmpNEnemyVel = NEnemyVel;
tmpEnemyVel = enemyVel;
int unsigned paddleVel = 5;
int unsigned i = 0;
int unsigned k = 6;
int unsigned enemyX = 80;
int unsigned enemyY = 30;
for(int unsigned row = 0; row < (opponents / 6); row++ )
{
for(; i < k; i++ )
{
opponent[i] = new Enemy(enemyX,enemyY);
enemyX = enemyX + 85;
}
enemyX = 80;
enemyY += 35;
k += 6;
}
while(running)
{
//--------------------------ENEMY MOVEMENT-----------------------------//
tmpTimer -= 3;
if(tmpTimer == 0)
{
arrayMove();
tmpTimer = fps;
tickCount ++;
}
if(movedown){
moveDown();
}
//--------------------------BULLET MOVEMENT----------------------------//
if(b[2]){
shot = true;
ammo.y -= 15;
}
if(ammo.y <= 0)
spent = true;
if(spent){
spent = false;
shot = false;
ammo.x = ((Box->box.x + (Box->box.w / 2)) - 6);
ammo.y = Box->box.y;
b[2] = 0;
}
for(int unsigned v = 0; v < opponents; v++)
{
if(collision(&ammo, &opponent[v]->box)){
opponent[v]->setVisible(0);
spent = true;
delete &opponent[v]->box;
}
}
//--------------------------SHIP MOVEMENT------------------------------//
if(b[0]){
Box->box.x -= paddleVel;
if(!shot){
ammo.x -= paddleVel;
}
if(Box->box.x <= 0){
Box->box.x += paddleVel;
}
}
if(b[1]){
Box->box.x += paddleVel;
if(!shot){
ammo.x += paddleVel;
}
if(Box->box.x + Box->box.w >= 640){
Box->box.x -= paddleVel;
}
}
//----------------------------RENDER-----------------------------------//
SDL_FillRect(screen,&screen->clip_rect,black);
if(!spent){
SDL_BlitSurface(bullet,NULL,screen,&ammo);
}
SDL_BlitSurface(ship, NULL, screen, &Box->box);
for(int unsigned j = 0; j < opponents; j++)
{
if(opponent[j]->getVisible()){
SDL_BlitSurface(enemy, NULL, screen, &opponent[j]->box);
}
}
SDL_Flip(screen);
//------------------------------FPS------------------------------------//
if(second/fps>(SDL_GetTicks()-start))
SDL_Delay(second/fps-(SDL_GetTicks()-start));
}
//---------------------------TERMINATE---------------------------------//
SDL_FreeSurface(screen);
SDL_FreeSurface(ship);
SDL_FreeSurface(enemy);
SDL_FreeSurface(bullet);
SDL_Quit();
return 0;
}
//--------------------------FUNCTIONS-----------------------------------//
void arrayMove()
{
for(int unsigned m = 0; m < opponents; m++)
{
if(tickCount == 12){
velSwap(&tmpEnemyVel, &tmpNEnemyVel);
movedown = true;
tickCount = 0;
}
opponent[m]->box.x -= tmpEnemyVel;
}
}
void moveDown()
{
for(int unsigned n = 0; n < opponents; n++)
{
opponent[n]->box.y += 25;
movedown = false;
}
}
void velSwap(unsigned int * X, unsigned int * Y)
{
int temp;
temp = *X;
*X = *Y;
*Y = temp;
}
|