Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Commit

Permalink
添加项目文件。
Browse files Browse the repository at this point in the history
  • Loading branch information
Wider committed Apr 5, 2020
1 parent 01738bf commit eb14683
Show file tree
Hide file tree
Showing 13 changed files with 612 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#define WINDOWS_WIDTH 640
#define WINDOWS_HEIGHT 480

#define DEBUG
#ifdef DEBUG
#include<iostream>
#include<cstdio>
using namespace std;
#endif

#define RUNNING 0
#define STILL 1
#define JUMPPING 2

#define RIGHT 0
#define LEFT 1


#include<utility>
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]);
}
}
}
36 changes: 36 additions & 0 deletions Map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include<graphics.h>
#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);
}
126 changes: 126 additions & 0 deletions Mario.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#pragma once

#include<graphics.h>
#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());
}
110 changes: 110 additions & 0 deletions MovableObject.h
Original file line number Diff line number Diff line change
@@ -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;
//其实有公式可以更精确计算
//但是用微分精确度也可以,而且简单
}
}
20 changes: 20 additions & 0 deletions Player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include<windows.h>

class Player
{
private:

public:

void getControl();
void applyControl();
};




inline void Player::getControl()
{

}
Binary file added assert/images/map1.bmp
Binary file not shown.
Binary file added assert/images/map1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assert/images/mario.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assert/images/mario_mask.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions 超级马里奥.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include<conio.h>
#include<ctime>
#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;
}
Loading

0 comments on commit eb14683

Please sign in to comment.