From 1e807a0dd27310031593f192a6ab6a1088ed2d52 Mon Sep 17 00:00:00 2001 From: Wider Date: Mon, 13 Apr 2020 23:41:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=80=A7=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=A2=B0=E6=92=9E=E6=A3=80=E6=B5=8B=EF=BC=8C?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B8=B8=E6=88=8F=E7=BB=86=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BaseObject.cpp | 64 +------------ BaseObject.h | 17 ++-- Global.h | 1 - Map.cpp | 85 +++++++++++++++++- Map.h | 1 + Mario.cpp | 35 ++++---- ...7\351\251\254\351\207\214\345\245\245.cpp" | 35 ++++---- ...7\351\251\254\351\207\214\345\245\245.exe" | Bin 303616 -> 303616 bytes 8 files changed, 137 insertions(+), 101 deletions(-) diff --git a/BaseObject.cpp b/BaseObject.cpp index d55332d..f325f2b 100644 --- a/BaseObject.cpp +++ b/BaseObject.cpp @@ -45,8 +45,8 @@ bool Vector::operator != (const Vector& rhs) const { if(*this == rhs) return false; else return true; } -double Vector::x() { return (_x); } -double Vector::y() { return (_y); } +double Vector::x() const { return (_x); } +double Vector::y() const { return (_y); } 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; } @@ -62,6 +62,8 @@ void Figure::update(int x, int y) { for(auto& i : figures) { if(i.tigger()) { + if(i.name!="rock0"&&i.name!="rock1") + //cout << i.name << endl; if(_status != i.name) { time_tick = steady_clock::now(); figure_cnt = 0; @@ -121,63 +123,5 @@ 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 left_exist_object,bool right_exist_object) { - //if( - // position.x() > t.position.x() - figure.width() && position.x() < t.position.x() + t.figure.width() - // && - // position.y() > t.position.y() - figure.height() && position.y() < t.position.y() + t.figure.height() - // ) - // return true; - //cout << t.position.x() << " " << t.position.y() << endl; - int px = static_cast(round(position.x())), - py = static_cast(round(position.y())), - tpx = static_cast(round(t.position.x())), - tpy = static_cast(round(t.position.y())); - - //上碰撞(相对被撞物体) - if(px + width() -1 >= tpx && px <= tpx + t.width() -1 && - py + height() >= tpy && py < tpy) { - if(velocity.y() > 0) { - velocity.y(0); - position.y((double)tpy - height()); - } - return true; - } - //下碰撞 - if(px + width() -1 >= tpx && px <= tpx + t.width() -1 && - py + height() -1 > tpy + height() -1 && py <= tpy + t.height()) { - if(velocity.y() < 0) { - velocity.y(0); - position.y((double)tpy + t.height()); - } - return true; - } - //左碰撞 - if(px + width() >= tpx && px < tpx && - py + height() >= tpy && py <= tpy + t.height()) { - if(!left_exist_object) { - if(velocity.x() > 0) { - velocity.x(0); - position.x((double)tpx - width()); - } - return true; - } - } - //右碰撞 - if(px + width() > tpx + t.width()&& px <= tpx + t.width() && - py + height() >= tpy && py <= tpy + t.height()) { - if(!right_exist_object) { - if(velocity.x() < 0) { - velocity.x(0); - position.x((double)tpx + t.width()); - } - return true; - } - } - return false; -} -//bool BaseObject::pxiel_crash() { -// -//} diff --git a/BaseObject.h b/BaseObject.h index a4125eb..f69f120 100644 --- a/BaseObject.h +++ b/BaseObject.h @@ -9,7 +9,7 @@ #include #include "Global.h" - +#define GRAVITY 0.0036 class Vector { private: @@ -31,8 +31,8 @@ class Vector { bool operator == (const Vector& rhs) const; bool operator == (const double rhs) const; bool operator != (const Vector& rhs) const; - double x(); - double y(); + double x() const; + double y() const; void x(double x); void y(double y); void set(double x, double y); @@ -40,16 +40,22 @@ class Vector { class Position: public Vector { public: using Vector::Vector; + Position():Vector(){} + Position(const Vector& t) { x(t.x()), y(t.y()); } using Vector::operator=; }; class Velocity: public Vector { public: using Vector::Vector; + Velocity():Vector() {} + Velocity(const Vector& t) { x(t.x()), y(t.y()); } using Vector::operator=; }; class Acceleration: public Vector { public: using Vector::Vector; + Acceleration():Vector() {} + Acceleration(const Vector& t) { x(t.x()), y(t.y()); } using Vector::operator=; }; struct FigureData { @@ -89,15 +95,14 @@ class BaseObject Velocity velocity; Acceleration acceleration; Figure figure; - BaseObject():_width(0),_height(0),position(), velocity(), acceleration(0, 0.004), figure(){} + BaseObject():_width(0),_height(0),position(), velocity(), acceleration(0, GRAVITY), figure(){} + int side_crash = 0; int width(); int height(); void width(int w); void height(int h); void update(double time); void show(Vector& offset); - 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 1a37d7e..0a65bbe 100644 --- a/Global.h +++ b/Global.h @@ -1,7 +1,6 @@ #pragma once #ifndef _GLOBAL_H_ #define _GLOBAL_H_ - #define WINDOWS_WIDTH 700 #define WINDOWS_HEIGHT 504 diff --git a/Map.cpp b/Map.cpp index 03fffbd..8eabf8d 100644 --- a/Map.cpp +++ b/Map.cpp @@ -12,7 +12,7 @@ void Map::init(BaseObject* h) //单元格为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, 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")); @@ -118,11 +118,44 @@ bool Map::right_exist_object(BaseObject& t) void Map::check_crash() { + 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)); + //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()); + } + } } + if(flag) hero->side_crash = flag; + else hero->side_crash = 0; //vector:: reverse_iterator l; //vector::iterator r; @@ -150,3 +183,47 @@ void Map::check_crash() //} } + + +string Map::collision(BaseObject &a,BaseObject &b,bool left_exist_object,bool right_exist_object) { + + int ax = static_cast(round(a.position.x())), + ay = static_cast(round(a.position.y())), + bx = static_cast(round(b.position.x())), + by = static_cast(round(b.position.y())); + //上碰撞(a相对被撞物体b) + if(ax + a.width() > bx && ax < bx + b.width() && + ay + a.height() >= by && ay < by) { + if(a.position.x() < b.position.x() && a.position.x() + a.width() < b.position.x() + b.width() && a.position.x() + a.width() - b.position.x() < a.position.y() + a.height() - b.position.y()) + return "left"; + if(a.position.x() > b.position.x() && a.position.x() + a.width() > b.position.x() + b.width() && b.position.x() + b.width() - a.position.x() < a.position.y() + a.height() - b.position.y()) + return "right"; + return "top"; + } + //下碰撞 + else if(ax + a.width() > bx && ax < bx + b.width() && + ay + a.height() > by + b.height() && ay < by + b.height()) { + if(a.position.x() < b.position.x() && a.position.x() + a.width() < b.position.x() + b.width() && a.position.x() + a.width() - b.position.x() < b.position.y() + b.height() - a.position.y()) + return "left"; + if(a.position.x() > b.position.x() && a.position.x() + a.width() > b.position.x() + b.width() && b.position.x() + b.width() - a.position.x() < b.position.y() + b.height() - a.position.y()) + return "right"; + return "bottom"; + } + //左碰撞 + else if(ax + a.width() >= bx && ax < bx && + ay + a.height() >= by && ay < by + b.height()) { + if(!left_exist_object) { + 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) { + //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"; + } + } + return ""; +} \ No newline at end of file diff --git a/Map.h b/Map.h index f7d3c9e..9f75211 100644 --- a/Map.h +++ b/Map.h @@ -28,6 +28,7 @@ class Map bool left_exist_object(BaseObject& t); bool right_exist_object(BaseObject& t); void check_crash(); + string collision(BaseObject& a, BaseObject& b, bool left_exist_object, bool right_exist_object); }; diff --git a/Mario.cpp b/Mario.cpp index 3350716..093cbaa 100644 --- a/Mario.cpp +++ b/Mario.cpp @@ -5,11 +5,14 @@ void Mario::run(double speed) { velocity.x(speed); else velocity.x(-speed); + if(side_crash == 1 && velocity.x() < 0)velocity.x(0); + if(side_crash == 2 && velocity.x() > 0)velocity.x(0); + if(side_crash == 3)velocity.x(0); + } void Mario::jump(double speed) { if(figure.status() == "jumpping" || figure.status() == "falling") return; - position.y(position.y()); velocity.y(speed); } @@ -39,20 +42,6 @@ void Mario::init() { 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); @@ -60,6 +49,7 @@ void Mario::init() { getimage(&temp, 0, 0, width, height); masks.push_back(temp); figure.addFigure("still", imgs, masks, [this]()->bool { + if(figure.status() == "jumpping") return false; if(velocity.x() == 0 && velocity.y() == 0) return true; return false; }); @@ -67,6 +57,21 @@ void Mario::init() { 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(figure.status() == "jumpping") return false; + if(velocity.x() != 0 && velocity.y() == 0) return true; + return false; + }); + imgs.clear(); masks.clear(); SetWorkingImage(&origin); 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 55e4cdd..96779c9 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" @@ -59,28 +59,33 @@ int main() BeginBatchDraw(); - const int TICKS_PER_SECOND = 60; - const int SKIP_TICKS = 1000 / TICKS_PER_SECOND; - const int MAX_FRAMESKIP = 5; + const int max_ups = 120; + const int max_fps = 120; + + const int update_skip_time = 1000 / max_ups; + const int frame_skip_time = 1000 / max_fps; using namespace std::chrono; - auto next_game_tick = steady_clock::now(); - int loops; - double interpolation; + auto update_time_tick = steady_clock::now(); + auto frame_time_tick = steady_clock::now(); + + double update_time_interval; + double frame_time_interval; + bool game_is_running = true; while(game_is_running) { - - loops = 0; - while(steady_clock::now() > next_game_tick&& loops < MAX_FRAMESKIP) { + auto now = steady_clock::now(); + update_time_interval = duration(now - update_time_tick).count(); + if(update_time_interval >= update_skip_time) { update(); - next_game_tick += milliseconds(SKIP_TICKS); - loops++; + update_time_tick = now; + } + frame_time_interval = duration(now - frame_time_tick).count(); + if(frame_time_interval >= frame_skip_time) { + reflush(frame_time_interval); + frame_time_tick = now; } - - interpolation = duration(steady_clock::now() + milliseconds(SKIP_TICKS) - next_game_tick).count() - / double(SKIP_TICKS); - reflush(interpolation); } EndBatchDraw(); 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 5c4c8507e832ee5849f9db5173586776895d2bbe..6a180f214e48272e624590d3d11024b4d5de59e7 100644 GIT binary patch delta 342 zcmZoTBGhn1Xaff$(+Bs>T#PHYnO@m7Z{gX#g@@5bdGi8E1E%c?MvM)T(?94l>gw@j zGBdoGu>0SCkM7n2Mg|5C)(H$iveT8rquZC`#r4BLnNC*?Fz58~=?X!N3e&R<7$rDw z@B8=v6Te`q#dJACMoHP7P_6^W45(4v9UFjVy|{8@dRq{qyt4j@@)VD5*9?#513<%1 zyvT)_0M_E0@nX`E>5O5F^3#86 zpEVN%ZchK7&FsmZ1_s}@`{ytxsxb0z-#CGJ{i^9X)+{Pq3$`;cMDAo_FfrWTXU$TS F1^{78hL!*T delta 349 zcmZoTBGhn1Xaff$(;tV;T#PHYnXHtXxA1J=!oz5zJXs-xYx*j0MrFNyEzArr9IpTS z@6p{_z{tSh!8(BfNOrn%cy#-6ylB4Id1d_*5O5F^3yN-Fp4l;zd8M-52K=l^TU7tJ6cV^YC