diff --git a/Global.h b/Global.h new file mode 100644 index 0000000..a9349be --- /dev/null +++ b/Global.h @@ -0,0 +1,35 @@ +#pragma once + +#define WINDOWS_WIDTH 640 +#define WINDOWS_HEIGHT 480 + +#define DEBUG +#ifdef DEBUG +#include +#include +using namespace std; +#endif + +#define RUNNING 0 +#define STILL 1 +#define JUMPPING 2 + +#define RIGHT 0 +#define LEFT 1 + + +#include +void rotateFlip(IMAGE* img) +{ + DWORD* img_pMem = GetImageBuffer(img); + + for(int y = 0; y< img->getheight(); y++) + { + for(int x = 0; x< int(img->getwidth()/2+0.5); x++) + { + int num1 = y*img->getwidth()+x; + int num2 = (y+1)*img->getwidth()-x-1; + std::swap(img_pMem[num1], img_pMem[num2]); + } + } +} diff --git a/Map.h b/Map.h new file mode 100644 index 0000000..24343ab --- /dev/null +++ b/Map.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include "Global.h" + +class Map +{ + private: + IMAGE background; + public: + Map(); + ~Map(); + void loadResource(); + void show(); + +}; + +Map::Map() +{ + +} + +Map::~Map() +{ + +} + +inline void Map::loadResource() +{ + loadimage(&background, _T("assert\\images\\map1.png")); +} + +inline void Map::show() +{ + putimage(0, 0, &background); +} diff --git a/Mario.h b/Mario.h new file mode 100644 index 0000000..fb31692 --- /dev/null +++ b/Mario.h @@ -0,0 +1,126 @@ +#pragma once + +#include +#include "Global.h" +#include "MovableObject.h" + +#define MARIO_X 0 +#define MARIO_Y 343 + +class Mario_figure +{ + private: + IMAGE running_figure[10]; + IMAGE running_figure_mask[10]; + IMAGE still_figure; + IMAGE still_figure_mask; + IMAGE jump_figure; + IMAGE jump_figure_mask; + int direction; + void LoadImages(int, LPCTSTR, LPCTSTR); + void changeDirection(); + public: + Mario_figure(); + void loadResource(); + void show(int, int, int, int); +}; + +class Mario: public MovableObject +{ + private: + Mario_figure figure; + public: + Mario(); + void loadResource(); + void show(); +}; + +inline void Mario_figure::LoadImages(int figure_num, LPCTSTR f_src, LPCTSTR fm_src) +{ + IMAGE origin, organ_mask; + loadimage(&origin, f_src); + loadimage(&organ_mask, fm_src); + int width = origin.getwidth()/figure_num; + int height = origin.getheight(); + + SetWorkingImage(&origin); + getimage(&still_figure, 0, 0, width, height); + for(int i = 0; i<3; i++) + getimage(&running_figure[i], width+width*i, 0, width, height); + getimage(&jump_figure, width*4, 0, width, height); + + SetWorkingImage(&organ_mask); + getimage(&still_figure_mask, 0, 0, width, height); + for(int i = 0; i<3; i++) + getimage(&running_figure_mask[i], width+width*i, 0, width, height); + getimage(&jump_figure_mask, width*4, 0, width, height); + + SetWorkingImage(NULL); +} + +inline void Mario_figure::changeDirection() +{ + rotateFlip(&still_figure); + rotateFlip(&still_figure_mask); + rotateFlip(&jump_figure); + rotateFlip(&jump_figure_mask); + for(int i = 0; i < 3; i++) + rotateFlip(&running_figure[i]), + rotateFlip(&running_figure_mask[i]); +} + + +inline Mario_figure::Mario_figure():direction(RIGHT){} + +inline void Mario_figure::loadResource() +{ + LoadImages(5, _T("assert\\images\\mario.png"), _T("assert\\images\\mario_mask.png")); +} + +inline void Mario_figure::show(int status, int drt, int x, int y) +{ + static clock_t last = clock();//定时器 + static int running_cnt = 0;//动作变化计数 + if(direction != drt) { + changeDirection(); + direction = drt; + } + switch(status) + { + case RUNNING: + putimage(x, y, &running_figure_mask[running_cnt], NOTSRCERASE); + putimage(x, y,&running_figure[running_cnt], SRCINVERT); + if((clock() - last) >= 100) {//定时器,动作变化间隔100ms + last = clock(); + running_cnt = (running_cnt + 1) % 3; + } + break; + case STILL: + putimage(x, y, &still_figure_mask, NOTSRCERASE); + putimage(x, y, &still_figure, SRCINVERT); + running_cnt = 0; + break; + case JUMPPING: + putimage(x, y, &jump_figure_mask, NOTSRCERASE); + putimage(x, y, &jump_figure, SRCINVERT); + running_cnt = 0; + break; + } +} + + + +inline Mario::Mario():MovableObject(MARIO_X,MARIO_Y){} + +inline void Mario::loadResource() +{ + figure.loadResource(); +} + +inline void Mario::show() +{ + if(is_running)figure.show(RUNNING, direction, getX(), getY()); + else if(is_jumping)figure.show(JUMPPING, direction, getX(), getY()); + else + figure.show(STILL, direction, getX(), getY()); +} \ No newline at end of file diff --git a/MovableObject.h b/MovableObject.h new file mode 100644 index 0000000..7d9b78b --- /dev/null +++ b/MovableObject.h @@ -0,0 +1,110 @@ +#pragma once +#include "Global.h" + +//时间比例,RATE=1000 ms : 数据刷新间隔,用于每次数据刷新的换算 +#define RATE 100.0 + +//重力加速度 +#define G 25 + +//MovableObject:所有可移动“活物”的基类 +class MovableObject +{ + protected: + double x; + double y; + double vx; + double vy; + double ax; + double ay; + bool is_running; + bool is_jumping; + int direction; + public: + MovableObject(int, int, bool, bool, int); + int getX(); + int getY(); + void setX(double); + void setY(double); + void run(int, double); + void jump(); + void still(); + void update(); +}; + + +inline MovableObject::MovableObject(int dx, int dy, bool run = 0, bool jump = 0, int drt = RIGHT): + x(dx), y(dy), vx(0), vy(0), ax(0),ay(G/RATE),is_running(run), is_jumping(jump), direction(drt) {}; + +//实数四舍五入后的整数X +inline int MovableObject::getX() +{ + return round(x); +} + +//实数四舍五入后的整数Y +inline int MovableObject::getY() +{ + return round(y); +} + +//设置X +inline void MovableObject::setX(double dx) +{ + x = dx; +} + +//设置Y +inline void MovableObject::setY(double dy) +{ + y = dy; +} + +//进入行走状态,横向移动,drt:方向,speed:速度(单位px/s) +inline void MovableObject::run(int drt, double speed = 250) +{ + if(drt == LEFT) { + vx=0-speed/RATE; + } + if(drt == RIGHT) { + vx=speed/RATE; + } + direction = drt; + + if(is_jumping) + is_running = 0; + else + is_running = 1; +} + +//进入跳跃状态,设置向上的初速度 +inline void MovableObject::jump() +{ + vy = 0-500.0/RATE; + is_jumping = 1; +} + +//进入静止状态 +inline void MovableObject::still() +{ + vx = 0; + is_running = is_jumping = 0; +} + +//更新实时数据 +inline void MovableObject::update() +{ + static clock_t last = clock();//定时器 + static clock_t now = clock();//定时器 + //可以使用C++11里面的chrono替代,有空研究 + //做定时器的目的是精确控制速度和位移 + if((now=clock()) - last >= 10) { + last = now; + x += vx; + y += vy; + vx += ax; + vy += ay; + //其实有公式可以更精确计算 + //但是用微分精确度也可以,而且简单 + } +} diff --git a/Player.h b/Player.h new file mode 100644 index 0000000..d32a1b8 --- /dev/null +++ b/Player.h @@ -0,0 +1,20 @@ +#pragma once +#include + +class Player +{ + private: + + public: + + void getControl(); + void applyControl(); +}; + + + + +inline void Player::getControl() +{ + +} diff --git a/assert/images/map1.bmp b/assert/images/map1.bmp new file mode 100644 index 0000000..028e6c9 Binary files /dev/null and b/assert/images/map1.bmp differ diff --git a/assert/images/map1.png b/assert/images/map1.png new file mode 100644 index 0000000..491aa9f Binary files /dev/null and b/assert/images/map1.png differ diff --git a/assert/images/mario.png b/assert/images/mario.png new file mode 100644 index 0000000..86215ea Binary files /dev/null and b/assert/images/mario.png differ diff --git a/assert/images/mario_mask.png b/assert/images/mario_mask.png new file mode 100644 index 0000000..ab27c00 Binary files /dev/null and b/assert/images/mario_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" new file mode 100644 index 0000000..b07149e --- /dev/null +++ "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.cpp" @@ -0,0 +1,53 @@ +锘#include +#include +#include "Mario.h" +#include "Map.h" +#include "Player.h" + +Map map; +Mario mario; +//寮濮嬬晫闈 +void start() { + +} + +//鏇存柊瀹炴椂鏁版嵁 +void update() { + if(GetAsyncKeyState(VK_SPACE)) + mario.jump(); + else if(GetAsyncKeyState(VK_LEFT)) + mario.run(LEFT); + else if(GetAsyncKeyState(VK_RIGHT)) + mario.run(RIGHT); + else mario.still(); + + mario.update(); +} + +//鍒锋柊鐢婚潰 +void reflush() { + cleardevice(); + map.show(); + mario.show(); + FlushBatchDraw(); + //_getch(); +} + +int main() +{ + initgraph(WINDOWS_WIDTH, WINDOWS_HEIGHT, SHOWCONSOLE); + + map.loadResource(); + mario.loadResource(); + + BeginBatchDraw(); + while(true) + { + start(); + update(); + reflush(); + } + 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.sln" "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.sln" new file mode 100644 index 0000000..10f1bf1 --- /dev/null +++ "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.sln" @@ -0,0 +1,31 @@ +锘 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29519.87 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "瓒呯骇椹噷濂", "瓒呯骇椹噷濂.vcxproj", "{4F69492E-52F4-46FC-84F4-5AAD8D8723F1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4F69492E-52F4-46FC-84F4-5AAD8D8723F1}.Debug|x64.ActiveCfg = Debug|x64 + {4F69492E-52F4-46FC-84F4-5AAD8D8723F1}.Debug|x64.Build.0 = Debug|x64 + {4F69492E-52F4-46FC-84F4-5AAD8D8723F1}.Debug|x86.ActiveCfg = Debug|Win32 + {4F69492E-52F4-46FC-84F4-5AAD8D8723F1}.Debug|x86.Build.0 = Debug|Win32 + {4F69492E-52F4-46FC-84F4-5AAD8D8723F1}.Release|x64.ActiveCfg = Release|x64 + {4F69492E-52F4-46FC-84F4-5AAD8D8723F1}.Release|x64.Build.0 = Release|x64 + {4F69492E-52F4-46FC-84F4-5AAD8D8723F1}.Release|x86.ActiveCfg = Release|Win32 + {4F69492E-52F4-46FC-84F4-5AAD8D8723F1}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {75343DAE-ED56-4BDC-A13E-EB7801294EBD} + EndGlobalSection +EndGlobal 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" new file mode 100644 index 0000000..77db2eb --- /dev/null +++ "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.vcxproj" @@ -0,0 +1,166 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {4F69492E-52F4-46FC-84F4-5AAD8D8723F1} + Win32Proj + 瓒呯骇椹噷濂 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + \ No newline at end of file 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" new file mode 100644 index 0000000..5fbd870 --- /dev/null +++ "b/\350\266\205\347\272\247\351\251\254\351\207\214\345\245\245.vcxproj.filters" @@ -0,0 +1,35 @@ +锘 + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + + + 婧愭枃浠 + + + + + 澶存枃浠 + + + 澶存枃浠 + + + 澶存枃浠 + + + 澶存枃浠 + + + 澶存枃浠 + + + \ No newline at end of file