#include "inertia.h"
#include"graphics.h"
#include"mydefine.h"
#include"math.h"
#include"inertia.h"
#include "role.h"
#pragma comment(lib,"Winmm.lib") //windows API 音乐
//
role::role(int world)
{
myHero.x = X0; //初始化主角数据
myHero.y = X0;
myHero.xx = myHero.x;
myHero.yy = myHero.y;
myHero.x0 = 0;
myHero.vX = 0;
myHero.vY = 0;
myHero.turn = 1;
myHero.direction.x = 0;
myHero.direction.y = 0;
myHero.isFly = true;
myHero.isShoot = false;
myHero.died = false;
myHero.ending = false;
myHero.passed = false;
myScene = 0; //初始化参数
hero_iframe = 1;
enemy_iframe = 1;
shootButtonDown = false;
shootTimeInterval = 0;
score = 0;
loadimage(&img_hero, "res\\role.bmp");
loadimage(&img_enemy, "res\\ani.bmp");
loadimage(&img_showBomb, "res\\ani.bmp");
loadimage(&img_bullet, "res\\ani.bmp");
for (int i = 0; i<ENEMY_TOTE; i++) //初始化敌人
{
myEnemy[i].x = 0;
myEnemy[i].y = 0;
myEnemy[i].turn = 0;
}
for (int j = 0; j<BOMB_NUMBER; j++) //初始化子弹爆炸
{
bombs[j].x = 0;
bombs[j].y = 0;
bomb_iframe[j] = 0;
}
for (int k = 0; k<BULLET_NUMBER; k++) //初始化子弹
{
bullets[k].x = 0;
bullets[k].y = 0;
bullets[k].turn = 0;
bullet_iframe[k] = 0;
}
createEnemy(world);
mciSendString("open res\\死亡1.mp3 alias music_died", NULL, 0, NULL); //音乐导入
mciSendString("open res\\跳.mp3 alias music_jump", NULL, 0, NULL);
mciSendString("open res\\金币.mp3 alias music_coin", NULL, 0, NULL);
mciSendString("open res\\踩敌人.mp3 alias music_tread", NULL, 0, NULL);
mciSendString("open res\\吃到武器.mp3 alias music_getWeapon", NULL, 0, NULL);
mciSendString("open res\\子弹.mp3 alias music_bullet", NULL, 0, NULL);
mciSendString("open res\\子弹撞墙.mp3 alias music_boom", NULL, 0, NULL);
mciSendString("open res\\子弹打到敌人.mp3 alias music_boom2", NULL, 0, NULL);
}
role::~role(void)
{
}
void role::createEnemy(int world) //与scene中的createFood等一样
{
if (world == 1)
{
Enemy emy[] = { { 3,8,1 },{ 18,7,-1 },{ 25,7,1 },{ 28,8,-1 },{ 33,8,-1 },{ 39,8,1 },{ 68,3,1 },{ 66,8,-1 },{ 81,6,1 },{ 92,6,1 } };
int i = 0;
while (i <= sizeof(emy) / sizeof(emy[0]))
{
myEnemy[i].x = emy[i].x*WIDTH;
myEnemy[i].y = emy[i].y*HEIGHT;
myEnemy[i].turn = emy[i].turn;
i++;
}
}
else if (world == 2)
{
Enemy emy[] = { { 18,8,1 },{ 24,5,1 },{ 28,5,-1 },{ 44,9,1 },{ 97,6,1 },{ 99,4,1 } };
int i = 0;
while (i <= sizeof(emy) / sizeof(emy[0]))
{
myEnemy[i].x = emy[i].x*WIDTH;
myEnemy[i].y = emy[i].y*HEIGHT;
myEnemy[i].turn = emy[i].turn;
i++;
}
}
else if (world == 3)
{
Enemy emy[] = { { 25,3,-1 },{ 31,3,1 },{ 39,3,-1 },{ 50,3,-1 },{ 68,3,1 },{ 66,8,-1 },{ 86,8,1 } };
int i = 0;
while (i <= sizeof(emy) / sizeof(emy[0]))
{
myEnemy[i].x = emy[i].x*WIDTH;
myEnemy[i].y = emy[i].y*HEIGHT;
myEnemy[i].turn = emy[i].turn;
i++;
}
}
}
void role::setBomb(int x, int y) //碰到墙体、最大距离、敌人时爆炸 传入xy
{
for (int i = 0; i<BOMB_NUMBER; i++)
{
if (bombs[i].x == 0 && bombs[i].y == 0)
{
bombs[i].x = x;
bombs[i].y = y;
bomb_iframe[i] = 1;
break;
}
}
}
void role::setBullet(int x, int y)
{
for (int i = 0; i<BULLET_NUMBER; i++)
{
if (bullets[i].x == 0 && bullets[i].y == 0)
{
bullets[i].x = x;
bullets[i].y = y;
bullets[i].turn = myHero.turn;
bullet_iframe[i] = 1;
break;
}
}
}
void role::bullteFlying(Bullet *p, scene *myScene) //子弹 //改良:最大距离
{
if (p->x == 0 && p->y == 0)
return;
if (p->x>XSIZE || p->x<-WIDTH)
{
p->x = 0;
p->y = 0;
p->turn = 0;
return;
}
Enemy *emy = touchEnemy(p->x, p->y, myEnemy);
Map *map = touchMap(p->x, p->y, myScene);
if (emy != NULL || map != NULL|| p->x>MAX_DISTANCE)//如果子弹打到敌人或者墙或者达到最大距离
{
if (emy != NULL)
{
mciSendString("play music_boom2 from 0", NULL, 0, NULL);
score += 5;
}
else
mciSendString("play music_boom from 0", NULL, 0, NULL);
//记下爆炸点的坐标
int x, y;
if (emy != NULL)
{
x = emy->x;
y = emy->y;
emy->x = 0;
emy->y = 0;
emy->turn = 0;
p->x = 0;
p->y = 0;
p->turn = 0;
}
else
{
x = (-(int)myHero.x0 + p->x) / WIDTH*WIDTH;
if (p->turn == -1)
x = (-(int)myHero.x0 + p->x + WIDTH) / WIDTH*WIDTH;
y = p->y;
p->x = 0;
p->y = 0;
p->turn = 0;
}
setBomb(x, y);
}
else
p->x += LEHGTH_INTERVAL_BULLET*p->turn; //子弹飞行
}
bool role::isTouch(POINT *p1, POINT *p2)
{
for (int i = 0; i<4; i++)//如果主角4个顶点有一个顶点在地图内,则判断为接触到地图
{
if (p1[i].x >= p2[0].x&&p1[i].y >= p2[0].y&&p1[i].x <= p2[1].x&&p1[i].y <= p2[1].y)
{
return true;
}
}
return false;
}
Map *role::touchMap(int x, int y, scene *myScene) //碰撞
{
//总体思路是判断主角的四个顶点是否至少有一个在地图内,有,则判断为主角碰撞到地图
int xmap = (int)myHero.x0; //图片坐标
POINT r[4];//主角4个顶点的坐标(比主角小一圈)
r[0].x = -xmap + x + 1;
r[0].y = y + 1;
r[1].x = -xmap + x + WIDTH - 1;
r[1].y = y + 1;
r[2].x = -xmap + x + 1;
r[2].y = y + HEIGHT - 1;
r[3].x = -xmap + x + WIDTH - 1;
r[3].y = y + HEIGHT - 1;
int i = 0;
//设定碰撞框与碰撞
while (myScene->getMap()[i].id>0 && myScene->getMap()[i].id<11 && i<MAP_NUMBER)//条件:地图存在且为前10种地图(地形) //后面的id是景色
{
POINT m[2];//地图左上角顶点和右下角顶点的坐标
m[0].x = myScene->getMap()[i].x*WIDTH;
m[0].y = myScene->getMap()[i].y*HEIGHT;
if (myScene->getMap()[i].id == 10)//id为10的地图要大些
{
m[1].x = myScene->getMap()[i].x*WIDTH + myScene->getMap()[i].xAmount * 2 * WIDTH;
m[1].y = myScene->getMap()[i].y*HEIGHT + myScene->getMap()[i].yAmount * 2 * HEIGHT;
}
else
{
m[1].x = myScene->getMap()[i].x*WIDTH + myScene->getMap()[i].xAmount*WIDTH;
m[1].y = myScene->getMap()[i].y*HEIGHT + myScene->getMap()[i].yAmount*HEIGHT;
}
if (isTouch(r, m))//如果两个矩形相交,则碰撞
return &(myScene->getMap()[i]);
i++;
}
return NULL;
}
POINT *role::touchCoins(int x, int y, scene *myScene)
{
//与碰撞地形的思路是一样的
//总体思路是判断主角的四个顶点是否至少有一个在硬币内,有,则判断为主角碰吃到硬币
int xmap = (int)myHero.x0;
POINT r[4];//主角4个顶点的坐标(比主角小一圈)
r[0].x = -xmap + x + 1;
r[0].y = y + 1;
r[1].x = -xmap + x + WIDTH - 1;
r[1].y = y + 1;
r[2].x = -xmap + x + 1;
r[2].y = y + HEIGHT - 1;
r[3].x = -xmap + x + WIDTH - 1;
r[3].y = y + HEIGHT - 1;
int i = 0;
while (i<COINS_NUMBER) //条件:存在硬币
{
POINT m[2];//硬币左上角顶点和右下角顶点的坐标
m[0].x = myScene->getCoins()[i].x*WIDTH;
m[0].y = myScene->getCoins()[i].y*HEIGHT;
m[1].x = m[0].x + WIDTH;
m[1].y = m[0].y + HEIGHT;
if (isTouch(r, m))//如果两个矩形相交,则碰撞
return &(myScene->getCoins()[i]);
i++;
}
return NULL;
}
POINT *role::touchFood(int x, int y, scene *myScene)
{
//总体思路是判断主角的四个顶点是否至少有一个在食物内,有,则判断为主角碰吃到食物
int xmap = (int)myHero.x0;
POINT r[4];//主角4个顶点的坐标(比主角小一圈)
r[0].x = -xmap + x + 1;
r[0].y = y + 1;
r[1].x = -xmap + x + WIDTH - 1;
r[1].y = y + 1;
r[2].x = -xmap + x + 1;
r[2].y = y + HEIGHT - 1;
r[3].x = -xmap + x + WIDTH - 1;
r[3].y = y + HEIGHT - 1;
int i = 0;
while (i<FOOD_NUMBER) //条件:存在食物
{
POINT m[2];//食物左上角顶点和右下角顶点的坐标
m[0].x = myScene->getFood()[i].x;
m[0].y = myScene->getFood()[i].y;
m[1].x = m[0].x + 3 * WIDTH / 2 + 4;
m[1].y = m[0].y + 4 * HEIGHT / 5;
if (isTouch(r, m))//如果两个矩形相交,则碰撞
return &(myScene->getFood()[i]);
i++;
}
return NULL;
}
Enemy *role::touchEnemy(int x, int y, Enemy *emy)
{
//与之前碰撞思路一样
//总体思路是判断主角的四个顶点是否至少有一个在地图内,有,则判断为主角碰撞到敌人
int xmap = (int)myHero.x0;
POINT r[4];//主角4个顶点的坐标(比主角小一圈)
r[0].x = -xmap + x + 1;
r[0].y = y + 1;
r[1].x = -xmap + x + WIDTH - 1;
r[1].y = y + 1;
r[2].x = -xmap + x + 1;
r[2].y = y + HEIGHT - 1;
r[3].x = -xmap + x + WIDTH - 1;
r[3].y = y + HEIGHT - 1;
int i = 0;
while (i<ENEMY_TOTE)//条件:存在敌人
{
if (emy[i].turn != 0)
{
POINT m[2];//敌人左上角顶点和右下角顶
Scikit-learn
- 粉丝: 4778
- 资源: 3181
最新资源
- 六轴,scara机器人运动学分析,建模和运动控制 matlab,simulink,simscape.机器人工具箱,DH建模 Pd控制,滑模控制,模糊控制等等
- 车辆,汽车检测1-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 美国旧金山28R粗糙跑道数据
- mp3转换器小程序-音频20241222115740.mp3
- 车辆船只检测8-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord、VOC数据集合集.rar
- Questasim仿真脚本2
- Questasim仿真脚本
- Django开发中常见问题与解决方案的全面指南
- 西工大noj 116题及答案word版.doc
- 四足机器人步态控制算法的技术原理及Python实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈