diff --git a/BaseObject.cpp b/BaseObject.cpp index f325f2b..00b173e 100644 --- a/BaseObject.cpp +++ b/BaseObject.cpp @@ -1,20 +1,20 @@ #include "BaseObject.h" -Vector Vector::operator = (const Vector& rhs) { +Vector Vector::operator = (const Vector& rhs) { _x = rhs._x; _y = rhs._y; return *this; } -Vector Vector::operator + (const Vector& rhs) const { +Vector Vector::operator + (const Vector& rhs) const { return Vector(_x + rhs._x, _y + rhs._y); } -Vector Vector::operator - (const Vector& rhs) const { +Vector Vector::operator - (const Vector& rhs) const { return Vector(_x - rhs._x, _y - rhs._y); } -Vector Vector::operator * (const double rhs) const { +Vector Vector::operator * (const double rhs) const { return Vector(_x * rhs, _y * rhs); } -Vector Vector::operator / (const double rhs) const { +Vector Vector::operator / (const double rhs) const { return Vector(_x / rhs, _y / rhs); } Vector Vector::operator += (const Vector& rhs) { @@ -33,15 +33,15 @@ Vector Vector::operator /= (const double rhs) { *this = *this / rhs; return *this; } -bool Vector::operator == (const Vector& rhs) const { +bool Vector::operator == (const Vector& rhs) const { if(_x == rhs._x && _y == rhs._y) return true; else return false; } -bool Vector::operator == (const double rhs) const { +bool Vector::operator == (const double rhs) const { if(_x == rhs && _y == rhs) return true; else return false; } -bool Vector::operator != (const Vector& rhs) const { +bool Vector::operator != (const Vector& rhs) const { if(*this == rhs) return false; else return true; } @@ -51,14 +51,14 @@ void Vector::x(double x) { _x = x; } void Vector::y(double y) { _y = y; } void Vector::set(double x, double y) { _x = x, _y = y; } -void Figure::addFigure(string name, vector imgs, vector masks, function tigger) { +void Figure::addFigure(string name, vector imgs, vector masks, function tigger) { if(figures.empty() && !imgs.empty()) { _width = imgs.front().getwidth(); _height = imgs.front().getheight(); } figures.push_back(FigureData{name, imgs, masks, tigger}); } -void Figure::update(int x, int y) { +void Figure::update(int x, int y) { for(auto& i : figures) { if(i.tigger()) { @@ -81,7 +81,7 @@ void Figure::update(int x, int y) { } } } -void Figure::turn() { +void Figure::turn() { for(auto& i : figures) { for(auto& j : i.images) rotateFlip(&j); @@ -92,36 +92,61 @@ string Figure::status() { return _status; } -void Figure::status(string s) +void Figure::status(string s) { _status = s; } -int Figure::width() { +int Figure::width() { return _width; } -int Figure::height() { +int Figure::height() { return _height; } -int BaseObject::width() { +int BaseObject::counter = 0; + +int BaseObject::width() { return _width; } -int BaseObject::height() { +int BaseObject::height() { return _height; } -void BaseObject::width(int w) { +int BaseObject::get_id() +{ + return id; +} +string BaseObject::type() +{ + return _type; +} +void BaseObject::width(int w) { _width = w; } -void BaseObject::height(int h) { +void BaseObject::height(int h) { _height = h; } -void BaseObject::update(double time) { +void BaseObject::type(string t) +{ + _type = t; +} +void BaseObject::update(double time) { + if(killed)return; position += (velocity * time + acceleration * time * time / 2); //position += velocity * time; velocity += acceleration * time; } -void BaseObject::show(Vector& offset) { +void BaseObject::show(Vector& offset) { + figure.update(static_cast(round(position.x()) + offset.x()), static_cast(round(position.y()) + offset.y())); } +void BaseObject::kill() +{ + killed = true; +} + +bool BaseObject::is_killed() +{ + return killed == true; +} diff --git a/BaseObject.h b/BaseObject.h index f69f120..100b353 100644 --- a/BaseObject.h +++ b/BaseObject.h @@ -84,25 +84,42 @@ class Figure { int height(); }; - class BaseObject { private: int _width; int _height; + string _type; + int id; + static int counter; + bool killed; public: Position position; Velocity velocity; Acceleration acceleration; Figure figure; - BaseObject():_width(0),_height(0),position(), velocity(), acceleration(0, GRAVITY), figure(){} + BaseObject(): + id(counter++),_width(0),_height(0),_type(),position(), velocity(), acceleration(0, GRAVITY), figure(), killed(0) {} + BaseObject(const double x, const double y): + id(counter++), _width(0), _height(0), _type(), position(x,y), velocity(), acceleration(0, GRAVITY), figure(), killed(0) {} + BaseObject(const double x, const double y, int width, int height): + id(counter++), _width(width), _height(height), _type(), position(x, y), velocity(), acceleration(0, GRAVITY), figure(), killed(0) {} + BaseObject(const double x, const double y, int width, int height, string type): + id(counter++), _width(width), _height(height), _type(type), position(x, y), velocity(), acceleration(0, GRAVITY), figure(), killed(0) {} + ~BaseObject() { counter--; } int side_crash = 0; int width(); int height(); + int get_id(); + string type(); void width(int w); void height(int h); + void type(string t); void update(double time); void show(Vector& offset); + virtual void kill(); + bool is_killed(); + virtual void init() = 0; }; diff --git a/Bullet.cpp b/Bullet.cpp new file mode 100644 index 0000000..22865c3 --- /dev/null +++ b/Bullet.cpp @@ -0,0 +1 @@ +#include "Bullet.h" diff --git a/Bullet.h b/Bullet.h new file mode 100644 index 0000000..e65759e --- /dev/null +++ b/Bullet.h @@ -0,0 +1,7 @@ +#pragma once +#include "BaseObject.h" +class Bullet: + public BaseObject +{ +}; + diff --git a/Controller.cpp b/Controller.cpp new file mode 100644 index 0000000..01fc74c --- /dev/null +++ b/Controller.cpp @@ -0,0 +1,40 @@ +#include "Controller.h" + +void Controller::play_music(string type) +{ + if(type == "welcome") { + mciSendString(_T("close all"), NULL, 0, NULL);//ֹͣ + mciSendString(_T("open \"assets\\audios\\bg.mp3\" alias welcome"), NULL, 0, NULL);//ӭ + mciSendString(_T("play welcome repeat"), NULL, 0, NULL);//ѭ + } + else if(type == "gaming") { + mciSendString(_T("close all"), NULL, 0, NULL);//ֹͣ + mciSendString(_T("open \"assets\\audios\\bg.mp3\" alias gaming"), NULL, 0, NULL);// + mciSendString(_T("play gaming"), NULL, 0, NULL);//ѭ + } + else if(type == "accele") { + mciSendString(_T("stop gaming"), NULL, 0, NULL);//ֹͣ + mciSendString(_T("open \"assets\\audios\\bg_accele.mp3\" alias accele"), NULL, 0, NULL);//ּ + mciSendString(_T("play accele"), NULL, 0, NULL);//ѭ + } + else if(type == "jump") { + mciSendString(_T("close jump"), NULL, 0, NULL); + mciSendString(_T("open \"assets\\audios\\jump.mp3\" alias jump"), NULL, 0, NULL);//Ծ + mciSendString(_T("play jump"), NULL, 0, NULL);//ѭ + } + else if(type == "win") { + mciSendString(_T("close all"), NULL, 0, NULL);//ֹͣ + mciSendString(_T("open \"assets\\audios\\win.mp3\" alias win"), NULL, 0, NULL);//ʤ + mciSendString(_T("play win"), NULL, 0, NULL);//ѭ + } + else if(type == "mario death") { + mciSendString(_T("close all"), NULL, 0, NULL);//ֹͣ + mciSendString(_T("open \"assets\\audios\\mario_death.mp3\" alias mario_death"), NULL, 0, NULL);//ʧ + mciSendString(_T("play mario_death"), NULL, 0, NULL);//ѭ + } + else if(type == "enemy death") { + mciSendString(_T("close enemy_death"), NULL, 0, NULL); + mciSendString(_T("open \"assets\\audios\\enemy_death.mp3\" alias enemy_death"), NULL, 0, NULL);//Ұ + mciSendString(_T("play enemy_death"), NULL, 0, NULL);//ѭ + } +} diff --git a/Controller.h b/Controller.h new file mode 100644 index 0000000..e6f96f9 --- /dev/null +++ b/Controller.h @@ -0,0 +1,26 @@ +#pragma once + +#ifndef _CONTROLLER_H_ +#define _CONTROLLER_H_ +#include "Global.h" +#include +#pragma comment(lib,"Winmm.lib") +#include + +class Controller +{ + private: + + public: + void welcome(); + void win(); + void lose(); + void play_music(string type); + void show_welcome_ui(); + void show_gaming_ui(); + void show_lose_ui(); + void show_help_ui(); +}; + + +#endif \ No newline at end of file diff --git a/Enemy.cpp b/Enemy.cpp new file mode 100644 index 0000000..8f6c15d --- /dev/null +++ b/Enemy.cpp @@ -0,0 +1,81 @@ +#include "Enemy.h" + +void Enemy::init() +{ + LPCTSTR img_src, mask_src; + img_src = _T("assets\\images\\enemy.png"); + mask_src = _T("assets\\images\\enemy_mask.png"); + int figure_num = 4; + IMAGE origin, organ_mask; + loadimage(&origin, img_src); + loadimage(&organ_mask, mask_src); + int width = origin.getwidth() / figure_num; + int height = origin.getheight(); + this->width(width); + this->height(height); + vector imgs, masks; + IMAGE temp; + + imgs.clear(); masks.clear(); + for(int i = 0; i < 2; i++) { + SetWorkingImage(&origin); + getimage(&temp, 0 + width * i, 0, width, height); + imgs.push_back(temp); + SetWorkingImage(&organ_mask); + getimage(&temp, 0 + width * i, 0, width, height); + masks.push_back(temp); + } + figure.addFigure("running", imgs, masks, [this]()->bool { + if(!is_killed()) return true; + return false; + }); + + + + imgs.clear(); masks.clear(); + for(int i = 2; i < 4; i++) { + SetWorkingImage(&origin); + getimage(&temp, 0 + width * i, 0, width, height); + imgs.push_back(temp); + SetWorkingImage(&organ_mask); + getimage(&temp, 0 + width * i, 0, width, height); + masks.push_back(temp); + } + figure.addFigure("killed", imgs, masks, [this]()->bool { + if(is_killed()) return true; + return false; + }); + SetWorkingImage(NULL); + +} + +void Enemy::kill() +{ + BaseObject::kill(); + kill_tick = steady_clock::now(); +} + + + +void Enemy::move_range(double a, double b) +{ + range_left = a; + range_right = b; +} + +double Enemy::move_range_left() +{ + return range_left; +} + +double Enemy::move_range_right() +{ + return range_right; +} + +bool Enemy::is_removed() +{ + if(is_killed()&&duration(steady_clock::now() - kill_tick).count() > 750) + return true; + return false; +} diff --git a/Enemy.h b/Enemy.h new file mode 100644 index 0000000..a627e31 --- /dev/null +++ b/Enemy.h @@ -0,0 +1,20 @@ +#pragma once +#include "BaseObject.h" +class Enemy: public BaseObject +{ + private: + double range_left, range_right; + time_point kill_tick; + public: + Enemy():BaseObject(unit_length * 4, unit_length * 11),range_left(0),range_right(0){} + Enemy(const double x, const double y,const double range_left,const double range_right): + BaseObject(x,y),range_left(range_left),range_right(range_right){} + void init(); + void kill(); + void move_range(double a, double b); + double move_range_left(); + double move_range_right(); + bool is_removed(); + +}; + diff --git a/Global.h b/Global.h index 0a65bbe..449ba4a 100644 --- a/Global.h +++ b/Global.h @@ -4,6 +4,8 @@ #define WINDOWS_WIDTH 700 #define WINDOWS_HEIGHT 504 +#define unit_length 36 + #define DEBUG #ifdef DEBUG #include diff --git a/Map.cpp b/Map.cpp index 3c43916..942cde5 100644 --- a/Map.cpp +++ b/Map.cpp @@ -8,94 +8,90 @@ void Map::init(BaseObject* h) loadimage(&background, _T("assets\\images\\map1.png")); _width = background.getwidth(); _height = background.getheight(); - int unit_length = 36; //ԪΪ35*35ķȫͼ228Ԫ14Ԫ //עǴ0ʼ // - rocket.push_back(Rocket(0 * unit_length, 10 * unit_length, 1 * unit_length , 1 * unit_length, "rock1")); - rocket.push_back(Rocket(0 * unit_length, 11 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - rocket.push_back(Rocket(2 * unit_length, 10 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - rocket.push_back(Rocket(2 * unit_length, 11 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); + add_rocket(0, 10, 1, 1, "rock1"); + add_rocket(0, 11, 1, 1, "rock1"); + add_rocket(2, 10, 1, 1, "rock1"); + add_rocket(2, 11, 1, 1, "rock1"); //̵ײʯ - rocket.push_back(Rocket(-1 * unit_length, 0 * unit_length, 1 * unit_length, 14 * unit_length)); - rocket.push_back(Rocket(228 * unit_length, 0 * unit_length, 1 * unit_length, 14 * unit_length)); - rocket.push_back(Rocket(0 * unit_length, 12 * unit_length, 69 * unit_length, 2 * unit_length)); - rocket.push_back(Rocket(71 * unit_length, 12 * unit_length, 15 * unit_length, 2 * unit_length)); - rocket.push_back(Rocket(89 * unit_length, 12 * unit_length, 64 * unit_length, 2 * unit_length)); - rocket.push_back(Rocket(155 * unit_length, 12 * unit_length, 73 * unit_length, 2 * unit_length)); - rocket.push_back(Rocket(0 * unit_length, 14 * unit_length, 227 * unit_length, 1 * unit_length)); + add_rocket(-1, 0, 1, 14); + add_rocket(228, 0, 1, 14); + add_rocket(0, 12, 69, 2); + add_rocket(71, 12, 15, 2); + add_rocket(89, 12, 64, 2); + add_rocket(155, 12, 73, 2); + add_rocket(0, 14, 227, 1); //̵Ĭɽ - for(int i=0;i<4;i++) - rocket.push_back(Rocket((134+i) * unit_length, (11-i) * unit_length, 1 * unit_length, (i+1) * unit_length)); - for(int i = 0; i < 4; i++) - rocket.push_back(Rocket((140 + i) * unit_length, (8 + i) * unit_length, 1 * unit_length, (4 - i) * unit_length)); - for(int i = 0; i < 4; i++) - rocket.push_back(Rocket((148 + i) * unit_length, (11 - i) * unit_length, (5-i) * unit_length, 1 * unit_length)); - for(int i = 0; i < 4; i++) - rocket.push_back(Rocket((155 + i) * unit_length, (8 + i) * unit_length, 1 * unit_length, (4 - i) * unit_length)); - for(int i = 0; i < 8; i++) - rocket.push_back(Rocket((181 + i) * unit_length, (11 - i) * unit_length, (9 - i) * unit_length, 1 * unit_length)); - rocket.push_back(Rocket(212 * unit_length, 11 * unit_length, 1 * unit_length, 1 * unit_length)); + for(int i=0;i<4;i++) add_rocket((134+i), (11-i), 1, (i+1)); + for(int i = 0; i < 4; i++) add_rocket((140 + i), (8 + i), 1, (4 - i)); + for(int i = 0; i < 4; i++) add_rocket((148 + i), (11 - i), (5-i), 1); + for(int i = 0; i < 4; i++) add_rocket((155 + i), (8 + i), 1, (4 - i)); + for(int i = 0; i < 8; i++) add_rocket((181 + i), (11 - i), (9 - i), 1); + add_rocket(212, 11, 1, 1); //̵Ưʯ - rocket.push_back(Rocket(16 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - for(int i = 0; i < 5; i++) - rocket.push_back(Rocket((20 + i) * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - rocket.push_back(Rocket(22 * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - for(int i = 0; i < 3; i++) - rocket.push_back(Rocket((77 + i) * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - for(int i = 0; i < 8; i++) - rocket.push_back(Rocket((80 + i) * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - for(int i = 0; i < 4; i++) - rocket.push_back(Rocket((91 + i) * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - rocket.push_back(Rocket(94 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - rocket.push_back(Rocket(100 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - rocket.push_back(Rocket(106 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - rocket.push_back(Rocket(109 * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - rocket.push_back(Rocket(109 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - rocket.push_back(Rocket(112 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - rocket.push_back(Rocket(117 * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - for(int i = 0; i < 3; i++) - rocket.push_back(Rocket((120 + i) * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - for(int i = 0; i < 4; i++) - rocket.push_back(Rocket((127 + i) * unit_length, 4 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - for(int i = 0; i < 2; i++) - rocket.push_back(Rocket((128 + i) * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); - for(int i = 0; i < 4; i++) - rocket.push_back(Rocket((168 + i) * unit_length, 8 * unit_length, 1 * unit_length, 1 * unit_length, "rock1")); + add_rocket(16, 8, 1, 1, "rock1"); + for(int i = 0; i < 5; i++) add_rocket((20 + i), 8, 1, 1, "rock1"); + add_rocket(22 , 4 , 1 , 1 , "rock1"); + for(int i = 0; i < 3; i++) add_rocket((77 + i), 8, 1, 1, "rock1"); + for(int i = 0; i < 8; i++) add_rocket((80 + i), 4, 1, 1, "rock1"); + for(int i = 0; i < 4; i++) add_rocket((91 + i), 4, 1, 1, "rock1"); + add_rocket(94, 8, 1, 1, "rock1"); + add_rocket(100, 8, 1, 1, "rock1"); + add_rocket(106, 8, 1, 1, "rock1"); + add_rocket(109, 4, 1, 1, "rock1"); + add_rocket(109, 8, 1, 1, "rock1"); + add_rocket(112, 8, 1, 1, "rock1"); + add_rocket(117, 8, 1, 1, "rock1"); + for(int i = 0; i < 3; i++) add_rocket((120 + i), 4, 1, 1, "rock1"); + for(int i = 0; i < 4; i++) add_rocket((127 + i), 4, 1, 1, "rock1"); + for(int i = 0; i < 2; i++) add_rocket((128 + i), 8, 1, 1, "rock1"); + for(int i = 0; i < 4; i++) add_rocket((168 + i), 8, 1, 1, "rock1"); - //rocket_sorted_by_left = rocket; - //rocket_sorted_by_right = rocket; - //sort(rocket_sorted_by_left.begin(), rocket_sorted_by_left.end(), [](Rocket &a,Rocket &b) { - // if(a.position.x() < b.position.x()) return true; - // if(a.position.x() == b.position.x() && a.position.y() < b.position.y()) return true; - // return false; - //}); - //sort(rocket_sorted_by_right.begin(), rocket_sorted_by_right.end(), [](Rocket& a, Rocket& b) { - // if(a.position.x() + a.width() -1 > b.position.x() + b.width() - 1) return true; - // if(a.position.x() + a.width() - 1 == b.position.x() + b.width() - 1 && a.position.y() + a.height() -1 < b.position.y() + b.height() - 1) return true; - // return false; - //}); - for(auto &i : rocket) i.init(); + for(auto i : object) i->init(); //Źܵ - pipe.push_back(Pipe(28 * unit_length, 10 * unit_length, 2 * unit_length, 2 * unit_length)); - pipe.push_back(Pipe(38 * unit_length, 9 * unit_length, 2 * unit_length, 3 * unit_length)); - pipe.push_back(Pipe(46 * unit_length, 8 * unit_length, 2 * unit_length, 4 * unit_length)); - pipe.push_back(Pipe(57 * unit_length, 8 * unit_length, 2 * unit_length, 4 * unit_length)); - pipe.push_back(Pipe(163 * unit_length, 10 * unit_length, 2 * unit_length, 2 * unit_length)); - pipe.push_back(Pipe(179 * unit_length, 10 * unit_length, 2 * unit_length, 2 * unit_length)); + add_pipe(28, 10, 2, 2); + add_pipe(38, 9, 2, 3); + add_pipe(46, 8, 2, 4); + add_pipe(57, 8, 2, 4); + add_pipe(163, 10, 2, 2); + add_pipe(179, 10, 2, 2); + + add_enemy(6, 11, 6, 14); + add_enemy(20, 7, 20, 24); + add_enemy(40, 11, 40, 45); + add_enemy(57, 7, 57, 58); + add_enemy(71, 11, 71, 76); + add_enemy(85, 3, 80, 85); + add_enemy(97, 11, 95, 99); + add_enemy(112, 7, 112, 112); + add_enemy(123, 11, 123, 133); + add_enemy(171, 11, 171, 175); + add_enemy(190, 11, 190, 211, 0.2); + add_enemy(193, 11, 190, 211, 0.3); + add_enemy(193, 11, 190, 211, 0.3); + add_enemy(196, 11, 190, 211, 0.1); + add_enemy(199, 11, 190, 211, 0.15); + add_enemy(211, 11, 190, 211, 0.25); + + for(auto i : enemy) i->init(); + } void Map::show(Vector offset) { putimage(0 + static_cast(offset.x()), 0 + static_cast(offset.y()), &background); - for(auto &i: rocket) - if(i.type()!="rock0") - i.show(offset); + for(auto i: object) + i->show(offset); + for(auto i : enemy) + i->show(offset); + } int Map::width() { return _width; @@ -103,136 +99,126 @@ int Map::width() { int Map::height() { return _height; } -void Map::update() +void Map::update(double time) { - check_crash(); + for(auto i : enemy) { + if(i->position.x() < i->move_range_left()) + i->velocity.x(-i->velocity.x()); + if(i->position.x() > i->move_range_right()) + i->velocity.x(-i->velocity.x()); + i->update(time); + } + remove_if(enemy.begin(), enemy.end(), [](Enemy* val) {return val->is_removed();}); + check_collision(); } -bool Map::left_exist_object(BaseObject &t) +bool Map::left_exist(BaseObject &t) { - for(auto& i : rocket) - if(t.position.x() == i.position.x() + i.width() && t.position.y() == i.position.y()) - return true; - for(auto& i : pipe) - if(t.position.x() == i.position.x() + i.width() && t.position.y() == i.position.y()) + for(auto i : object) + if(t.position.x() == i->position.x() + i->width() && t.position.y() == i->position.y()) return true; return false; } -bool Map::right_exist_object(BaseObject& t) +bool Map::right_exist(BaseObject& t) { - for(auto& i : rocket) - if(t.position.x() + t.width() == i.position.x() && t.position.y() == i.position.y()) - return true; - for(auto& i : pipe) - if(t.position.x() + t.width() == i.position.x() && t.position.y() == i.position.y()) + for(auto i : object) + if(t.position.x() + t.width() == i->position.x() && t.position.y() == i->position.y()) return true; return false; } -void Map::check_crash() +void Map::check_collision() { int flag = 0; - for(auto &i : rocket) { - //if(i.position.x() > hero->position.x() + hero->width() || i.position.x() + i.width() < hero->position.x()) - // continue; - //hero->block_crash(i, left_exist_object(i), right_exist_object(i)); - bool l = left_exist_object(i), - r = right_exist_object(i); - string str = collision(*hero, i,l,r); - if(str == "left") - flag = (1<<1)|flag;//0û 1 2 3 - if(str == "right") - flag = (1<<0)|flag; - if(str != "") { - //cout << str <<' '<velocity.y() > 0) - hero->velocity.y(0); - hero->position.y(i.position.y() - hero->height()); - } - else if(str == "bottom") { - if(hero->velocity.y()<0) - hero->velocity.y(0); - hero->position.y(i.position.y() + i.height()); - } - else if(str == "left") { - if(hero->velocity.x() > 0) - hero->velocity.x(0); - hero->position.x(i.position.x() - hero->width()); - } - else if(str == "right") { - if(hero->velocity.x() < 0) - hero->velocity.x(0); - hero->position.x(i.position.x() + i.width()); + double x1 = hero->position.x() - unit_length, + y1 = hero->position.y() - unit_length, + x2, y2; + int w1 = hero->width(), + h1 = hero->height(), + w2, h2; + for(auto i : object) { + x2 = i->position.x() - unit_length; + y2 = i->position.y() - unit_length; + w2 = i->width(); + h2 = i->height(); + if(fabs(x1 + w1 - x2 - w2) < w1 + w2 && fabs(y1 + h1 - y2 - h2) < h1 + h2) + { + bool l = left_exist(*i), + r = right_exist(*i); + string str = collision(*hero, *i, l, r); + if(str == "left") + flag = (1 << 1) | flag;//0û 1 2 3 + if(str == "right") + flag = (1 << 0) | flag; + if(str != "") { + //cout << str <<' '<velocity.y() > 0) + hero->velocity.y(0); + hero->position.y(i->position.y() - hero->height()); + } + else if(str == "bottom") { + if(hero->velocity.y() < 0) + hero->velocity.y(0); + hero->position.y(i->position.y() + i->height()); + } + else if(str == "left") { + if(hero->velocity.x() > 0) + hero->velocity.x(0); + hero->position.x(i->position.x() - hero->width()); + } + else if(str == "right") { + if(hero->velocity.x() < 0) + hero->velocity.x(0); + hero->position.x(i->position.x() + i->width()); + } } } } - - for(auto& i : pipe) { - bool l = left_exist_object(i), - r = right_exist_object(i); - string str = collision(*hero, i, l, r); - if(str == "left") - flag = (1 << 1) | flag;//0û 1 2 3 - if(str == "right") - flag = (1 << 0) | flag; - if(str != "") { - //cout << str <<' '<position.x() - unit_length; + y2 = i->position.y() - unit_length; + w2 = i->width(); + h2 = i->height(); + if(!i->is_killed() && (fabs(x1 + w1 - x2 - w2) < w1 + w2 && fabs(y1 + h1 - y2 - h2) < h1 + h2)) + { + string str = collision(*hero, *i, 0, 0); if(str == "top") { - if(hero->velocity.y() > 0) - hero->velocity.y(0); - hero->position.y(i.position.y() - hero->height()); - } - else if(str == "bottom") { - if(hero->velocity.y() < 0) - hero->velocity.y(0); - hero->position.y(i.position.y() + i.height()); - } - else if(str == "left") { - if(hero->velocity.x() > 0) - hero->velocity.x(0); - hero->position.x(i.position.x() - hero->width()); + i->kill(); } - else if(str == "right") { - if(hero->velocity.x() < 0) - hero->velocity.x(0); - hero->position.x(i.position.x() + i.width()); + else if(str == "left" || str == "right") { + hero->kill(); } } } if(flag) hero->side_crash = flag; else hero->side_crash = 0; +} - //vector:: reverse_iterator l; - //vector::iterator r; - //r = lower_bound(rocket_sorted_by_left.begin(), rocket_sorted_by_left.end(), *hero, [](BaseObject mid, BaseObject val) { - // return mid.position.x() < val.position.x() + val.width() -1; - //}); - //l = upper_bound(rocket_sorted_by_right.rbegin(), rocket_sorted_by_right.rend(), *hero, [](BaseObject val, BaseObject mid) { - // return val.position.x() < mid.position.x() + mid.width() -1; - //}); - // - //static auto time_tick = steady_clock::now(); - //if((steady_clock::now() - time_tick) >= milliseconds(500)) { - // time_tick = steady_clock::now(); - // for(vector::iterator i = l.base()--; i!= rocket_sorted_by_right.end() ; i++) { - // if((*i).position.x() != (*l.base()--).position.x()) break; - // hero->block_crash(i); - // //cout << (*i).position.y() << endl; - // } - // for(auto i = r; i != rocket_sorted_by_left.end(); i++) { - // if((*i).position.x() != (*r).position.x()) break; - // hero->block_crash(*i); - // //cout << (*i).position.y() << endl; - // } - // //cout << (*l.base()--).position.x() + (*l.base()--).width() - 1 << ' ' << (*(r)).position.x() << endl; - //} +void Map::add_rocket(int x, int y, int width, int height, string type) +{ + Rocket* p = new Rocket(x * unit_length, y * unit_length, width * unit_length, height * unit_length, type); + object.push_back(p); } +void Map::add_pipe(int x, int y, int width, int height) +{ + Pipe* p = new Pipe(x * unit_length, y * unit_length, width * unit_length, height * unit_length); + object.push_back(p); +} + +void Map::add_enemy(int x, int y, int range_left, int range_right, double v) +{ + Enemy* p = new Enemy(x * unit_length, y * unit_length, range_left * unit_length, range_right * unit_length); + p->acceleration.y(0); + p->velocity.x(v); + enemy.push_back(p); +} + -string Map::collision(BaseObject &a,BaseObject &b,bool left_exist_object,bool right_exist_object) { +string Map::collision(BaseObject &a,BaseObject &b,bool left_exist,bool right_exist) { int ax = static_cast(round(a.position.x())), ay = static_cast(round(a.position.y())), @@ -259,14 +245,14 @@ string Map::collision(BaseObject &a,BaseObject &b,bool left_exist_object,bool ri //ײ else if(ax + a.width() >= bx && ax < bx && ay + a.height() >= by && ay < by + b.height()) { - if(!left_exist_object) { + if(!left_exist) { return "left"; } } //ײ else if(ax + a.width() > bx + b.width() && ax <= bx + b.width() && ay + a.height() >= by && ay < by + b.height()) { - if(!right_exist_object) { + if(!right_exist) { //cout << "a.x:" << ax << "\t\ta.y:" << ay << "\t\ta.width:" << a.width() << "\ta.height:" << a.height() << endl; //cout << "b.x:" << bx << "\t\tb.y:" << by << "\t\tb.width:" << b.width() << "\tb.height:" << b.height() << endl; return "right"; diff --git a/Map.h b/Map.h index 3f424f1..9f87cef 100644 --- a/Map.h +++ b/Map.h @@ -8,31 +8,31 @@ #include "Global.h" #include "Rocket.h" #include "Pipe.h" - +#include "Enemy.h" class Map { private: IMAGE background; - vector rocket; - vector pipe; - - //vector rocket_sorted_by_left; - //vector rocket_sorted_by_right; + vector object; + vector enemy; BaseObject* hero; int _width; int _height; public: - Map():background(NULL), rocket({}), pipe({}), hero(NULL),_width(0),_height(0){}; + Map():background(NULL), object({}), enemy({}), hero(NULL),_width(0),_height(0){}; void init(BaseObject* h); void show(Vector offset); int width(); int height(); - void update(); - bool left_exist_object(BaseObject& t); - bool right_exist_object(BaseObject& t); - string collision(BaseObject& a, BaseObject& b, bool left_exist_object, bool right_exist_object); - void check_crash(); + void update(double time); + bool left_exist(BaseObject& t); + bool right_exist(BaseObject& t); + string collision(BaseObject& a, BaseObject& b, bool left_exist, bool right_exist); + void check_collision(); + void add_rocket(int x, int y, int width = 1, int height = 1, string type = "rock0"); + void add_pipe(int x, int y, int width = 1, int height = 1); + void add_enemy(int x, int y, int range_left, int range_right, double v = 0.2); }; diff --git a/Mario.cpp b/Mario.cpp index 093cbaa..49051a2 100644 --- a/Mario.cpp +++ b/Mario.cpp @@ -87,3 +87,8 @@ void Mario::init() { SetWorkingImage(NULL); } + +void Mario::kill() +{ + BaseObject::kill(); +} diff --git a/Mario.h b/Mario.h index 4021a74..ed10ffe 100644 --- a/Mario.h +++ b/Mario.h @@ -8,8 +8,8 @@ #include #include "BaseObject.h" -#define MARIO_X 0 -#define MARIO_Y 343 +#define MARIO_X unit_length*1 +#define MARIO_Y unit_length*10 class Mario: public BaseObject @@ -17,12 +17,13 @@ class Mario: public BaseObject private: int direction; public: - Mario():BaseObject(),direction(RIGHT){} + Mario():BaseObject(MARIO_X, MARIO_Y),direction(RIGHT){} void run(double speed = NORMAL_RUN_SPEED); void jump(double speed = NORMAL_JUMP_SPEED); void still(); void turn(int d = RIGHT); void init(); + void kill(); }; diff --git a/Pipe.cpp b/Pipe.cpp index ad423bf..8976035 100644 --- a/Pipe.cpp +++ b/Pipe.cpp @@ -1,15 +1,9 @@ #include "Pipe.h" -Pipe::Pipe(int x, int y, int width, int height) -{ - position.x(x); - position.y(y); - this->width(width); - this->height(height); -} void Pipe::setPipe(int x, int y, int width, int height) { + type("pipe"); position.x(x); position.y(y); this->width(width); diff --git a/Pipe.h b/Pipe.h index 62a0489..0d45809 100644 --- a/Pipe.h +++ b/Pipe.h @@ -4,7 +4,9 @@ class Pipe: public BaseObject { public: Pipe():BaseObject(){} - Pipe(int x, int y, int width, int height); - void setPipe(int x, int y, int width, int height); + Pipe(int x, int y, int width = 1, int height = 1): + BaseObject(x, y, width, height, "pipe") {} + void setPipe(int x, int y, int width = 1, int height = 1); + void init() {} }; diff --git a/Player.cpp b/Player.cpp deleted file mode 100644 index a2a873a..0000000 --- a/Player.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "Player.h" diff --git a/Player.h b/Player.h deleted file mode 100644 index 46416b0..0000000 --- a/Player.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#ifndef _PLAYER_H_ -#define _PLAYER_H_ - - - -#include - -class Player -{ - private: - - public: - - void getControl(); - void applyControl(); -}; - - -#endif \ No newline at end of file diff --git a/Rocket.cpp b/Rocket.cpp index a4854e7..2d21694 100644 --- a/Rocket.cpp +++ b/Rocket.cpp @@ -1,31 +1,18 @@ #include "Rocket.h" -Rocket::Rocket(int x, int y, int width, int height, string type) -{ - _type = type; - position.x(x); - position.y(y); - this->width(width); - this->height(height); -} -void Rocket::setRocket(int x, int y, int width, int height, string type) +void Rocket::setRocket(int x, int y, int width, int height, string _type) { - _type = type; + type(_type); position.x(x); position.y(y); this->width(width); this->height(height); } -string Rocket::type() -{ - return _type; -} - void Rocket::init() { - if(_type == "rock0") return; + if(type() == "rock0") return; LPCTSTR img_src, mask_src; img_src = _T("assets\\images\\rock.png"); mask_src = _T("assets\\images\\rock_mask.png"); diff --git a/Rocket.h b/Rocket.h index 72ae734..a317007 100644 --- a/Rocket.h +++ b/Rocket.h @@ -5,13 +5,11 @@ #include "BaseObject.h" class Rocket : public BaseObject { - private: - string _type; public: - Rocket():BaseObject(), _type("rock0"){} - Rocket(int x, int y, int width = 1, int height = 1, string type = "rock0"); + Rocket():BaseObject(){} + Rocket(int x, int y, int width = 1, int height = 1, string type = "rock0"): + BaseObject(x, y, width, height, type){} void setRocket(int x, int y, int width = 1, int height = 1, string type="rock0"); - string type(); void init(); }; diff --git a/assets/audios/bg.mp3 b/assets/audios/bg.mp3 new file mode 100644 index 0000000..1b5dfff Binary files /dev/null and b/assets/audios/bg.mp3 differ diff --git a/assets/audios/bg_accele.mp3 b/assets/audios/bg_accele.mp3 new file mode 100644 index 0000000..40d6b8d Binary files /dev/null and b/assets/audios/bg_accele.mp3 differ diff --git a/assets/audios/enemy_death.mp3 b/assets/audios/enemy_death.mp3 new file mode 100644 index 0000000..f59c930 Binary files /dev/null and b/assets/audios/enemy_death.mp3 differ diff --git a/assets/audios/jump.mp3 b/assets/audios/jump.mp3 new file mode 100644 index 0000000..8e66167 Binary files /dev/null and b/assets/audios/jump.mp3 differ diff --git a/assets/audios/mario_death.mp3 b/assets/audios/mario_death.mp3 new file mode 100644 index 0000000..64225af Binary files /dev/null and b/assets/audios/mario_death.mp3 differ diff --git a/assets/audios/win.mp3 b/assets/audios/win.mp3 new file mode 100644 index 0000000..e99c35f Binary files /dev/null and b/assets/audios/win.mp3 differ diff --git a/assets/images/enemy.png b/assets/images/enemy.png new file mode 100644 index 0000000..b2fa2b8 Binary files /dev/null and b/assets/images/enemy.png differ diff --git a/assets/images/enemy_mask.png b/assets/images/enemy_mask.png new file mode 100644 index 0000000..7d044d5 Binary files /dev/null and b/assets/images/enemy_mask.png differ diff --git "a/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.cpp" "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.cpp" index cb2209b..f113fc2 100644 --- "a/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.cpp" +++ "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.cpp" @@ -1,12 +1,15 @@ -#include -#include -#include +#include +#include +#include +#include #include "Mario.h" +#include "Enemy.h" #include "Map.h" -//#include "Player.h" +#include "Controller.h" Mario mario; -Map map; +Map map1; +Controller ctl; //开始界面 void start() { @@ -23,8 +26,10 @@ void update() { jump -= 0.5; } if(GetAsyncKeyState(VK_SPACE) || GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState(VK_RIGHT)) { - if(GetAsyncKeyState(VK_SPACE)) + if(GetAsyncKeyState(VK_SPACE)) { + ctl.play_music("jump"); mario.jump(jump); + } if(GetAsyncKeyState(VK_LEFT)) mario.turn(LEFT), mario.run(speed); if(GetAsyncKeyState(VK_RIGHT)) @@ -36,22 +41,22 @@ void update() { //刷新画面 void reflush(double time) { mario.update(time); - map.update(); + map1.update(time); cleardevice(); //offset:地图和人物相对视窗位移,用于使视窗跟随人物移动 //开头处人物未超过视窗宽度1/2,不发生位移 Vector offset(0, 0); if(mario.position.x() + mario.width() / 2 >= WINDOWS_WIDTH / 2 && - mario.position.x() + mario.width() / 2 <= map.width() - WINDOWS_WIDTH / 2) { + mario.position.x() + mario.width() / 2 <= map1.width() - WINDOWS_WIDTH / 2) { //始终保持人物居中 offset.x(-(mario.position.x() + mario.width() / 2 - WINDOWS_WIDTH / 2)); } - else if(mario.position.x() + mario.width() / 2 > map.width() - WINDOWS_WIDTH / 2) { + else if(mario.position.x() + mario.width() / 2 > map1.width() - WINDOWS_WIDTH / 2) { //人物距离结束处不足窗口1/2,停止位移 - offset.x(-(map.width() - WINDOWS_WIDTH)); + offset.x(-(map1.width() - WINDOWS_WIDTH)); } - map.show(offset); + map1.show(offset); mario.show(offset); FlushBatchDraw(); } @@ -60,10 +65,9 @@ void reflush(double time) { int main() { initgraph(WINDOWS_WIDTH, WINDOWS_HEIGHT, SHOWCONSOLE); - + ctl.play_music("gaming"); mario.init(); - map.init(&mario); - + map1.init(&mario); BeginBatchDraw(); const int max_ups = 240; @@ -97,4 +101,6 @@ int main() EndBatchDraw(); closegraph(); + + return 0; } \ No newline at end of file diff --git "a/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.exe" "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.exe" index 6a180f2..44092fb 100644 Binary files "a/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.exe" and "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.exe" differ diff --git "a/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.vcxproj" "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.vcxproj" index 21486af..a2c7fce 100644 --- "a/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.vcxproj" +++ "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.vcxproj" @@ -152,8 +152,10 @@ + + - + @@ -161,12 +163,14 @@ + + - + diff --git "a/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.vcxproj.filters" "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.vcxproj.filters" index e304a61..423f863 100644 --- "a/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.vcxproj.filters" +++ "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.vcxproj.filters" @@ -29,10 +29,16 @@ 源文件 - + 源文件 - + + 源文件 + + + 源文件 + + 源文件 @@ -52,10 +58,16 @@ 头文件 - + 头文件 - + + 头文件 + + + 头文件 + + 头文件