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
|
class Bullet
{
public:
static const int BULLET_WIDTH = 15;
static const int BULLET_HEIGHT = 25;
static const int BULLET_VEL = 5;
Bullet();
//xFire, yFire - position relative to player
void handle_input(int xFire, int yFire);
void bullet_render(); //renders bullet
void bullet_move(); //moves bullet
bool isAlive;
private:
//bullet offsets
int bullet_x, bullet_y;
//velocity of bullets
int velX, velY;
};
std::vector<Bullet> bullets;
Bullet::Bullet()
{
isAlive = false;
bullet_x = 0;
bullet_y = 0;
velX = 0;
velY = BULLET_VEL;
}
void Bullet::handle_input(int xFire, int yFire)
{
if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_z)
{
Bullet b;
bullet_x = xFire;
bullet_y = yFire;
isAlive = true;
bullets.push_back(b);
}
}
void Bullet::bullet_move()
{
if(isAlive == false) return; //do nothing
for(int i = 0; i < bullets.size(); i++)
{
bullets[i].bullet_x += velX;
bullets[i].bullet_y -= velY;
}
//if bullet goes off screen, delete
for(int i = 0; i < bullets.size(); i++)
{
bullet[i].bullet_y -= velY;
if((bullets[i].bullet_y < 0) || (bullets[i].bullet_y + BULLET_HEIGHT > SCREEN_HEIGHT))
{
isAlive = false;
bullets.erase(bullets.begin() + i);
}
}
}
void Bullet::bullet_render()
{
if(isAlive == true)
{
for(int i = 0; i < bullets.size(); i++)
{
apply_surface(bullets[i].bullet_x, bullets[i].bullet_y, bullet_fire, screen);
}
}
}
int main(int argc, char* args[])
{
bool quit = false;
//offsets of bg
int bgX = 0;
int bgY = 0;
Player player(SCREEN_WIDTH/2 - (55/2), SCREEN_HEIGHT - 65);
Player player2(50, 50);
Bullet zbullet;
Timer fps;
//initialize
if(init() == false)
{
return 1;
}
//load the files
if(load_files() == false)
{
return 1;
}
while(quit == false)
{
fps.start();
while(SDL_PollEvent(&event))
{
player.handle_input();
zbullet.handle_input(player.x+20, player.y);
if(event.type == SDL_QUIT)
{
quit = true;
}
}
//scrolls background
bgY += 2;
if(bgY >= background->h)
{
bgY = 0;
}
//renders text
message = TTF_RenderText_Solid(font, "SCORE", textColor);
if(message == NULL)
{
return 1;
}
//shows bg and text
apply_surface(bgX, bgY, background, screen);
apply_surface(bgX, bgY - background->h, background, screen);
apply_surface(10, 0, message, screen);
//moves objects
player.move(player2.get_rects());
zbullet.bullet_move();
//shows objects
zbullet.bullet_render();
player.render();
player2.render();
if(SDL_Flip(screen) == -1)
{
return 1;
}
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
clean_up();
return 0;
}
|