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

Working

parent ab436145
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -5,8 +5,10 @@ using namespace std;
Enemy::Enemy(Window &window)
{
//populate rect with enemy image
rect = new Rect("enemy.png", window, 1, 11);
vector<pair<int, int> > enemy_animations;
//set up animations
for(int i = 0; i < 1; i++)
{
for(int j = 0; j < 11; j++)
......@@ -15,7 +17,9 @@ Enemy::Enemy(Window &window)
}
}
rect->SetAnimationFrames(enemy_animations);
//move to initial position
rect->Move(400, 300);
//do the same with flipped rect
flipped_rect = new Rect("enemy_flipped.png", window, 1, 11);
vector<pair<int, int> > flipped_enemy_animations;
for(int i = 0; i < 1; i++)
......@@ -27,7 +31,9 @@ Enemy::Enemy(Window &window)
}
flipped_rect->SetAnimationFrames(enemy_animations);
flipped_rect->Move(400, 300);
//initially, y velocity is 0
vy = 0;
//initially pointing to right
right = true;
}
......@@ -38,25 +44,32 @@ Enemy::~Enemy()
void Enemy::Update(vector<Entity*> entities)
{
//gravity
vy += 10;
if(right)
{
//move to right
rect->Move(rect->get_x() + 10, rect->get_y() + vy);
flipped_rect->Move(rect->get_x() + 10, rect->get_y() + vy);
}
else
{
//move to left
rect->Move(rect->get_x() - 10, rect->get_y() + vy);
flipped_rect->Move(rect->get_x() - 10, rect->get_y() + vy);
}
//too far to right
if(rect->get_x() >= 600)
right = false;
//too far to left
else if(rect->get_x() <= 200)
right = true;
for(int i = 0; i < entities.size(); i++)
{
//colliding with entity? -> stop moving down
if(rect->CheckCollision(*(entities[i]->get_rect())))
vy = 0;
//correct position after collision
rect->CorrectPosition(entities[i]->get_rect());
flipped_rect->CorrectPosition(entities[i]->get_rect());
}
......@@ -64,6 +77,7 @@ void Enemy::Update(vector<Entity*> entities)
void Enemy::Draw(Window &window)
{
//draw rect or flipped rect depending on direction
if(right)
rect->Draw(window);
else
......
......@@ -2,6 +2,9 @@
#define ENEMY_H
#include "entity.h"
/**
* Main enemy class, inherits entity class
*/
class Enemy: public Entity
{
public:
......@@ -11,9 +14,12 @@ public:
void Draw(Window &window);
Rect *get_rect();
private:
//store rect and the flipped version flipped rect
Rect *rect;
Rect *flipped_rect;
//store y velocity of enemy
int vy;
//is enemy going left or right?
bool right;
};
#endif
......@@ -37,32 +37,41 @@ void Game::GameLoop()
vector<Entity*> entities;
for(int i = 0; i < 10; i++)
entities.push_back(new Platform(window, i * 120, 500));
//while the quit button hasn't been pressed
while(!quit)
{
//handle input
while(SDL_PollEvent(&e) != 0)
{
if(e.type == SDL_QUIT)
quit = true;
player.HandleEvents(e);
}
//clear window
window.ClearWindow();
//update positions of enemies and player
for(int i = 0; i < enemies.size(); i++)
enemies[i]->Update(entities);
player.Update(entities);
//handle logic
for(int i = 0; i < enemies.size(); i++)
{
if(player.HandleEnemy(enemies[i]))
{
delete enemies[i];
enemies.clear();
enemies.push_back(new Enemy(window));
}
}
//draw enemies, players and other entities
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);
//update the window
window.Update();
//wait for 100 ms
SDL_Delay(100);
}
}
......
......@@ -2,6 +2,7 @@
Platform::Platform(Window &window, int x, int y)
{
//create platform at (x, y)
rect = new Rect("platform.png", window);
rect->Move(x, y);
}
......@@ -17,5 +18,6 @@ void Platform::Draw(Window &window)
}
Rect *Platform::get_rect()
{
//get rectangle of platform
return rect;
}
......@@ -7,6 +7,7 @@ using namespace std;
Player::Player(Window &window)
{
//set up right facing rect and animations
rect = new Rect("mario.png", window, 1, 4);
vector<pair<int, int> > mario_animations;
for(int i = 0; i < 1; i++)
......@@ -17,6 +18,7 @@ Player::Player(Window &window)
}
}
rect->SetAnimationFrames(mario_animations);
//set up left facing rect and 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++)
......@@ -27,8 +29,11 @@ Player::Player(Window &window)
}
}
flipped_rect->SetAnimationFrames(mario_flipped_animations);
//not jumping
jump = false;
//facing right
flipped = false;
//no velocity
vx = 0;
vy = 0;
}
......@@ -44,13 +49,16 @@ void Player::HandleEvents(const SDL_Event &e)
{
switch (e.key.keysym.sym)
{
//pressed up -> jump
case SDLK_UP:
jump = true;
break;
//move to left
case SDLK_LEFT:
vx -= 20;
flipped = true;
break;
//move to right
case SDLK_RIGHT:
vx += 20;
flipped = false;
......@@ -61,9 +69,11 @@ void Player::HandleEvents(const SDL_Event &e)
{
switch (e.key.keysym.sym)
{
//stop moving left
case SDLK_LEFT:
vx += 20;
break;
//stop moving right
case SDLK_RIGHT:
vx -= 20;
break;
......@@ -81,8 +91,10 @@ void Player::Update(vector<Entity *> entities)
vy = -50;
jump = false;
}
//move rects
rect->Move(rect->get_x() + vx, rect->get_y() + vy);
flipped_rect->Move(rect->get_x() + vx, rect->get_y() + vy);
//handle collisions
for(int i = 0; i < entities.size(); i++)
{
if(rect->CheckCollision(*(entities[i]->get_rect())))
......@@ -94,10 +106,13 @@ void Player::Update(vector<Entity *> entities)
bool Player::HandleEnemy(Enemy *enemy)
{
//killed enemy!
if(vy > 0 && rect->CheckCollision(*(enemy->get_rect())))
return true;
//died!
if(vy <= 0 && rect->CheckCollision(*enemy->get_rect()))
cout << "DEAD!!" << endl;
//nothing happened
return false;
}
void Player::Draw(Window &window)
......
......@@ -13,16 +13,20 @@ class Player: public Entity
public:
Player(Window &window);
~Player();
//handle events
void HandleEvents(const SDL_Event &event);
void Update(vector<Entity *> entities);
bool HandleEnemy(Enemy *enemy);
void Draw(Window &window);
Rect *get_rect();
private:
//rect when facing right, flipped rect when facing left
Rect *rect;
Rect *flipped_rect;
bool flipped;
//x and y velocity
int vx, vy;
//did player jump?
bool jump;
};
......
......@@ -9,18 +9,21 @@ using namespace std;
void Rect::LoadImage(string filename, Window &window)
{
//load image into surface
SDL_Surface *loaded = IMG_Load(filename.c_str());
if(loaded == NULL)
{
cout << "Unable to load image: " << filename << " " << IMG_GetError() << endl;
return;
}
//convert to window format
image = SDL_ConvertSurface(loaded, window.get_screen_surface()->format, NULL);
if(image == NULL)
{
cout << "Unable to convert surface: " << SDL_GetError() << endl;
}
SDL_FreeSurface(loaded);
//set up animation
animation_frames.push_back(make_pair(0, 0));
current_frame = 0;
}
......@@ -31,8 +34,6 @@ Rect::Rect(string filename, Window &window)
y = 0;
w = image->w;
h = image->h;
//vx = 0;
//vy = 0;
rows = 1;
columns = 1;
}
......@@ -44,8 +45,6 @@ Rect::Rect(string filename, Window &window, int rows, int columns)
y = 0;
w = image->w/columns;
h = image->h/rows;
//vx = 0;
//vy = 0;
this->rows = rows;
this->columns = columns;
}
......@@ -62,34 +61,26 @@ 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::UpdatePosition()
//{
//x += vx;
//y += vy;
//}
void Rect::Draw(Window &window)
{
Draw(window, animation_frames[current_frame].first, animation_frames[current_frame].second);
//go to next frame
current_frame = (current_frame + 1) % animation_frames.size();
}
void Rect::Draw(Window &window, int row, int column)
{
SDL_Rect srcrect;
//set up src rect to clip
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;
//set up dstrect to draw on
SDL_Rect dstrect;
dstrect.x = x;
dstrect.y = y;
//draw
SDL_BlitSurface(image, &srcrect, window.get_screen_surface(), &dstrect);
}
......@@ -106,10 +97,7 @@ 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)
......@@ -118,16 +106,8 @@ void Rect::CorrectPosition(vector<Rect*> rects)
{
if(CheckCollision(*rects[i]))
{
//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)
//move above colliding object
y = rects[i]->get_y() - h;
//else if(vy < 0)
// y = rects[i]->get_y() + rects[i]->get_h();
}
}
}
......
#include <cassert>
#include "rect.h"
#include "player.h"
#include "enemy.h"
#include "window.h"
using namespace std;
void TestRect()
......@@ -7,27 +10,29 @@ void TestRect()
Window window;
Rect rect("mario.png", window);
Rect rect2("mario.png", window);
rect.move(5, 5);
rect.Move(5, 5);
assert(rect.get_x() == 5);
assert(rect.get_y() == 5);
rect2.move(400, 400);
rect2.Move(400, 400);
assert(rect2.get_x() == 400);
assert(rect2.get_y() == 400);
assert(!rect.CheckCollision(rect2));
rect2.move(6, 6);
rect2.Move(6, 6);
assert(rect2.get_x() == 6);
assert(rect2.get_y() == 6);
assert(rect.CheckCollision(rect2));
rect.AddVelocity(1, 1);
for(int i = 0; i < 10; i++)
{
rect.UpdatePosition();
assert(rect.get_x() == (5 + (i+1)));
assert(rect.get_y() == (5 + i + 1));
}
}
void TestPlayerEnemy()
{
Window window;
Player player(window);
Enemy enemy(window);
assert(!player.HandleEnemy(&enemy));
}
int main()
{
TestRect();
TestPlayerEnemy();
}
......@@ -7,6 +7,7 @@ using namespace std;
Window::Window()
{
//create window
window = SDL_CreateWindow("Platformer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
if(window == NULL)
{
......@@ -15,18 +16,21 @@ Window::Window()
}
cout << "HERE" << endl;
screen_surface = SDL_GetWindowSurface(window);
//fill it with white
SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0xFF, 0xFF, 0xFF));
SDL_UpdateWindowSurface(window);
}
Window::~Window()
{
//destructor of window
SDL_DestroyWindow(window);
window = NULL;
}
void Window::ClearWindow()
{
//clear the window with white
SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0xFF, 0xFF, 0xFF));
}
......@@ -37,5 +41,6 @@ SDL_Surface *Window::get_screen_surface()
void Window::Update()
{
//update window
SDL_UpdateWindowSurface(window);
}
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