Skip to content
Snippets Groups Projects
Commit ab436145 authored by sgupta72's avatar sgupta72
Browse files

Working

parent 8eb9816b
No related branches found
No related tags found
No related merge requests found
all :
g++ main.cpp game.cpp window.cpp rect.cpp -lSDL2 -lSDL2_image
g++ main.cpp game.cpp window.cpp rect.cpp player.cpp platform.cpp entity.cpp enemy.cpp -lSDL2 -lSDL2_image
test :
g++ test.cpp game.cpp window.cpp rect.cpp -lSDL2 -lSDL2_image -o test
g++ test.cpp game.cpp window.cpp rect.cpp player.cpp platform.cpp entity.cpp enemy.cpp -lSDL2 -lSDL2_image -o test
#include "enemy.h"
#include <iostream>
using namespace std;
Enemy::Enemy(Window &window)
{
rect = new Rect("enemy.png", window, 1, 11);
vector<pair<int, int> > enemy_animations;
for(int i = 0; i < 1; i++)
{
for(int j = 0; j < 11; j++)
{
enemy_animations.push_back(make_pair(i, j));
}
}
rect->SetAnimationFrames(enemy_animations);
rect->Move(400, 300);
flipped_rect = new Rect("enemy_flipped.png", window, 1, 11);
vector<pair<int, int> > flipped_enemy_animations;
for(int i = 0; i < 1; i++)
{
for(int j = 10; j >= 0; j--)
{
flipped_enemy_animations.push_back(make_pair(i, j));
}
}
flipped_rect->SetAnimationFrames(enemy_animations);
flipped_rect->Move(400, 300);
vy = 0;
right = true;
}
Enemy::~Enemy()
{
delete rect;
}
void Enemy::Update(vector<Entity*> entities)
{
vy += 10;
if(right)
{
rect->Move(rect->get_x() + 10, rect->get_y() + vy);
flipped_rect->Move(rect->get_x() + 10, rect->get_y() + vy);
}
else
{
rect->Move(rect->get_x() - 10, rect->get_y() + vy);
flipped_rect->Move(rect->get_x() - 10, rect->get_y() + vy);
}
if(rect->get_x() >= 600)
right = false;
else if(rect->get_x() <= 200)
right = true;
for(int i = 0; i < entities.size(); i++)
{
if(rect->CheckCollision(*(entities[i]->get_rect())))
vy = 0;
rect->CorrectPosition(entities[i]->get_rect());
flipped_rect->CorrectPosition(entities[i]->get_rect());
}
}
void Enemy::Draw(Window &window)
{
if(right)
rect->Draw(window);
else
flipped_rect->Draw(window);
}
Rect *Enemy::get_rect()
{
return rect;
}
enemy.h 0 → 100644
#ifndef ENEMY_H
#define ENEMY_H
#include "entity.h"
class Enemy: public Entity
{
public:
Enemy(Window &window);
~Enemy();
void Update(vector<Entity *> entities);
void Draw(Window &window);
Rect *get_rect();
private:
Rect *rect;
Rect *flipped_rect;
int vy;
bool right;
};
#endif
enemy.png 0 → 100644
enemy.png

35.4 KiB

enemy_flipped.png

35.3 KiB

#include "entity.h"
using namespace std;
void Entity::HandleEvents(const SDL_Event &event)
{
}
void Entity::Update(vector<Entity*> &entities)
{
}
void Entity::Draw(Window &window)
{
}
entity.h 0 → 100644
#ifndef ENTITY_H
#define ENTITY_H
#include <SDL2/SDL.h>
#include <vector>
#include "window.h"
using namespace std;
/**
* Base entity class
*/
class Entity
{
public:
virtual ~Entity() {};
virtual void HandleEvents(const SDL_Event &event);
virtual void Update(vector<Entity*> &entities);
virtual void Draw(Window &window);
virtual Rect *get_rect() = 0;
};
#endif
#include "game.h"
#include "window.h"
#include "rect.h"
#include "player.h"
#include "entity.h"
#include "platform.h"
#include "enemy.h"
#include <iostream>
#include <SDL2/SDL_image.h>
......@@ -24,70 +28,40 @@ Game::Game()
void Game::GameLoop()
{
//Create window
Window window;
Rect rect("mario.png", window);
vector<Rect*> rects;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 9; j++)
{
rects.push_back(new Rect("spritesheet.png", window, 4, 9));
rects[rects.size()-1]->Move(300, (9 * i + j) * 64);
}
}
//Create player
Player player(window);
vector<Enemy *> enemies;
enemies.push_back(new Enemy(window));
vector<Entity*> entities;
for(int i = 0; i < 10; i++)
entities.push_back(new Platform(window, i * 120, 500));
while(!quit)
{
while(SDL_PollEvent(&e) != 0)
{
if(e.type == SDL_QUIT)
quit = true;
else if(e.type == SDL_KEYDOWN && e.key.repeat == 0)
{
switch (e.key.keysym.sym)
{
case SDLK_UP:
rect.AddVelocity(0, -10);
break;
case SDLK_DOWN:
rect.AddVelocity(0, 10);
break;
case SDLK_LEFT:
rect.AddVelocity(-10, 0);
break;
case SDLK_RIGHT:
rect.AddVelocity(10, 0);
break;
}
}
else if(e.type == SDL_KEYUP && e.key.repeat == 0)
{
switch (e.key.keysym.sym)
{
case SDLK_UP:
rect.AddVelocity(0, 10);
break;
case SDLK_DOWN:
rect.AddVelocity(0, -10);
break;
case SDLK_LEFT:
rect.AddVelocity(10, 0);
break;
case SDLK_RIGHT:
rect.AddVelocity(-10, 0);
break;
}
}
player.HandleEvents(e);
}
rect.UpdatePosition();
rect.CorrectPosition(rects);
for(int i = 0; i < 4; i++)
window.ClearWindow();
for(int i = 0; i < enemies.size(); i++)
enemies[i]->Update(entities);
player.Update(entities);
for(int i = 0; i < enemies.size(); i++)
{
for(int j = 0; j < 9; j++)
if(player.HandleEnemy(enemies[i]))
{
rects[9 * i + j]->Draw(window, i, j);
delete enemies[i];
enemies.clear();
}
}
rect.Draw(window);
player.Draw(window);
for(int i = 0; i < enemies.size(); i++)
enemies[i]->Draw(window);
for(int i = 0; i < entities.size(); i++)
entities[i]->Draw(window);
window.Update();
SDL_Delay(100);
}
......
mario.png

7.13 KiB | W: | H:

mario.png

8.58 KiB | W: | H:

mario.png
mario.png
mario.png
mario.png
  • 2-up
  • Swipe
  • Onion skin
mario_flipped.png

8.65 KiB

#include "platform.h"
Platform::Platform(Window &window, int x, int y)
{
rect = new Rect("platform.png", window);
rect->Move(x, y);
}
Platform::~Platform()
{
delete rect;
}
void Platform::Draw(Window &window)
{
rect->Draw(window);
}
Rect *Platform::get_rect()
{
return rect;
}
#ifndef PLATFORM_H
#define PLATFORM_H
#include "entity.h"
#include "window.h"
#include "rect.h"
class Platform: public Entity
{
public:
Platform(Window &window, int x, int y);
~Platform();
void Draw(Window &window);
Rect *get_rect();
private:
Rect *rect;
};
#endif
platform.png

5.98 KiB

#include "player.h"
#include <SDL2/SDL.h>
#include <vector>
#include <iostream>
using namespace std;
Player::Player(Window &window)
{
rect = new Rect("mario.png", window, 1, 4);
vector<pair<int, int> > mario_animations;
for(int i = 0; i < 1; i++)
{
for(int j = 0; j < 4; j++)
{
mario_animations.push_back(make_pair(i, j));
}
}
rect->SetAnimationFrames(mario_animations);
flipped_rect = new Rect("mario_flipped.png", window, 1, 4);
vector<pair<int, int> > mario_flipped_animations;
for(int i = 0; i < 1; i++)
{
for(int j = 3; j >= 0; j--)
{
mario_flipped_animations.push_back(make_pair(i, j));
}
}
flipped_rect->SetAnimationFrames(mario_flipped_animations);
jump = false;
flipped = false;
vx = 0;
vy = 0;
}
Player::~Player()
{
delete rect;
}
void Player::HandleEvents(const SDL_Event &e)
{
if(e.type == SDL_KEYDOWN && e.key.repeat == 0)
{
switch (e.key.keysym.sym)
{
case SDLK_UP:
jump = true;
break;
case SDLK_LEFT:
vx -= 20;
flipped = true;
break;
case SDLK_RIGHT:
vx += 20;
flipped = false;
break;
}
}
else if(e.type == SDL_KEYUP && e.key.repeat == 0)
{
switch (e.key.keysym.sym)
{
case SDLK_LEFT:
vx += 20;
break;
case SDLK_RIGHT:
vx -= 20;
break;
}
}
}
void Player::Update(vector<Entity *> entities)
{
//gravity
vy += 10;
//perform jump
if(jump)
{
vy = -50;
jump = false;
}
rect->Move(rect->get_x() + vx, rect->get_y() + vy);
flipped_rect->Move(rect->get_x() + vx, rect->get_y() + vy);
for(int i = 0; i < entities.size(); i++)
{
if(rect->CheckCollision(*(entities[i]->get_rect())))
vy = 0;
rect->CorrectPosition(entities[i]->get_rect());
flipped_rect->CorrectPosition(entities[i]->get_rect());
}
}
bool Player::HandleEnemy(Enemy *enemy)
{
if(vy > 0 && rect->CheckCollision(*(enemy->get_rect())))
return true;
if(vy <= 0 && rect->CheckCollision(*enemy->get_rect()))
cout << "DEAD!!" << endl;
return false;
}
void Player::Draw(Window &window)
{
if(!flipped)
{
if(vx == 0)
rect->Draw(window, 0, 0);
else
rect->Draw(window);
}
else
{
if(vx == 0)
flipped_rect->Draw(window, 0, 3);
else
flipped_rect->Draw(window);
}
}
Rect *Player::get_rect()
{
return rect;
}
player.h 0 → 100644
#ifndef PLAYER_H
#define PLAYER_H
#include "entity.h"
#include "enemy.h"
#include <SDL2/SDL.h>
/**
* Main player class, inherits entity
*/
class Player: public Entity
{
public:
Player(Window &window);
~Player();
void HandleEvents(const SDL_Event &event);
void Update(vector<Entity *> entities);
bool HandleEnemy(Enemy *enemy);
void Draw(Window &window);
Rect *get_rect();
private:
Rect *rect;
Rect *flipped_rect;
bool flipped;
int vx, vy;
bool jump;
};
#endif
......@@ -21,6 +21,8 @@ void Rect::LoadImage(string filename, Window &window)
cout << "Unable to convert surface: " << SDL_GetError() << endl;
}
SDL_FreeSurface(loaded);
animation_frames.push_back(make_pair(0, 0));
current_frame = 0;
}
Rect::Rect(string filename, Window &window)
{
......@@ -29,8 +31,8 @@ Rect::Rect(string filename, Window &window)
y = 0;
w = image->w;
h = image->h;
vx = 0;
vy = 0;
//vx = 0;
//vy = 0;
rows = 1;
columns = 1;
}
......@@ -42,8 +44,8 @@ Rect::Rect(string filename, Window &window, int rows, int columns)
y = 0;
w = image->w/columns;
h = image->h/rows;
vx = 0;
vy = 0;
//vx = 0;
//vy = 0;
this->rows = rows;
this->columns = columns;
}
......@@ -60,31 +62,29 @@ void Rect::Move(int x, int y)
this->y = y;
}
void Rect::AddVelocity(int vx, int vy)
{
this->vx += vx;
this->vy += vy;
}
//void Rect::AddVelocity(int vx, int vy)
//{
// this->vx += vx;
// this->vy += vy;
//}
void Rect::UpdatePosition()
{
x += vx;
y += vy;
}
//void Rect::UpdatePosition()
//{
//x += vx;
//y += vy;
//}
void Rect::Draw(Window &window)
{
SDL_Rect dstrect;
dstrect.x = x;
dstrect.y = y;
SDL_BlitSurface(image, NULL, window.get_screen_surface(), &dstrect);
Draw(window, animation_frames[current_frame].first, animation_frames[current_frame].second);
current_frame = (current_frame + 1) % animation_frames.size();
}
void Rect::Draw(Window &window, int row, int column)
{
SDL_Rect srcrect;
srcrect.x = (double(row)/double(rows)) * (image->h/rows);
srcrect.y = (double(column)/double(columns)) * (image->w / columns);
srcrect.x = double(column) * (image->h/rows);
srcrect.y = double(row) * (double(image->w) / double(columns));
srcrect.w = image->w/columns;
srcrect.h = image->h/rows;
SDL_Rect dstrect;
......@@ -102,18 +102,36 @@ bool Rect::CheckCollision(Rect &rect)
return true;
}
void Rect::CorrectPosition(Rect* rect)
{
if(CheckCollision(*rect))
{
//if(vy > 0)
y = rect->get_y() - h;
//else if(vy < 0)
// y = rect->get_y() + rect->get_h();
}
}
void Rect::CorrectPosition(vector<Rect*> rects)
{
for(int i = 0; i < rects.size(); i++)
{
if(CheckCollision(*rects[i]))
{
x -= vx;
y -= vy;
break;
//x -= vx;
//if(vx > 0)
// x = rects[i]->get_x() - w;
//else if(vx < 0)
// x = rects[i]->get_x() + rects[i]->get_w();
//y -= vy;
//if(vy > 0)
y = rects[i]->get_y() - h;
//else if(vy < 0)
// y = rects[i]->get_y() + rects[i]->get_h();
}
}
}
int Rect::get_x()
{
return x;
......@@ -123,7 +141,23 @@ int Rect::get_y()
{
return y;
}
int Rect::get_w()
{
return w;
}
int Rect::get_h()
{
return h;
}
SDL_Surface *Rect::get_image()
{
return image;
}
void Rect::SetAnimationFrames(vector<pair<int, int> > animation_frames)
{
this->animation_frames = animation_frames;
}
......@@ -18,7 +18,7 @@ public:
* load spritesheet
* filename: filaname of image file
* window: window to draw on
**/
*/
Rect(string filename, Window &window);
/**
* load spritesheet
......@@ -26,21 +26,21 @@ public:
* window: window to draw on
* rows: rows in spritesheet
* column: columns in spritesheet
**/
*/
Rect(string filename, Window &window, int rows, int columns);
~Rect();
/**
* move rect to x, y coordinate
**/
*/
void Move(int x, int y);
/**
* add velocity to movement
**/
void AddVelocity(int vx, int vy);
//void AddVelocity(int vx, int vy);
/**
* update position of rect
**/
void UpdatePosition();
//void UpdatePosition();
/**
* draw the image
* window: window to draw on
......@@ -58,6 +58,11 @@ public:
* rect: rectangle ot check against
**/
bool CheckCollision(Rect &rect);
/**
* move back if colliding with rect in rects
* rect: rect to check against
**/
void CorrectPosition(Rect *rect);
/**
* move back if colliding with rect in rects
* rects: rect vector to check against
......@@ -71,6 +76,13 @@ public:
int get_x();
int get_y();
int get_w();
int get_h();
//set frames to use for animation
void SetAnimationFrames(vector<pair<int, int> > animation_frames);
private:
/**
* actually load image
......@@ -81,7 +93,8 @@ private:
SDL_Surface *image;
int x, y;
int w, h;
int vx, vy;
int rows, columns;
vector<pair<int, int> > animation_frames;
int current_frame;
};
#endif
......@@ -25,6 +25,11 @@ Window::~Window()
window = NULL;
}
void Window::ClearWindow()
{
SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0xFF, 0xFF, 0xFF));
}
SDL_Surface *Window::get_screen_surface()
{
return screen_surface;
......
......@@ -13,6 +13,7 @@ public:
~Window();
//get screen surface to draw on, etc.
SDL_Surface *get_screen_surface();
void ClearWindow();
//update window
void Update();
private:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment