diff --git a/BaseObject.cpp b/BaseObject.cpp index 8bcead1..d55332d 100644 --- a/BaseObject.cpp +++ b/BaseObject.cpp @@ -62,21 +62,20 @@ void Figure::update(int x, int y) { for(auto& i : figures) { if(i.tigger()) { - using namespace std::chrono; - static auto time_tick = steady_clock::now(); - static size_t cnt = 0; if(_status != i.name) { time_tick = steady_clock::now(); - cnt = 0; + figure_cnt = 0; _status = i.name; } - putimage(x, y, &i.masks[i.images.size() - cnt - 1], NOTSRCERASE); - putimage(x, y, &i.images[i.images.size() - cnt - 1], SRCINVERT); + //cout << figure_cnt << endl; + //没人发现我写的bug + putimage(x, y, &i.masks[figure_cnt % i.images.size()], NOTSRCERASE); + putimage(x, y, &i.images[figure_cnt % i.images.size()], SRCINVERT); if((steady_clock::now() - time_tick) >= milliseconds(100)) { - cnt = (cnt + 1) % i.images.size(); + figure_cnt = (figure_cnt + 1) % i.images.size(); time_tick = steady_clock::now(); } - + return; } } } @@ -122,7 +121,7 @@ void BaseObject::update(double time) { void BaseObject::show(Vector& offset) { figure.update(static_cast(round(position.x()) + offset.x()), static_cast(round(position.y()) + offset.y())); } -bool BaseObject::block_crash(BaseObject& t) { +bool BaseObject::block_crash(BaseObject& t,bool left_exist_object,bool right_exist_object) { //if( // position.x() > t.position.x() - figure.width() && position.x() < t.position.x() + t.figure.width() // && @@ -156,20 +155,24 @@ bool BaseObject::block_crash(BaseObject& t) { //左碰撞 if(px + width() >= tpx && px < tpx && py + height() >= tpy && py <= tpy + t.height()) { - if(velocity.x() > 0) { - velocity.x(0); - position.x((double)tpx - width()); + if(!left_exist_object) { + if(velocity.x() > 0) { + velocity.x(0); + position.x((double)tpx - width()); + } + return true; } - return true; } //右碰撞 if(px + width() > tpx + t.width()&& px <= tpx + t.width() && py + height() >= tpy && py <= tpy + t.height()) { - if(velocity.x() < 0) { - velocity.x(0); - position.x((double)tpx + t.width()); + if(!right_exist_object) { + if(velocity.x() < 0) { + velocity.x(0); + position.x((double)tpx + t.width()); + } + return true; } - return true; } return false; } diff --git a/BaseObject.h b/BaseObject.h index 2144b5d..a4125eb 100644 --- a/BaseObject.h +++ b/BaseObject.h @@ -58,15 +58,17 @@ struct FigureData { vector masks; std::function tigger; }; - +using namespace std::chrono; class Figure { private: vector figures; + int figure_cnt = 0; + steady_clock::time_point time_tick = steady_clock::now(); int _width; int _height; string _status; public: - Figure():figures(),_width(0),_height(0),_status("default") {} + Figure():figures(), figure_cnt(0),_width(0),_height(0),_status("default") {} void addFigure(string name, vector imgs, vector masks, function tigger); void update(int x, int y); void turn(); @@ -94,7 +96,7 @@ class BaseObject void height(int h); void update(double time); void show(Vector& offset); - bool block_crash(BaseObject& t); + bool block_crash(BaseObject& t, bool left_exist_object, bool right_exist_object); bool pxiel_crash(); }; diff --git a/Global.h b/Global.h index e9616fe..1a37d7e 100644 --- a/Global.h +++ b/Global.h @@ -2,8 +2,8 @@ #ifndef _GLOBAL_H_ #define _GLOBAL_H_ -#define WINDOWS_WIDTH 680 -#define WINDOWS_HEIGHT 490 +#define WINDOWS_WIDTH 700 +#define WINDOWS_HEIGHT 504 #define DEBUG #ifdef DEBUG diff --git a/Map.cpp b/Map.cpp index 445ee78..03fffbd 100644 --- a/Map.cpp +++ b/Map.cpp @@ -1,14 +1,23 @@ #include "Map.h" - +#include void Map::init(BaseObject* h) { + hero = h; + loadimage(&background, _T("assets\\images\\map1.png")); _width = background.getwidth(); _height = background.getheight(); - int unit_length = 35; + 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")); + + //铺垫底层基石 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)); @@ -16,6 +25,8 @@ void Map::init(BaseObject* h) 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)); + + //铺垫默认山丘 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++) @@ -27,12 +38,56 @@ void Map::init(BaseObject* h) 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)); - hero = h; + + //铺垫漂浮岩石 + 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")); + + //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(); + } 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); } int Map::width() { return _width; @@ -45,9 +100,53 @@ void Map::update() check_crash(); } +bool Map::left_exist_object(BaseObject &t) +{ + for(auto& i : rocket) + 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) +{ + for(auto& i : rocket) + if(t.position.x() + t.width() == i.position.x() && t.position.y() == i.position.y()) + return true; + return false; +} + void Map::check_crash() { - for(auto& i : rocket) { - hero->block_crash(i); + 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)); } + + //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; + //} + } diff --git a/Map.h b/Map.h index 9634b5f..f7d3c9e 100644 --- a/Map.h +++ b/Map.h @@ -13,6 +13,8 @@ class Map private: IMAGE background; vector rocket; + //vector rocket_sorted_by_left; + //vector rocket_sorted_by_right; BaseObject* hero; int _width; int _height; @@ -23,6 +25,8 @@ class Map int width(); int height(); void update(); + bool left_exist_object(BaseObject& t); + bool right_exist_object(BaseObject& t); void check_crash(); }; diff --git a/Mario.cpp b/Mario.cpp index 52e99b1..3350716 100644 --- a/Mario.cpp +++ b/Mario.cpp @@ -34,12 +34,25 @@ void Mario::init() { 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; + for(int i = 0; i < 3; i++) { + SetWorkingImage(&origin); + getimage(&temp, width + width * i, 0, width, height); + imgs.push_back(temp); + SetWorkingImage(&organ_mask); + getimage(&temp, width + width * i, 0, width, height); + masks.push_back(temp); + } + figure.addFigure("running", imgs, masks, [this]()->bool { + if(velocity.x() != 0 && velocity.y() == 0) return true; + return false; + }); + + imgs.clear(); masks.clear(); SetWorkingImage(&origin); getimage(&temp, 0, 0, width, height); imgs.push_back(temp); @@ -47,26 +60,13 @@ void Mario::init() { getimage(&temp, 0, 0, width, height); masks.push_back(temp); figure.addFigure("still", imgs, masks, [this]()->bool { - if(velocity.x() == 0 && velocity.y() == 0) return true; + if(velocity.x() == 0 && velocity.y() == 0) return true; return false; }); figure.addFigure("falling", imgs, masks, [this]()->bool { if(velocity.y() > 0) return true; return false; }); - imgs.clear(); masks.clear(); - for(int i = 0; i < 3; i++) { - SetWorkingImage(&origin); - getimage(&temp, width + width * i, 0, width, height); - imgs.push_back(temp); - SetWorkingImage(&organ_mask); - getimage(&temp, width + width * i, 0, width, height); - masks.push_back(temp); - } - figure.addFigure("running", imgs, masks, [this]()->bool { - if(velocity.x() != 0 && velocity.y() == 0) return true; - return false; - }); imgs.clear(); masks.clear(); SetWorkingImage(&origin); diff --git a/Mario.h b/Mario.h index b79e2ab..4021a74 100644 --- a/Mario.h +++ b/Mario.h @@ -3,7 +3,7 @@ #define _MARIO_H_ #define NORMAL_RUN_SPEED 0.3 -#define NORMAL_JUMP_SPEED -1.2 +#define NORMAL_JUMP_SPEED -1.1 #include #include "BaseObject.h" diff --git a/Rocket.cpp b/Rocket.cpp index 010e715..a4854e7 100644 --- a/Rocket.cpp +++ b/Rocket.cpp @@ -1,18 +1,58 @@ #include "Rocket.h" -Rocket::Rocket(int x, int y, int width, int height) +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) +void Rocket::setRocket(int x, int y, int width, int height, string 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; + LPCTSTR img_src, mask_src; + img_src = _T("assets\\images\\rock.png"); + mask_src = _T("assets\\images\\rock_mask.png"); + int figure_num = 7; + 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); + string names[7] = {"rock1","rock2","rock3","rock4","rock5","rock6","rock7"}; + vector imgs, masks; + IMAGE temp; + + for(int i = 0; i < 7; i++) { + imgs.clear(); masks.clear(); + SetWorkingImage(&origin); + getimage(&temp, width * i, 0, width, height); + imgs.push_back(temp); + SetWorkingImage(&organ_mask); + getimage(&temp, width * i, 0, width, height); + masks.push_back(temp); + figure.addFigure(names[i], imgs, masks, [name = names[i],this]()->bool { + if(this->type() == name) return true; + return false; + }); + } + SetWorkingImage(NULL); +} diff --git a/Rocket.h b/Rocket.h index 0e5f8f2..72ae734 100644 --- a/Rocket.h +++ b/Rocket.h @@ -5,10 +5,14 @@ #include "BaseObject.h" class Rocket : public BaseObject { + private: + string _type; public: - Rocket():BaseObject(){} - Rocket(int x, int y, int width, int height); - void setRocket(int x,int y,int width,int height); + Rocket():BaseObject(), _type("rock0"){} + Rocket(int x, int y, int width = 1, int height = 1, string type = "rock0"); + void setRocket(int x, int y, int width = 1, int height = 1, string type="rock0"); + string type(); + void init(); }; #endif \ No newline at end of file diff --git a/assets/images/map1.png b/assets/images/map1.png index 252bc77..9bafb25 100644 Binary files a/assets/images/map1.png and b/assets/images/map1.png differ diff --git a/assets/images/mario.png b/assets/images/mario.png index 86215ea..7ecf5ff 100644 Binary files a/assets/images/mario.png and b/assets/images/mario.png differ diff --git a/assets/images/mario_mask.png b/assets/images/mario_mask.png index ab27c00..7fb79ba 100644 Binary files a/assets/images/mario_mask.png and b/assets/images/mario_mask.png differ diff --git a/assets/images/rock.png b/assets/images/rock.png new file mode 100644 index 0000000..7f1dc65 Binary files /dev/null and b/assets/images/rock.png differ diff --git a/assets/images/rock_mask.png b/assets/images/rock_mask.png new file mode 100644 index 0000000..46a2115 Binary files /dev/null and b/assets/images/rock_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 fa8eb69..55e4cdd 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" @@ -68,7 +68,6 @@ int main() auto next_game_tick = steady_clock::now(); int loops; double interpolation; - bool game_is_running = true; while(game_is_running) { 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 ef73f17..5c4c850 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