寒假花了小半个月做了一个小游戏。WOW。
我发觉游戏其实就是一帧又一帧的图像变化,其中涉及鼠标点击移动、键盘控制、声音控制等方面的操作。在此介绍一些自己学到的基础的东西。
窗口输出和图像输出:
initgraph(l, w, EX_SHOWCONSOLE);//lw控制长宽(矩形),EX_SHOWCONSOLE可以同时显示程序运行窗口。
IMAGE BEIJING;//首先需要定义IMAGE类型。
IMAGE feiji[10];
//加载
loadimage(&BEIJING, "飞机大战/背景图2.png",x,y);
loadimage(feiji+n, "飞机大战/feiji.png",x,y);//注意类型,其中xy表示控制长宽,不填则为图片本身分辨率。
//输出
putimage(feiji[i].x, feiji[i].y, feiji + n);//其中feiji + n指代输出第几张特定图片。
//透明贴图函数
void transparentimage(IMAGE* dstimg, int x, int y, IMAGE* srcimg, UINT transparentcolor)
{
HDC dstDC = GetImageHDC(dstimg);
HDC srcDC = GetImageHDC(srcimg);
int w = srcimg->getwidth();
int h = srcimg->getheight();
// 使用 Windows GDI 函数实现透明位图
TransparentBlt(dstDC, x, y, w, h, srcDC, 0, 0, w, h, transparentcolor);
}//使用改变putimage为transparentimage
transparentimage(NULL,pthis->x, pthis->y, myplanes + pthis->frame,BLACK);
//第一项NULL不用改变,xy为位置, myplanes + pthis->frame为输出特定图片,统一BLACK。
//PS:我本人在使用这透明贴图的时候,图片类型只有为GIF的空背景图片才可以成功。
鼠标界面控制:
bool isin(ExMessage* msg, int x, int y, int w, int h)
{
if (msg->x >= x && msg->x <= x + w && msg->y >= y && msg->y <= y + h)
{
return true;
}
return false;
}
startupScene(ExMessage* msg)
{
if (msg->message == WM_LBUTTONDOWN)
{
if (isin(msg, 345, 192, 310, 83))//这个AC调整宽度,BD调整长度
{
//跳转界面操作;
}
}
while (true)
{
ExMessage msg;//msg结构体
while (peekmessage(&msg, EX_MOUSE))
{
startupScene(&msg);
if (go)break;//自己设置的条件
}
if (go)break;
}
键盘输入控制:
GetAsyncKeyState(VK_UP);//VK表示键盘up表示键盘右下角那边的向上箭头符号
GetAsyncKeyState(VK_DOWN);
GetAsyncKeyState(VK_LEFT);
GetAsyncKeyState(VK_RIGHT);
GetAsyncKeyState(VK_SPACE);//空格
//用法
if (GetAsyncKeyState(VK_UP) && //条件(可以没有))
{
//输出;
}
计时器:
由于电脑运行极快,我们点击一下键盘可能电脑传输了很多次,所以为了减缓这种效果,可以使用自定义定时器,但是要注意,定时器设置不能够重叠,否则可能将导致紊乱,即BUG出现。
bool timer(int ms, int id)
{
static int start[1500];//定时器数量
int end = clock();
if (end - start[id] >= ms)
{
start[id] = end;
return true;
}
return false;
}
//用法:
if (timer(600, 100))
{
//;
}
Beep函数:
Beep(Music[i].s * x, Music[i].t * y);//x控制音调,y控制发音时长。
Seelp函数:
Sleep(x);//其中x单位是毫秒。让程序暂停xms。
附上游戏代码:
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<easyx.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<map>
#include<list>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<stack>
#include<limits>
#include <graphics.h>
#include <conio.h>
#pragma comment( lib, "MSIMG32.LIB")
#define BULLET_NUM 50
#define ENEMY_NUM 200
using namespace std;
void transparentimage(IMAGE* dstimg, int x, int y, IMAGE* srcimg, UINT transparentcolor)
{
HDC dstDC = GetImageHDC(dstimg);
HDC srcDC = GetImageHDC(srcimg);
int w = srcimg->getwidth();
int h = srcimg->getheight();
// 使用 Windows GDI 函数实现透明位图
TransparentBlt(dstDC, x, y, w, h, srcDC, 0, 0, w, h, transparentcolor);
}
IMAGE myplanes[2];
IMAGE BEIJING;
IMAGE bullets[5];
IMAGE img_enemy[10];
IMAGE failed;
IMAGE WIN;
IMAGE baozha;
IMAGE dadaodanp;
int hhhh, uu, sum1, sum; bool queding = false;
//音乐
enum Fy
{
d1 = 262, d1_ = 277, d2 = 294, d2_ = 311, d3 = 330, d4 = 349, d5 = 392, d5_ = 415,
d6 = 440, d6_ = 466, d7 = 494, z1 = 523, z1_ = 554, z2 = 578, z2_ = 622, z3 = 659,
z4 = 698, z4_ = 740, z5 = 784, z5_ = 831, z6 = 880, z6_ = 932, z7 = 988, g1 = 1046,
g1_ = 1109, g2 = 1175, g2_ = 1245, g3 = 1318, g4 = 1397, g4_ = 1480,
g5 = 1568, g5_ = 1661, g6 = 1760, g6_ = 1865, g7 = 1976, yaya = 0
};
struct Yf
{
enum Fy s;
int t;
};
struct Yf Music[1000] =
{
{z6,50},{z7,50},{g1,150},{z7,50},{g1,100}, //5
{g3,100},{z7,300},{z3,100},{z6,150},{z5,50}, //10
{z6,100},{g1,100},{z5,300},{z2,50}, //14
{z3,50},{z4,150},{z3,50},{z4,50},{g1,150}, //19
{z3,150},{z2,50},{z3,50},{g1,150},{z7,150}, //24
{z4_,50},{z4_,100},{z7,100},{z7,200},{z6,50}, //29
{z7,50},{g1,150},{z7,50},{g1,100},{g3,100}, //34
{z7,200},{z3,100},{z6,150}, //37
{z5,50},{z6,100},{g1,100},{z5,300},{z3,100},{z4,100},{g1,50}, //44
{z7,150},{g1,100},{g2,100}, //47
{g3,50},{g1,150},{g1,50},{z7,50},{z6,100},{z7,100},{z5_,100}, //54
{z6,300},{g1,50},{g2,50}, //57
{g3,150},{g2,50},{g3,100},{g5,100},{g2,300},{z5,100}, //63
{g1,150},{z7,50},{g1,100},{g3,100},{g3,300},{z6,50},{z7,50},{g1,150}, //71
{z7,50},{g1,100},{g2,100},{g1,150},{z5,50}, //76
{z5,200},{g4,100},{g3,100},{g2,100},{g1,100}, //81
{g3,400},{yaya,50},{g3,50},{g6,200},{g5,100},{g5,100},{g3,50}, //88
{g2,50},{g1,100},{yaya,50},{g1,50},{g2,100},{g1,50},{g2,100},{g5,100}, //96
{g3,200},{yaya,50},{g3,50},{g6,200},{g5,200},{g3,50},{g2,50}, //103
{g1,200},{yaya,50},{g1,50},{g2,100},{g1,50},{g2,100},{z7,100}, //110
{z6,200},{yaya,100},{z6,50},{z7,50},{z6,500}
};
//结构体大构造
struct myplane
{
int x;
int y;
int h;
bool isdie;
int frame;
}gamer, enemy[ENEMY_NUM], boss;
struct BULLET
{
int x;
int y;
bool isdie;
}bullet[BULLET_NUM], bullet0[BULLET_NUM], bullet1[BULLET_NUM], bullet3[BULLET_NUM], bossgg[BULLET_NUM], dadaodan[BULLET_NUM];
bool timer(int ms, int id)
{
static int start[1500];
int end = clock();
if (end - start[id] >= ms)
{
start[id] = end;
return true;
}
return false;
}
//小怪状态
void loadresource()//加载文件
{
// 加载图片
loadimage(&BEIJING, "飞机大战/背景图2.png");
loadimage(myplanes + 0, "飞机大战/myplane.gif", 80, 80);
loadimage(myplanes + 1, "飞机大战/myplane2.gif", 80, 80);
loadimage(bullets + 0, "飞机大战/子弹.gif", 10, 20);
loadimage(bullets + 1, "飞机大战/子弹1.png", 5, 10);
loadimage(bullets + 2, "飞机大战/子弹3.png", 5, 10);
loadimage(bullets + 3, "飞机大战/子弹4.png", 5, 10);
loadimage(img_enemy + 0, "飞机大战/敌人plus.gif");
loadimage(img_enemy + 1, "飞机大战/敌人plus2.gif", 75, 75);
loadimage(img_enemy + 2, "飞机大战/敌人2.gif", 60, 60);
loadimage(img_enemy + 3, "飞机大战/敌人plus4.gif", 100, 100);
loadimage(img_enemy + 4, "飞机大战/BOSS.png", getwidth(), 200);
loadimage(&failed, "飞机大战/失败.png", getwidth(), getheight());
loadimage(&WIN, "飞机大战/win.png", getwidth(), getheight());
loadimage(&BEIJING, "飞机大战/背景图2.png");
loadimage(&baozha, "飞机大战/baozha.gif", 60, 60);
loadimage(&dadaodanp, "飞机大战/大导弹.gif", 20, 35);
}
void createbullet()
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (bullet[i].isdie)
{
bullet[i].x = gamer.x + myplanes->getwidth() / 2 - 5;
bullet[i].y = gamer.y;
bullet[i].isdie = false;
break;
}
}
}
void createebullet0(myplane* ethis, int k, BULLET* bthis)
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (bullet0[i].isdie)
{
bullet0[i].x = ethis[k].x + img_enemy[ethis[k].frame].getwidth() / 2;
bullet0[i].y = img_enemy[ethis[k].frame].getheight() + ethis[k].y;
bullet0[i].isdie = false;
break;
}
}
}
void createebullet1(myplane* ethis, int k)
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (bullet1[i].isdie)
{
bullet1[i].x = ethis[k].x + img_enemy[ethis[k].frame].getwidth() / 2;
bullet1[i].y = img_enemy[ethis[k].frame].getheight() + ethis[k].y;
bullet1[i].isdie = false;
break;
}
}
}
void createebullet3(myplane* ethis, int k)
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (bullet3[i].isdie)
{
bullet3[i].x = ethis[k].x + img_enemy[ethis[k].frame].getwidth() / 2;
bullet3[i].y = img_enemy[ethis[k].frame].getheight() + ethis[k].y;
bullet3[i].isdie = false;
break;
}
}
}
void plane_chushi(myplane* pthis, int x, int y)
{
pthis->x = x;
pthis->y = y;
pthis->h = hhhh;
pthis->isdie = false;
pthis->frame = 0;
}
void plane_draw(myplane* pthis)
{
transparentimage(NULL, pthis->x, pthis->y, myplanes + pthis->frame, BLACK);//透明——>drawIng
pthis->frame = (pthis->frame + 1) % 2;
}
int G = 5;
void plane_move(myplane* pthis)//键盘输入移动
{
if (queding)G += 3;
if (GetAsyncKeyState(VK_UP) && pthis->y > 0)
{
pthis->y -= G;
}
if (GetAsyncKeyState(VK_DOWN) && pthis->y + myplanes->getheight() / 2 < getheight())
{
pthis->y += G;
}
if (GetAsyncKeyState(VK_LEFT) && pthis->x + myplanes->getwidth() / 2 > 0)
{
pthis->x -= G;
}
if (GetAsyncKeyState(VK_RIGHT) && pthis->x + myplanes->getwidth() / 2 < getwidth())
{
pthis->x += G;
}
if (GetAsyncKeyState(VK_SPACE) && timer(200, 0))
{
createbullet();
}
}
int cnt = 0;
void createnemy()
{
for (int i = 0; i < ENEMY_NUM; i++)
{
if (enemy[i].isdie)
{
enemy[i].x = rand() % getwidth();
enemy[i].y = -img_enemy->getheight();
enemy[i].isdie = false;
enemy[i].frame = cnt++;
enemy[i].h = 1;
if (cnt == 4)cnt = 0;
break;
}
}
}
void moveenemy()
{
for (int i = 0; i < ENEMY_NUM; i++)
{
if (!enemy[i].isdie)
{
if (enemy[i].frame == 0)
{
enemy[i].y += 3;
}
else
if (enemy[i].frame == 1)
{
enemy[i].y += 2;
}
else
if (enemy[i].frame == 2)
{
enemy[i].y += 6;
}
else if (enemy[i].frame == 3)
{
enemy[i].y += 1;
}
if (enemy[i].y >= getheight())
{
enemy[i].isdie = true;
}
}
}
}
void createnemybullet(myplane* ethis)
{
for (int i = 0; i < ENEMY_NUM; i++)
{
if (!ethis[i].isdie && ethis[i].frame != 2 && timer(400, 10))
{
if (ethis->frame == 0)
{
createebullet0(enemy, i, bullet0);
}
if (ethis->frame == 1/*&& timer(400, 200)*/)
{
createebullet1(enemy, i);
}
if (ethis->frame == 3/*&& timer(600, 300)*/)
{
createebullet3(enemy, i);
}
}
}
}
void movebullet()
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (!bullet[i].isdie)
{
bullet[i].y -= 8;
if (bullet[i].y < 0)
{
bullet[i].isdie = true;
}
}
if (!bullet0[i].isdie)
{
bullet0[i].y += 8;
if (bullet0[i].y > getheight())
{
bullet0[i].isdie = true;
}
}
if (!bullet1[i].isdie)
{
bullet1[i].y += 8;
if (bullet1[i].y > getheight())
{
bullet1[i].isdie = true;
}
}
if (!bullet3[i].isdie)
{
bullet3[i].y += 8;
if (bullet3[i].y > getheight())
{
bullet3[i].isdie = true;
}
}
if (!bossgg[i].isdie)
{
bossgg[i].y += 8;
if (bossgg[i].y > getheight())
{
bossgg[i].isdie = true;
}
}
if (!dadaodan[i].isdie)
{
dadaodan[i].y += 8;
if (dadaodan[i].y > getheight())
{
dadaodan[i].isdie = true;
}
}
}
}
void init()
{
loadresource();
plane_chushi(&gamer, (getwidth() - myplanes->getwidth()) / 2, getheight() - myplanes->getheight());
// 初始化子弹
for (int i = 0; i < BULLET_NUM; i++)
{
bullet[i].isdie = true;
bullet0[i].isdie = true;
bullet1[i].isdie = true;
bullet3[i].isdie = true;
bossgg[i].isdie = true;
dadaodan[i].isdie = true;
}
//初始化敌人
for (int i = 0; i < ENEMY_NUM; i++)
{
enemy[i].isdie = true;
}
}
void draw()//绘制界面 :飞机和背景
{
putimage(0, 0, &BEIJING);
plane_draw(&gamer); int tg = 1;
for (int i = 0; i < BULLET_NUM; i++)
{
if (!bullet[i].isdie)
{
transparentimage(NULL, bullet[i].x, bullet[i].y, bullets + 0, BLACK);
}
if (!bullet0[i].isdie)
{
putimage(bullet0[i].x, bullet0[i].y, bullets + 3);
}
if (!bullet1[i].isdie)
{
putimage(bullet1[i].x, bullet1[i].y, bullets + 1);
}
if (!bullet3[i].isdie)
{
putimage(bullet3[i].x, bullet3[i].y, bullets + 2);
}
if (!bossgg[i].isdie)
{
putimage(bossgg[i].x, bossgg[i].y, bullets + tg);
tg++; if (tg == 4)tg = 0;
}
if (!dadaodan[i].isdie)
{
transparentimage(NULL, dadaodan[i].x, dadaodan[i].y, &dadaodanp, BLACK);
}
}
for (int i = 0; i < ENEMY_NUM; i++)
{
if (!enemy[i].isdie)
{
transparentimage(NULL, enemy[i].x, enemy[i].y, img_enemy + enemy[i].frame, BLACK);
}
}
}
void panduan(BULLET* pthis, myplane* ethis, myplane* mythis)
{
int k = 99 / 100; int yy = 0;
for (int i = 0; i < BULLET_NUM; i++)
{
if (!pthis[i].isdie)
{
for (int j = 0; j < ENEMY_NUM; j++)
{
if (!ethis[j].isdie)
{
if ((pthis[i].x >= ethis[j].x && (pthis[i].x <= ethis[j].x + img_enemy[enemy[j].frame].getwidth())) && (pthis[i].y <= ethis[j].y + k * img_enemy[enemy[j].frame].getheight()))
{
pthis[i].isdie = true;
ethis[j].h--;
}
if (ethis[j].frame == 0)//普通
{
if (ethis[j].h == -1)
{
Beep(Music[i].s, Music[i].t * 1);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
uu++; cout << "您击败了敌机,获取积分+1,目前的总积分为:" << uu << endl;
cout << "--------------------------------\n";
cout << "BOSS还需要:" << sum1 - uu << " 积分被你吸引!\n";
ethis[j].isdie = true; /* Beep(Music[yy].s, Music[yy].t * 5),yy++; uu++;*/
}
}
else
if (ethis[j].frame == 1 || ethis[j].frame == 2)//攻速,移速
{
if (!ethis[j].h)
{
Beep(Music[i].s, Music[i].t * 1);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
uu++; cout << "您击败了敌机,获取积分+1,目前的总积分为:" << uu << endl;
cout << "--------------------------------\n";
cout << "BOSS还需要:" << sum1 - uu << " 积分被你吸引!\n";
ethis[j].isdie = true; /* Beep(Music[yy].s, Music[yy].t * 5), yy++; uu++;*/
}
}
else
if (ethis[j].frame == 3)//防御
{
if (ethis[j].h == -3)
{
Beep(Music[i].s, Music[i].t * 1);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
transparentimage(NULL, ethis[j].x + 10, ethis[j].y + 10, &baozha, BLACK);
uu++; cout << "您击败了敌机,获取积分+1,目前的总积分为:" << uu << endl;
cout << "--------------------------------\n";
cout << "BOSS还需要:" << sum1 - uu << " 积分被你吸引!\n";
ethis[j].isdie = true; /*Beep(Music[yy].s, Music[yy].t * 5), yy++; uu++;*/
}
}
}
}
}
}
}
bool panduanplus(myplane* ethis, myplane* mythis)
{
for (int j = 0; j < ENEMY_NUM; j++)
{
if ((ethis[j].y + img_enemy[enemy->frame].getheight() >= mythis->y) && (ethis[j].y <= mythis->y + myplanes[0].getheight()))
{
if ((mythis->x >= ethis[j].x && mythis->x <= ethis[j].x + img_enemy[enemy[j].frame].getwidth()) || (mythis->x + myplanes[0].getwidth() >= ethis[j].x && mythis->x + myplanes[0].getwidth() <= ethis[j].x + img_enemy[enemy[j].frame].getwidth()) || ((ethis[j].x >= mythis->x) && (ethis[j].x + img_enemy[ethis[j].frame].getwidth() <= mythis->x + myplanes[0].getwidth())))
{
mythis->h--;
printf("由于飞机碰撞,您的生命值发生剧烈变化:%d \n", mythis->h);
ethis[j].isdie = true;
break;
}
}
}
if (mythis->h < 0)return true;
return false;
}
void panduanebullet(myplane* ethis, myplane* mthis)
{
for (int i = 0; i < ENEMY_NUM; i++)
{
int x = ethis[i].frame;
if (!ethis[i].isdie && ethis[i].frame != 2)
{
if (x == 0)
{
for (int j = 0; j < BULLET_NUM; j++)
{
if (!bullet0[j].isdie)
{
if (bullet0[j].x >= mthis->x && bullet0[j].x + bullets[0].getwidth() <= mthis->x + myplanes[0].getwidth())
{
if (bullet0[j].y + bullets[0].getheight() >= mthis->y && bullet0[j].y <= mthis->y + myplanes[0].getheight())
{
mthis->h -= 10; cout << "由于1号飞机子弹击中您,您的生命值变化:" << mthis->h << " \n";
bullet0[j].isdie = true;
/* break;*/
}
}
}
}
}
if (x == 1)
{
for (int j = 0; j < BULLET_NUM; j++)
{
if (!bullet1[j].isdie)
{
if (bullet1[j].x >= mthis->x && bullet1[j].x + bullets[0].getwidth() <= mthis->x + myplanes[0].getwidth())
{
if (bullet1[j].y + bullets[0].getheight() >= mthis->y && bullet1[j].y <= mthis->y + myplanes[0].getheight())
{
bullet1[j].isdie = true;
mthis->h -= 10; cout << "由于2号飞机子弹击中您,您的生命值变化:" << mthis->h << " \n";
break;
}
}
}
}
}
if (x == 3)
{
for (int j = 0; j < BULLET_NUM; j++)
{
if (!bullet3[j].isdie)
{
if (bullet3[j].x >= mthis->x && bullet3[j].x + bullets[0].getwidth() <= mthis->x + myplanes[0].getwidth())
{
if (bullet3[j].y + bullets[0].getheight() >= mthis->y && bullet3[j].y <= mthis->y + myplanes[0].getheight())
{
bullet3[j].isdie = true;
mthis->h -= 10; cout << "由于3号飞机子弹击中您,您的生命值变化:" << mthis->h << " \n";
break;
}
}
}
}
}
}
}
}
void createdadaodan(BULLET* dathis)
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (dathis[i].isdie)
{
dathis[i].x = rand() % getwidth();
dathis[i].y = -dadaodanp.getheight();
dathis[i].isdie = false;
break;
}
}
}
void panduandadaodan(myplane* mthis, BULLET* dathis)
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (!dathis->isdie)
{
if (dathis->y + dadaodanp.getheight() > mthis->y)
{
if ((dathis->x > mthis->x && dathis->x < mthis->x + myplanes[0].getwidth()) || ((dathis->x + dadaodanp.getwidth() > mthis->x) && (dathis->x + dadaodanp.getwidth() < mthis->x + myplanes[0].getwidth())))
{
mthis->h -= 15;
dathis[i].isdie = true;
cout << "--------------------------------\n";
cout << "您被恶魔导弹击中,生命值大量损失:" << mthis->h << endl;
}
}
}
}
}
//页面点击
struct resource
{
IMAGE bj[10];
}res;
void res_init(resource* res)
{
for (int i = 0; i < 5; i++)
{
char p[50] = { 0 };
sprintf_s(p, "飞机大战/开端%d.png", i + 1);
loadimage(res->bj + i, p, getwidth(), getheight());
}
}
bool isin(ExMessage* msg, int x, int y, int w, int h)
{
if (msg->x >= x && msg->x <= x + w && msg->y >= y && msg->y <= y + h)
{
return true;
}
return false;
}
int which = 0;
bool go = false;
void startupScene(ExMessage* msg)
{
if (msg->message == WM_LBUTTONDOWN)
{
if (which == 0)
{
which++;
}
else
if (which == 1)
{
if (isin(msg, 345, 192, 310, 83))//确定了
{
go = true;
}
if (isin(msg, 365, 340, 270, 65))//确定了
{
which = 3;//NO
}
if (isin(msg, 0, 520, 250, 300))//确定了
{
which = 2;//介绍
}
}
else
if (which == 2)
{
if (isin(msg, 400, 150, 232, 105))//确定
{
which = 1;
}
}
else
if (which == 3)//光遇OK
{
if (isin(msg, 340, 400, 280, 165))//确定了
{
which = 1;
}
if (isin(msg, 895, 555, 450, 100))//确定了
{
which = 4;
}
}
else
if (which == 4)
{
if (isin(msg, 340, 325, 280, 142))//确定了
{
which = 1;
}
if (isin(msg, 895, 555, 450, 100))//确定了
{
which = 3;
}
}
}
putimage(0, 0, res.bj + which);
}
//BOSS状态
void bossbupanduan(BULLET* mthis, myplane* bothis)
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (!mthis[i].isdie)
{
if (mthis[i].y <= bothis->y + img_enemy[4].getheight())
{
bothis->h--;
mthis->isdie = true;
cout << "子弹击中BOSS,BOSS血量减少至:" << bothis->h << endl;
}
}
}
}
int crt = 1;
void bossbulletpanduan(BULLET* ethis, myplane* mthis)
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (!ethis[i].isdie)
{
if (ethis[i].x >= mthis->x && ethis[i].x + bullets[0].getwidth() <= mthis->x + myplanes[0].getwidth())
{
if (ethis[i].y + bullets[0].getheight() >= mthis->y && ethis[i].y <= mthis->y + myplanes[0].getheight())
{
mthis->h -= crt++; cout << "您被BOSS的子弹击中,生命值减少至:" << mthis->h << endl;
ethis[i].isdie = true;
if (crt >= 12)crt = 3;
}
}
}
}
}
void creatbossbu(BULLET* ethis)
{
for (int i = 0; i < 7; i++)
{
if (bossgg[i].isdie)
{
bossgg[i].x = rand() % 945;
bossgg[i].y = img_enemy[4].getheight();
bossgg[i].isdie = false;
break;
}
}
}
bool BOSSpanduanplus(myplane* ethis, myplane* mythis)
{
for (int j = 0; j < ENEMY_NUM; j++)
{
if (mythis->y < 200)
{
mythis->h--;
ethis->h--;
printf("由于与BOSS碰撞,您的生命值发生剧烈变化:%d \n", mythis->h);
printf("由于与BOSS碰撞,BOSS生命值发生剧烈变化:%d \n", ethis->h);
break;
}
}
if (mythis->h < 0)return true;
return false;
}
int main()
{
//三个随机数
srand(time(0));
int v[5] = { 1500,2000,3000,1666,9999 };
sum = v[rand() % 5]; bool apper = false;
int gdf[5] = { 175,220,666,250,99 };
hhhh = gdf[rand() % 5];
int ooo[5] = { 25,55,95 };
uu = 0, sum1 = ooo[rand() % 3];
initgraph(944, 588, EX_SHOWCONSOLE);
init();
res_init(&res);
while (true)
{
ExMessage msg;
while (peekmessage(&msg, EX_MOUSE))
{
startupScene(&msg);
if (go)break;
}
if (go)break;
}
cout << "--------------------------------\n";
cout << "敌机即将到达战场!\n";
cout << "--------------------------------\n";
cout << "您的初始生命值为:" << hhhh << " 请您注意!" << endl;
cout << "--------------------------------\n";
cout << "您本次需要积分:" << sum1 << " 召唤BOSS\n";
cout << "--------------------------------\n";
Sleep(3000);
while (true)
{
int starttime = clock();
if (timer(600, 100))
{
createnemy(); createdadaodan(dadaodan);
}
createnemybullet(enemy);
draw();
moveenemy();
plane_move(&gamer);
movebullet();
panduan(bullet, enemy, &gamer);
panduanebullet(enemy, &gamer);
panduandadaodan(&gamer, dadaodan);
if (panduanplus(enemy, &gamer) || gamer.h < 0)
{
goto sd;
}
int frametime = clock() - starttime;
if (1000 / 70 - frametime > 0)//一帧应该执行的时间大于当前执行的时间(提前完成)
{
Sleep(1000 / 70 - frametime);
}
if (sum1 == uu)
{
apper = true;
boss.h = sum; boss.isdie = false;
for (int i = 0; i < BULLET_NUM; i++)
{
bullet[i].isdie = true;
}
}
if (apper)goto boss;
}
boss:
{
for (int i = 0; i < ENEMY_NUM; i++)enemy[i].isdie = true;
for (int i = 0; i < BULLET_NUM; i++)dadaodan[i].isdie = true;
cout << "--------------------------------\n";
cout << "BOSS还有五秒到达战场\n";
cout << "--------------------------------\n";
cout << "恭喜您成功召唤了BOSS,但是八成撑不下去,无所谓,您会出丑。" << endl;
cout << "--------------------------------\n";
cout << "但是我还是要告诉您,\nBOSS在您极端运气的加持下,它的生命值为:" << sum << endl;
cout << "--------------------------------\n";
cout << "加油喔!" << endl;
cout << "--------------------------------\n";
Sleep(6000);
while (true)
{
int starttime = clock();
draw();
putimage(0, 0, img_enemy + 4);
creatbossbu(bossgg);
plane_move(&gamer);
movebullet();
bossbupanduan(bullet, &boss);
bossbulletpanduan(bossgg, &gamer);
BOSSpanduanplus(&boss, &gamer);
if (timer(600, 100))
{
createdadaodan(dadaodan);
}
panduandadaodan(&gamer, dadaodan);
if (gamer.h <= 0)goto sd;
if (boss.h <= 0)goto win;
int frametime = clock() - starttime;
if (1000 / 70 - frametime > 0)//一帧应该执行的时间大于当前执行的时间(提前完成)
{
Sleep(1000 / 70 - frametime);
}
}
}
sd:
while (true)
{
putimage(0, 0, &failed);
for (int i = 0; i < 194; ++i)
{
Beep(Music[i].s, Music[i].t * 18);
if (i == 193)i = 0;
}
}
win:
{
while (true)
{
for (int i = 0; i < 194; ++i)
{
putimage(0, 0, &WIN);
Beep(Music[i].s, Music[i].t * 18);
if (i == 193)i = 0;
}
}
}
getchar();//按任意键退出
}
如果有想玩这个的还有一些源文件可以找我要哈哈哈。