/*windows.h 的png透明贴图工具*/void random_nums()//一帧内生成十个的随机数,前五个赋值给龙的判定移动变量,后五个给龙的追击判定变量{ int num = 10; int used[100] = { 0 }; // 标记数组,初始化为 0 int numbers[10]; srand((unsigned int)time(NULL)); // 初始化随机数种子 for (int i = 0; i < num; i++) { int num; do { num = rand() % 100; // 生成 0 到 RANGE - 1 之间的随机数 } while (used[num]); // 如果该数字已被利用,则重新生成 numbers[i] = num; used[num] = 1; // 标记该数字已被利用 } // 输出生成的随机数 for (int i = 1; i <= num / 2; i++) { dragon_rand_move_num[i] = numbers[i]; } for (int i = num / 2 + 1;i <= num;i++) { dragon_rand_pursuit_num[i - num / 2] = numbers[i]; }}//一帧内生成特定数目的随机数int cycle_count(int min, int max, int type)//调用返回值从min~max之间的单向循环{ static int count[10]; while (count[type] < min - 1) count[type]++; count[type]++; if (count[type] > max) count[type] = min; return count[type];}//不同type参数分配不同的静态变量count/*可控范围的底码循环,用于运动图片的切换*/void control_hero()//控制人物移动
{
if (GetAsyncKeyState('D') && hero.x < 1550)//角色右移
{
hero.x += HERO_SPEED;
Previous_direction = true;
}
if (GetAsyncKeyState('A') && hero.x > -5)//角色左移
{
hero.x -= HERO_SPEED;
Previous_direction = false;
}
if (GetAsyncKeyState('W') && hero.y > -5)//角色上移
hero.y -= HERO_SPEED;
if (GetAsyncKeyState('S') && hero.y < 850)//角色下移
hero.y += HERO_SPEED;
}
/*控制角色移动*/
void creat_sword(){ if (GetAsyncKeyState('J')) { for (int i = 1;i <= 10;i++) { if (!light_sword[i].live) { light_sword[i].live = true; light_sword[i].x = hero.x - 100;//光刃继续人物前坐标释放 light_sword[i].y = hero.y - 100; if (Previous_direction)//是否朝右 light_sword[i].direction = true; else light_sword[i].direction = false; break; } } }}//创造光刃void move_sword(){ for (int i = 1;i <= 10;i++) { if (light_sword[i].live) { if (light_sword[i].direction)//是否朝右 light_sword[i].x += LIGHT_SWORD_SPEED; else light_sword[i].x -= LIGHT_SWORD_SPEED; } }}//移动光刃void draw_sword(){ for (int i = 1;i <= 10;i++) if (light_sword[i].live) { if (light_sword[i].direction) transparentimage3(NULL, light_sword[i].x, light_sword[i].y, &light_sword_imgR); else transparentimage3(NULL, light_sword[i].x, light_sword[i].y, &light_sword_imgL); }}//绘画光刃void draw_HPMP(){ transparentimage3(NULL, 10, 10, &HP_img); transparentimage3(NULL, 10, 70, &MP_img);}//状态栏的构建void Select_texture()//选择使命状态并且画图{ if (GetAsyncKeyState('D'))//是否按下D { transparentimage3(NULL, hero.x, hero.y, &run_imgR[run_num]); } else {//没有按下D if (GetAsyncKeyState('A'))//是否按下A { transparentimage3(NULL, hero.x, hero.y, &run_imgL[run_num]); } else {//没有按下A if (GetAsyncKeyState('W') || GetAsyncKeyState('S')) {//是否按下W或S if (Previous_direction)//是否右朝向 transparentimage3(NULL, hero.x, hero.y, &run_imgR[run_num]);//右朝向上下移动 else//左朝向 transparentimage3(NULL, hero.x, hero.y, &run_imgL[run_num]);//左朝向上下移动 } else {//待机动作 if (Previous_direction)//是否右朝向 transparentimage3(NULL, hero.x, hero.y, &stop_imgR[stop_num]);//待机右朝向 else//左朝向 transparentimage3(NULL, hero.x, hero.y, &stop_imgL[stop_num]);//待机左朝向 } } }}//人物动作状态的选择判定画图void putback(){ putimage(0, 0, &back);}//背景图的绘画void beyond_sword_boundary(){ for (int i = 1;i <= 10;i++) if (light_sword[i].x<0 || light_sword[i].x>WIDTH) light_sword[i].live = false;}//超出边界的光刃判定消失void creat_add_HP()//创造加血特效 (内含按键 U ){//触发条件,查验按键“U" 并且 特效不存活 并且 特效已完成 if (GetAsyncKeyState('U') && !add_blood.live) add_blood.live = true;}//创造加血特效 (内含按键 U )int control_effect_count(int min, int max, bool* live, int type)//控制特效的单次循环运行{ static int count[10] = { min - 1 }; count[type]++; if (count[type] >= max + 1) { *live = false; count[type] = min - 1; return count[type] + 1; } return count[type];}控制特效的单次便利图像运行,单次便利结束后,将传入的bool类型指针变为falsevoid draw_effect_ADD_blood(){ if (add_blood.live) transparentimage3(NULL, hero.x - 100, hero.y - 150, &TX_ADD_HP[TX_ADD_HP_num]);}//加血特效的绘画void select_dragon_speed() //根据距离分配速率{ for (int i = 1;i <= DRAGON_NUM_MAX;i++) if (dragon[i].pursuit && dragon[i].live) {//同时满意追击和移动和存活条件后,赋值追击速率 double cx = (double)(dragon[i].x - hero.x); //敌我x坐标差 double cy = (double)(dragon[i].y - hero.y); //敌我y坐标差 double cz = sqrt(cx * cx + cy * cy); //绝对距离 if (cx == 0 && cy == 0)//防止敌我目标重合带来的除0bug { cz = 1; } double cxz = cx / cz; double cyz = cy / cz;//移动方向参数 dragon[i].speed_x = (int)(-DRAGON_SPEED * cxz); dragon[i].speed_y = (int)(-DRAGON_SPEED * cyz);//分配速率 }}//根据敌我位移分配速度和状态
void dragon_move()
{
for (int i = 1;i <= DRAGON_NUM_MAX;i++)
{
if (dragon[i].live && dragon[i].pursuit)
{//基本移动
dragon[i].x += dragon[i].speed_x;
dragon[i].y += dragon[i].speed_y;
}
if (dragon[i].speed_x > 0)
dragon[i].direction = false;
else
dragon[i].direction = true;
}
}
//龙的移动void draw_dragon(){ for (int i = 1;i <= DRAGON_NUM_MAX;i++) if (dragon[i].live) { if (dragon[i].direction) transparentimage3(NULL, dragon[i].x, dragon[i].y, &dragon_imgR[dragon_img_num]); else transparentimage3(NULL, dragon[i].x, dragon[i].y, &dragon_imgL[dragon_img_num]); }}//龙的绘画void Stop_the_Dragon_Crossing_Realm()//克制龙的越界{ for (int i = 1;i <= DRAGON_NUM_MAX;i++) { if (dragon[i].x <= 20)// 注意30-20要 > speed_x,防止瞬间越界 { dragon[i].x = 30; dragon[i].speed_x = -dragon[i].speed_x; } if (dragon[i].x >= 1680)// 注意980-970要 > speed_x,防止瞬间越界 { dragon[i].x = 1670; dragon[i].speed_x = -dragon[i].speed_x; } if (dragon[i].y <= 20)// 注意30-20要 > speed_y,防止瞬间越界 { dragon[i].y = 30; dragon[i].speed_y = -dragon[i].speed_y; } if (dragon[i].y >= 980)// 注意1680-1670要 > speed_y,防止瞬间越界 { dragon[i].y = 970; dragon[i].speed_y = -dragon[i].speed_y; } }}//克制龙越界void creat_dragon(){ for (int i = 1;i <= DRAGON_NUM_MAX;i++) { if (dragon[i].HP <= 0 && dragon[i].live) { dragon[i].die_num_zhen = 0; dragon[i].live = false; //dragon[i].deathTime = clock(); // 更新死亡时间 } if (!dragon[i].live) { if (dragon[i].die_num_zhen <= 4)//4*0.5=2s continue; //if (clock() - dragon[i].deathTime < 2000) continue; // 5 秒内不重新生成 dragon[i].x = 800; dragon[i].y = 500; dragon[i].live = true; dragon[i].HP = 100; // 重新生成时恢复血量 break; } }}//创造龙,附带空地才创造void dragon_x_dragon()//两条龙之间保持距离,克制重叠{ for (int i = 1;i <= DRAGON_NUM_MAX;i++) { for (int a = 1;a <= i;a++) { if (dragon[i].x - dragon[a].x <= 200 && dragon[i].x - dragon[a].x > 0) {// dragon[i]在左 <- -> dragon[i+1]在右 if (dragon[a].speed_x > 0) dragon[a].speed_x = 0;//如果左边的在右移则水平克制 if (dragon[i].speed_x < 0) dragon[i].speed_x = 0; } if (dragon[a].x - dragon[i].x <= 200 && dragon[a].x - dragon[i].x > 0) {// dragon[i+1]在左 <- -> dragon[i]在右 if (dragon[i].speed_x > 0) dragon[i].speed_x = 0; if (dragon[a].speed_x < 0) dragon[a].speed_x = 0; } } }}//两条龙之间保持距离,克制重叠,该函数需要放到获取所有速率之后void draw_light_effect(){ for (int i = 1;i <= 10;i++) if (Affected_effect[i].live) transparentimage3(NULL, Affected_effect[i].x, Affected_effect[i].y, &light_effect[Affected_img_num]);}//对基本光刃受击特效的绘画void Attack_detection(){ for (int i = 1;i <= 10;i++) { int ctr = 1; for (int a = 1;a <= DRAGON_NUM_MAX;a++) { if (light_sword[i].x - dragon[a].x<200 && light_sword[i].x - dragon[a].x>-200 && light_sword[i].live) if (dragon[a].live) if (light_sword[i].y - dragon[a].y<0 && light_sword[i].y - dragon[a].y>-200) { dragon[a].HP -= 20; Affected_effect[i].x = dragon[a].x + 50; Affected_effect[i].y = dragon[a].y + 30; Affected_effect[i].live = true; light_sword[i].live = false; ctr = 0; break; } } if (ctr == 0) break; }}//基本光刃掷中判定以及反馈//=========================================================================功能函数的构建void timer_thing()//需要时间延迟的事件集合{ if (timer(100, 1)) {//脚色待机动作速率 stop_num = cycle_count(1, 12, 1); } if (timer(60, 2)) {//脚色奔驰动作速率 run_num = cycle_count(1, 4, 2); } if (timer(50, 3)) //防止一瞬间释放过多的光刃 { creat_sword();//控制光刃释放(控制按键:J ) } if (timer(50, 4) && add_blood.live)//控制加血特效图片运行的延迟 { TX_ADD_HP_num = control_effect_count(1, 15, &add_blood.live, 1); } if (timer(100, 5)) //控制龙的动作图片 { dragon_img_num = cycle_count(1, 6, 3); } if (timer(2000, 7)) { creat_dragon();//创造龙 } if (timer(10, 8)) { dragon_move();//龙的移动 } if (timer(10, 9)) {//基础光刃攻击受击特效速率控制 for (int i = 1;i <= 10;i++) Affected_img_num = control_effect_count(1, 30, &Affected_effect[i].live, 2); } if (timer(500, 10)) { for (int i = 1;i <= DRAGON_NUM_MAX;i++) { if (!dragon[i].live) dragon[i].die_num_zhen++; } }}//需要时间延迟的事件集合,内含J按键int main()
游戏已经被我封装好,分享到了网盘上,感情兴趣的可以实验一下。
通过网盘分享的文件:封装游戏测试.zip链接:
https://pan.baidu.com/s/1indM1boxj6QvrpsaIH_85Q?pwd=LONG
提取码: LONG
提取后利用方法:
利用文件资源管理器打开,点击Debug
点击该运行文件,
点击全部解压缩
同样再找到该运行文件并运行
一步步操作
上述您选择安装的指定位置(一样平常息争压后的文件一个位置)
就会出现一个软件,点击运行,就可以玩了
(注意:同时打开的有一个黑框框,最小化即可,不要关掉,他会获取用户的按键操作)
(W A S D 移动 U 特效 J 攻击)(只是一个基础2D游戏框架,未添加太多功能,感兴趣的小伙伴可以按照喜好实验添加)