Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"functions.h"
- using namespace std;
- class Projectile;
- vector<Projectile> AllProjectiles;
- static bool quit = false;
- SDL_Event event;
- SDL_Surface* screen = NULL;
- TTF_Font* font = NULL;
- SDL_Rect backgroundStats = {0, 0, 800, 475};
- int score = 0;
- enum ProjectileType
- {
- Lazer = 2,
- Rocket = 5,
- };
- enum Direction
- {
- LEFT = 1,
- RIGHT
- };
- class Projectile
- {
- private:
- int personalID;
- static int ID;
- int x, y, xVel, damage, frame, maxFrame;
- ProjectileType type;
- SDL_Surface* sprite;
- SDL_Rect* animLeft;
- SDL_Rect* animRight;
- void defineAnim();
- Direction direction;
- public:
- Projectile(ProjectileType Type, int X, int Y, int XVel);
- Projectile(const Projectile& proj);
- ~Projectile();
- void Move();
- void HandleAnimation();
- void Show();
- int getID(){ return personalID; }
- int getObjCount(){ return ID; }
- static void removeObjCount(){ ID--; }
- SDL_Rect getRect();
- };
- int Projectile::ID = 0;
- SDL_Rect Projectile::getRect()
- {
- SDL_Rect temp = {x, y, 94, 20};
- return temp;
- }
- Projectile::Projectile(ProjectileType Type, int X, int Y, int XVel):
- x(X),
- y(Y),
- xVel(XVel),
- type(Type),
- damage(Type * 10),
- personalID(ID++),
- frame(0)
- {
- if(xVel)
- {
- direction = RIGHT;
- }
- else
- {
- direction = LEFT;
- }
- if(Type == Lazer)
- {
- sprite = loadImage("Lazer.png");
- maxFrame = 1;
- }
- else if(Type == Rocket)
- {
- sprite = loadImage("Rocket.png");
- maxFrame = 4;
- }
- animLeft = new SDL_Rect[maxFrame];
- animRight = new SDL_Rect[maxFrame];
- defineAnim();
- }
- Projectile::Projectile(const Projectile& proj)
- {
- this->x = proj.x;
- this->y = proj.y;
- this->xVel = proj.xVel;
- this->damage = proj.damage;
- this->frame = proj.frame;
- this->type = proj.type;
- this->direction = proj.direction;
- displayTTF("COPY CONSTRUCTPOR!", 255, 255, 255);
- SDL_Flip(screen);
- SDL_Delay(2000);
- if(type == Lazer)
- {
- sprite = loadImage("Lazer.png");
- maxFrame = 1;
- }
- else if(type == Rocket)
- {
- sprite = loadImage("Rocket.png");
- maxFrame = 4;
- }
- animLeft = new SDL_Rect[maxFrame];
- animRight = new SDL_Rect[maxFrame];
- defineAnim();
- }
- void Projectile::Move()
- {
- x += xVel;
- if(xVel)
- {
- direction = RIGHT;
- }
- else if(xVel < 0)
- {
- direction = LEFT;
- }
- }
- void Projectile::Show()
- {
- if(xVel < 0)
- {
- blitSurface(x, y, sprite, &animLeft[frame]);
- }
- else if(xVel > 0)
- {
- blitSurface(x, y, sprite, &animRight[frame]);
- }
- }
- void Projectile::HandleAnimation()
- {
- frame = (SDL_GetTicks() / 150) % maxFrame;
- if(frame > maxFrame)
- {
- frame = 1;
- }
- }
- void Projectile::defineAnim()
- {
- if(type == Lazer)
- {
- if(xVel < 0)
- {
- animLeft[0].x = 0; animLeft[1].x = 0; animLeft[2].x = 0; animLeft[3].x = 0;
- animLeft[0].y = 0; animLeft[1].y = 0; animLeft[2].y = 0; animLeft[3].y = 0;
- animLeft[0].w = 20; animLeft[1].w = 20; animLeft[2].w = 20; animLeft[3].w = 20;
- animLeft[0].h = 6; animLeft[1].h = 6; animLeft[2].h = 6; animLeft[3].h = 6;
- }
- else if(xVel > 0)
- {
- animRight[0].x = 0; animRight[1].x = 0; animRight[2].x = 0; animRight[3].x = 0;
- animRight[0].y = 0; animRight[1].y = 0; animRight[2].y = 0; animRight[3].y = 0;
- animRight[0].w = 20; animRight[1].w = 20; animRight[2].w = 20; animRight[3].w = 20;
- animRight[0].h = 6; animRight[1].h = 6; animRight[2].h = 6; animRight[3].h = 6;
- }
- }
- else if(type == Rocket)
- {
- if(xVel < 0)
- {
- animLeft[0].x = 96; animLeft[1].x = 96; animLeft[2].x = 96; animLeft[3].x = 96;
- animLeft[0].y = 1; animLeft[1].y = 25; animLeft[2].y = 48; animLeft[3].y = 71;
- animLeft[0].w = 94; animLeft[1].w = 94; animLeft[2].w = 94; animLeft[3].w = 94;
- animLeft[0].h = 20; animLeft[1].h = 20; animLeft[2].h = 20; animLeft[3].h = 20;
- }
- else if(xVel > 0)
- {
- animRight[0].x = 2; animRight[1].x = 2; animRight[2].x = 2; animRight[3].x = 2;
- animRight[0].y = 1; animRight[1].y = 25; animRight[2].y = 48; animRight[3].y = 71;
- animRight[0].w = 94; animRight[1].w = 94; animRight[2].w = 94; animRight[3].w = 94;
- animRight[0].h = 20; animRight[1].h = 20; animRight[2].h = 20; animRight[3].h = 20;
- }
- }
- }
- Projectile::~Projectile()
- {
- SDL_FreeSurface(sprite);
- delete[] animLeft;
- delete[] animRight;
- animLeft = NULL;
- animRight = NULL;
- }
- void SpawnProjectile(ProjectileType type, int x, int y, int xVel)
- {
- Projectile proj(type, x, y, xVel);
- AllProjectiles.push_back(proj);
- }
- class Player
- {
- private:
- int x, y, w, h, yVel, health;
- SDL_Surface* sprite;
- ProjectileType type;
- public:
- Player(int X, int Y, int W, int H);
- ~Player();
- void handleEvents();
- void move();
- void show();
- SDL_Rect getRect();
- };
- SDL_Rect Player::getRect()
- {
- SDL_Rect temp = {x, y, w, h};
- return temp;
- }
- Player::Player(int X, int Y, int W, int H):
- x(X),
- y(Y),
- w(W),
- h(H),
- yVel(0),
- health(100)
- {
- sprite = loadImage("Superman.png");
- type = Lazer;
- }
- Player::~Player()
- {
- SDL_FreeSurface(sprite);
- }
- void Player::handleEvents()
- {
- if(event.type == SDL_KEYDOWN)
- {
- switch(event.key.keysym.sym)
- {
- case(SDLK_w): yVel--; break;
- case(SDLK_s): yVel++; break;
- case(SDLK_r): if(type == 2){ type = Rocket; } else{ type = Lazer; } break;
- }
- }
- else if(event.type == SDL_KEYUP)
- {
- switch(event.key.keysym.sym)
- {
- case(SDLK_w): yVel++; break;
- case(SDLK_s): yVel--; break;
- }
- }
- static int lastClicked = SDL_GetTicks();
- if(event.type == SDL_MOUSEBUTTONDOWN)
- {
- if(event.button.button == SDL_BUTTON_LEFT)
- {
- if(SDL_GetTicks() - lastClicked >= (type * 1000))
- {
- SpawnProjectile(type, x + 100, y, 1);
- lastClicked = SDL_GetTicks();
- }
- }
- }
- }
- void Player::move()
- {
- y += yVel;
- if((y < 0) || (y + h > (SCREEN_HEIGHT - 50)))
- {
- y -= yVel;
- }
- }
- void Player::show()
- {
- blitSurface(x, y, sprite);
- displayTTF("Weapon: ", 0, SCREEN_HEIGHT - 25);
- if(type == Lazer)
- {
- displayTTF("Lazer beams", 90, SCREEN_HEIGHT - 25);
- }
- if(type == Rocket)
- {
- displayTTF("Rockets", 90, SCREEN_HEIGHT - 25);
- }
- displayTTF("Health:", 230, SCREEN_HEIGHT - 25);
- if(health >= 80)
- {
- displayTTF(health, 310, SCREEN_HEIGHT - 25, 0, 255, 0);
- }
- if(health < 80 && health > 60)
- {
- displayTTF(health, 310, SCREEN_HEIGHT - 25, 128, 255, 0);
- }
- if(health < 60 && health > 40)
- {
- displayTTF(health, 310, SCREEN_HEIGHT - 25, 255, 128, 0);
- }
- if(health < 40 && health > 20)
- {
- displayTTF(health, 310, SCREEN_HEIGHT - 25, 244, 80, 0);
- }
- if(health < 20)
- {
- displayTTF(health, 310, SCREEN_HEIGHT - 25, 255, 0, 0);
- }
- }
- int main(int argv, char* argc[])
- {
- register vector<Projectile>::iterator it;
- if(init("Space Game!"))
- {
- return 1;
- }
- SDL_Surface* background = loadImage("Background.jpg");
- Player one(15, 50, 156, 47);
- while(!quit)
- {
- while(SDL_PollEvent(&event))
- {
- if(event.type == SDL_QUIT)
- {
- quit = true;
- }
- one.handleEvents();
- }
- clearScreen();
- backgroundStats.x--;
- if( backgroundStats.x <= -backgroundStats.w )
- {
- backgroundStats.x = 0;
- }
- blitSurface(backgroundStats.x, backgroundStats.y, background);
- blitSurface(backgroundStats.x + backgroundStats.w, backgroundStats.y, background);
- for(it = AllProjectiles.begin(); it != AllProjectiles.end(); it++)
- {
- it->Move();
- it->HandleAnimation();
- it->Show();
- SDL_Rect one = it->getRect();
- if(one.x < 0 || (one.x + one.w) > SCREEN_WIDTH)
- {
- it = AllProjectiles.erase(it);
- }
- else
- {
- ++it;
- }
- }
- one.move();
- one.show();
- displayTTF("_______________________________________________________________________________", 0, SCREEN_HEIGHT - 50);
- displayTTF(it->getObjCount(), 370, SCREEN_HEIGHT - 25);
- displayTTF("Projectiles", 410, SCREEN_HEIGHT - 25);
- displayTTF("Score:", 530, SCREEN_HEIGHT - 25);
- displayTTF(score, 590, SCREEN_HEIGHT - 25);
- SDL_Flip(screen);
- SDL_Delay(1);
- }
- clearUp();
- return 0;
- }
- //FUNCTIONS.H
- #ifndef _functions_H_
- #define _functions_H_
- #include"SDL/SDL.h"
- #include"SDL/SDL_mixer.h"
- #include"SDL/SDL_image.h"
- #include"SDL/SDL_ttf.h"
- #include<string>
- #include<vector>
- #include<cmath>
- #include<sstream>
- const int SCREEN_WIDTH = 800;
- const int SCREEN_HEIGHT = 500;
- const int BPP = 32;
- int init( std::string caption );
- double calculateDistance( double x1, double x2, double y1, double y2 );
- void blitSurface(int srcX, int srcY, SDL_Surface* source, SDL_Rect* clip = NULL);
- void displayTTF( std::string text, int x, int y, int red = 0, int green = 0, int blue = 0);
- void displayTTF( int num, int x, int y, int red = 0, int green = 0, int blue = 0);
- int checkCollison( SDL_Rect* rect1, SDL_Rect* rect2 );
- SDL_Surface* loadImage( std::string path );
- void clearScreen();
- void clearUp();
- #endif
- //FUNCTIONS.CPP
- #include"functions.h"
- extern SDL_Event event;
- extern SDL_Surface* screen;
- extern TTF_Font* font;
- int init( std::string caption )
- {
- if( TTF_Init() == -1 )
- {
- return 1;
- }
- font = TTF_OpenFont("arial.ttf", 20);
- if( SDL_Init(SDL_INIT_EVERYTHING) == -1 )
- {
- return 1;
- }
- if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
- {
- return 1;
- }
- if( (screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, BPP, SDL_SWSURFACE )) == NULL )
- {
- return 1;
- }
- SDL_WM_SetCaption( caption.c_str(), NULL );
- return 0;
- }
- double calculateDistance( double x1, double x2, double y1, double y2 )
- {
- return sqrt( (x1 - x2)*(x1-x2) + (y2-y1)*(y2-y1) );
- }
- void blitSurface(int srcX, int srcY, SDL_Surface* source, SDL_Rect* clip )
- {
- SDL_Rect offsets;
- offsets.x = srcX;
- offsets.y = srcY;
- SDL_BlitSurface( source, clip, screen, &offsets );
- }
- void displayTTF( std::string text, int x, int y, int red, int green, int blue)
- {
- SDL_Color colour = { red, green, blue };
- SDL_Surface* temp = TTF_RenderText_Solid( font, text.c_str(), colour );
- blitSurface( x, y, temp );
- SDL_FreeSurface( temp );
- temp = NULL;
- }
- void displayTTF( int num, int x, int y, int red, int green, int blue)
- {
- SDL_Color colour = { red, green, blue };
- std::stringstream stream;
- stream << num;
- SDL_Surface* temp = TTF_RenderText_Solid( font, stream.str().c_str(), colour );
- blitSurface( x, y, temp );
- SDL_FreeSurface( temp );
- temp = NULL;
- }
- int checkCollison( SDL_Rect* rect1, SDL_Rect* rect2 )
- {
- if(rect1->y >= rect2->y + rect2->h)
- return 0;
- if(rect1->x >= rect2->x + rect2->w)
- return 0;
- if(rect1->y + rect1->h <= rect2->y)
- return 0;
- if(rect1->x + rect1->w <= rect2->x)
- return 0;
- return 1;
- }
- SDL_Surface* loadImage( std::string path )
- {
- SDL_Surface* loadedImage = NULL;
- SDL_Surface* optimizedImage = NULL;
- loadedImage = IMG_Load( path.c_str() );
- if( loadedImage != NULL )
- {
- optimizedImage = SDL_DisplayFormat( loadedImage );
- SDL_FreeSurface( loadedImage );
- }
- if( optimizedImage != NULL )
- {
- Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 255, 0, 255 );
- SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
- }
- return optimizedImage;
- }
- void clearScreen()
- {
- SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 255, 255, 255 ) );
- }
- void clearUp()
- {
- TTF_CloseFont( font );
- SDL_Quit();
- TTF_Quit();
- Mix_CloseAudio();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement