easyx开发坦克大战(vs2013,easyx,链表,c++)
很久之前写的坦克大战,分享出来希望能和像我一样喜欢游戏的小伙伴们多一些灵感,敌人和子弹都是用圆和矩形拼接而成的
后来又做了一些改进,加入了死亡爆炸效果和背景音乐,但基本思路便是下列代码所述
爆炸效果图片和音乐文件不太好上传,这样也能运行,只是没有爆炸效果和背景音乐而已,其余没有任何变化
对了,u和i都有数量限制哦,还有自己生命值也有限制,但是有作弊键,想知道是哪些键就认真读代码吧
首先 :源.CPP代码如下
#include
#include
#include
#include
#include
#include
#include
#include"标头.h"
#define swidth 1800
#define sheight 1000
struct bullet //定义子弹类
{
int x;
int y;
int dx;
int dy;
int scope;//射程
int ltime;//持续时间
int flag;//子弹类型
int attack;
int num;
char name[100];
struct bullet *next;
};
struct tank//定义坦克类
{
int x;
int y;
int hwidth;
int hheight;
int dx;
int dy;
int flag;
int attack;
int health;
int defend;
char name[100];
struct tank * next;
};
struct wall //定义墙类,后来太麻烦了没用到
{
int x;
int y;
int hwidth;
int hheight;
int health;
};
struct tank me,boss; //定义我和老boss
struct tank *enemy,*henemy,*tenemy; //定义敌人,分别表示敌人,敌人链表的头指针,敌人链表的尾指针
struct bullet *head,*tail,*ehead,*etail; //我自己的子弹链表的头指针,尾指针,敌人子弹链表的头指针和尾指针
char ch;
int isimage, tx, ty;
int times = 1; //控制子弹发射速度
int battle; //定义关卡数
int flag;
int bflag; //判断是不是第一次进入某一关,是的话就弄出字幕,否则不显示字幕
int num1 = 30, num2 = 30;
void printme(); //制作我自己
void moveme(); //移动我自己
void gameinit(); //游戏初始化
void makemybullet(); //制作我自己的子弹(制作就是打印出来,并做一些相应调整)
void printmybullet(); //打印我自己的子弹
void clearmybullet(); //清理我自己的子弹,用背景色盖住子弹的前一个位置,造成子弹移动的视觉效果
void makeenemy(int a,int b); //制作敌人
void printenemy(); //制作敌人
void enemymove(); //敌人移动
void makebullet(struct tank* tan); //敌方坦克产生子弹
void printbullet(); //制作敌方坦克的子弹
void bulletmove(); //子弹移动
void hit(); //判断子弹是否撞到坦克
void deleteenemy(struct tank *tan);//从敌方坦克链表中删除一个节点
void makeboss(); //产生boss,设置基本属性
void printboss(); //打印boss
void bossmove(); //boss移动
void makebossbullet(); //boss产生甜甜圈,哈哈,吃一个试试
int main()
{
/*initgraph(640, 480);
setlinecolor(WHITE);
setfillcolor(RED);
BeginBatchDraw();
for (int i = 50; i<600; i++)
{
circle(i, 100, 40);
floodfill(i, 100, WHITE);//批量绘图
FlushBatchDraw();
Sleep(10);
cleardevice();
}
EndBatchDraw();*/
initgraph(1800, 1000);
//outtextxy(1530, 20, L"我的剩余血量");
gameinit(); //初始化
boss.health = 10;
int flag = 0;
BeginBatchDraw();
loadsound();
TCHAR c = L'0'; //一系列初始化工作
while (1)
{
/*setfillcolor(BLACK);
solidrectangle(0, 0, 1800, 1000);*/
if (tenemy == henemy&&!bflag&&battle<=4)
{
bflag = 1;
battle++;
}
if (battle == 1&&bflag)
{
cleardevice();
outtextxy(500, 20, L"第一波敌人即将来袭,请做好准备"); //第一关字幕设置
outtextxy(500, 50, L"按任意键继续");
_getch();
_getch();
_getch();
_getch();
_getch();
cleardevice();
outtextxy(450, 20, L"第");
outtextxy(465, 20, c+battle);
outtextxy(475, 20, L"波");
makeenemy(100, 100);
makeenemy(900, 100);
makeenemy(1700, 100);
makeenemy(100, 900);
makeenemy(1700, 900);
bflag = 0;
outtextxy(1530, 20, L"我的剩余血量");
}
else if (battle == 2 && bflag)//第二关字幕设置
{
Sleep(3000);
cleardevice();
outtextxy(500, 20, L"下一波敌人即将来袭,请做好准备");
outtextxy(500, 50, L"按任意键继续");
FlushBatchDraw();
_getch();
_getch();
_getch();
_getch();
_getch();
cleardevice();
outtextxy(450, 20, L"第");
outtextxy(465, 20, c + battle);
outtextxy(475, 20, L"波");
makeenemy(200, 200);
makeenemy(900, 200);
makeenemy(1700, 200);
makeenemy(200, 900);
makeenemy(1700, 900);
makeenemy(500, 900);
makeenemy(600, 400);
makeenemy(1500, 600);
makeenemy(1700, 200);
makeenemy(800,300);
bflag = 0;
outtextxy(1530, 20, L"我的剩余血量");
}
else if (battle == 3 && bflag) //第三关字幕设置
{
Sleep(3000);
cleardevice();
outtextxy(500, 20, L"下一波敌人即将来袭,请做好准备");
outtextxy(500,50, L"按任意键继续");
FlushBatchDraw();
_getch();
_getch();
_getch();
_getch();
_getch();
cleardevice();
outtextxy(450, 20, L"第");
outtextxy(465, 20, c + battle);
outtextxy(475, 20, L"波");
makeenemy(500, 900);
makeenemy(600, 400);
makeenemy(1500, 600);
makeenemy(1700, 200);
makeenemy(800, 300);
makeenemy(800, 400);
makeenemy(700, 200);
makeenemy(1300, 200);
makeenemy(200, 900);
makeenemy(1700, 400);
makeenemy(500, 400);
makeenemy(100, 200);
makeenemy(600, 200);
makeenemy(200, 400);
makeenemy(900, 400);
bflag = 0;
outtextxy(1530, 20, L"我的剩余血量");
}
else if (battle == 4 && bflag) //第四关字幕设置
{
Sleep(3000);
cleardevice();
outtextxy(500, 20, L"下一波敌人即将来袭,请做好准备");
outtextxy(500, 50, L"按任意键继续");
FlushBatchDraw();
_getch();
_getch();
_getch();
_getch();
_getch();
cleardevice();
outtextxy(450, 20, L"第");
outtextxy(465, 20, c + battle);
outtextxy(475, 20, L"波");
makeenemy(500, 900);
makeenemy(600, 400);
makeenemy(1500, 600);
makeenemy(1700, 200);
makeenemy(800, 300);
makeenemy(800, 400);
makeenemy(700, 200);
makeenemy(1300, 200);
makeenemy(200, 900);
makeenemy(1700, 400);
makeenemy(500, 400);
makeenemy(100, 200);
makeenemy(600, 200);
makeenemy(200, 400);
makeenemy(900, 400);
makeenemy(500, 400);
makeenemy(100, 200);
makeenemy(600, 200);
makeenemy(200, 400);
makeenemy(900, 400);
makeenemy(600, 700);
makeenemy(800, 900);
makeenemy(700, 200);
makeenemy(200, 400);
makeenemy(500, 400);
bflag = 0;
outtextxy(1530, 20, L"我的剩余血量");
}
else if (battle == 5 && bflag) //第五关字幕设置
{
Sleep(3000);
cleardevice();
outtextxy(500, 20, L"boss即将来袭,请做好准备");
outtextxy(500, 50, L"按任意键继续");
FlushBatchDraw();
_getch();
_getch();
_getch();
_getch();
_getch();
cleardevice();
outtextxy(450, 20, L"boss来袭");
makeboss();
printboss();
bflag = 0;
}
printmybullet();
printbullet();
printme();
printenemy();
if (isimage)
{
Sleep(100);
setfillcolor(BLACK);
solidrectangle(tx - 30 - 64, ty - 30 - 64, tx - 30 + 64, ty - 30 + 64);
isimage = 0;
}
if(battle==5) printboss(); //第五关是boss关
hit();
FlushBatchDraw(); //批量绘图显示
if (me.health <= 0)
{
MessageBox(GetHWnd(), L"游戏结束", L"你已死亡", NULL); //极其简单的游戏结束设置
exit(0);
}
else if (boss.health <= 0) //boss死亡则在boss身上贴上一系列爆炸图片做出相应的效果
{
FlushBatchDraw();
imageload(boss.x - 170, boss.y + 100);
Sleep(1000);
FlushBatchDraw();
imageload(boss.x - 100, boss.y - 30);
Sleep(1000);
FlushBatchDraw();
imageload(boss.x - 20, boss.y + 40);
Sleep(1000);
FlushBatchDraw();
imageload(boss.x + 80, boss.y - 100);
Sleep(1000);
FlushBatchDraw();
imageload(boss.x + 180, boss.y + 90);
Sleep(1000);
FlushBatchDraw();
imageload(boss.x + 130, boss.y + 90);
Sleep(1000);
FlushBatchDraw();
imageload(boss.x + 30, boss.y -60);
Sleep(1000);
FlushBatchDraw();
//vic();
//FlushBatchDraw();
Sleep(4000);
MessageBox(GetHWnd(), L"游戏胜利", L"boss已被消灭", NULL);
exit(0);
}
if (_kbhit()) //如果有按键就移动我自己
{
moveme();
}
Sleep(30); //控制游戏节奏
clearmybullet(); //清理之前的子弹轨迹
bulletmove(); //并且在新位置打印子弹,造成子弹移动的假象
times++;
if (times % 5 == 0) //控制敌人移动速度
{
enemymove();
if (battle == 5)bossmove();
}
}
_getch();
closegraph(); //程序结束时关闭窗口
}
void gameinit()
{
isimage = 0;
setfillcolor(LIGHTRED);
solidrectangle(100, 100, 1600, 200);
setfillcolor(LIGHTBLUE);
solidrectangle(100, 100, 200, 900);
setfillcolor(LIGHTGRAY);
solidrectangle(200, 800, 1600, 900);
setfillcolor(LIGHTGREEN);
solidrectangle(1600, 100, 1700, 900);
outtextxy(700, 400, L"欢迎来到豪哥版坦克大战,按任意键开始游戏!!!");
outtextxy(700, 500, L"按w a s d移动,j u i 发射子弹,激光对boss无效哦!! ");//游戏一开始的界面设置
_getch();
_getch();
cleardevice(); //按键输入后清屏设置一些数据并进入游戏
me.flag = 0;
me.dy = -1;
me.x = 900;
me.y = 900;
me.health = 500;
me.defend = 50;
me.hheight = 14;
me.hwidth = 10; //设置我的属性
head = (bullet*)malloc(sizeof(bullet));
head->flag = 2;
head->next = NULL; //设置子弹的头结点
tail = head; //一开始尾节点和头结点一样,直接赋值即可
battle = 1;
enemy = (struct tank*)malloc(sizeof(struct tank ));
henemy = enemy;
henemy->next = NULL;
tenemy = henemy;
henemy->flag = 2; //敌方坦克链表设置
ehead = (struct bullet *)malloc(sizeof(struct bullet));
ehead->next=NULL;
etail = ehead;
ehead->flag = 2;
bflag = 1;
cleardevice();
outtextxy(500, 20, L"第一波敌人即将来袭,请做好准备");
outtextxy(500, 50, L"按任意键继续");
_getch();
_getch();
_getch();
_getch();
_getch();
cleardevice();
outtextxy(450, 20, L"第");
outtextxy(465, 20, L"1");
outtextxy(475, 20, L"波");
makeenemy(100, 100);
makeenemy(900, 100);
makeenemy(1700, 100);
makeenemy(100, 900);
makeenemy(1700, 900);
bflag = 0; //注意第一关一旦第一次进入后,就将bflag清零,避免再次进入
outtextxy(1530, 20, L"我的剩余血量");
boss.x = -1000; //各种设置
}
void makemybullet()
{
bullet *t;
t = (bullet*)malloc(sizeof(bullet));
if (ch == 'j' || ch == 'J')
t->flag = 0;
else if (ch == 'u' || ch == 'U')
t->flag = 3;
else if (ch == 'i' || ch == 'I')
t->flag = 4;
else if (ch == 'o' || ch == 'O')
t->flag = 5;
else if (ch == 'y' || ch == 'Y')
t->flag = 6; //根据按键不同,产生不同类型的子弹,用一个flag代表产生子弹的类型
t->dx = me.dx;
t->dy = me.dy; //要让子弹移动方向和我在放子弹之前的朝向一样
t->attack = me.attack; //攻击力
if (t->flag == 0)
{
t->attack = 100;
if (t->dx == 1)
{
t->x = me.x + me.hheight + 4;
t->y = me.y;
}
else if (t->dx == -1)
{
t->x = me.x - me.hheight - 4;
t->y = me.y;
}
else if (t->dy == 1)
{
t->x = me.x;
t->y = me.y - me.hheight - 4;
}
else if (t->dy == -1)
{
t->x = me.x;
t->y = me.y + me.hheight + 4;
}
t->scope = 600; //子弹射程为600
}
else if (t->flag == 3)
{
t->attack = 100;
if (t->dx == 1)
{
t->x = me.x + me.hheight + 4;
t->y = me.y;
}
else if (t->dx == -1)
{
t->x = me.x - me.hheight - 4;
t->y = me.y;
}
else if (t->dy == 1)
{
t->x = me.x;
t->y = me.y - me.hheight - 4;
}
else if (t->dy == -1)
{
t->x = me.x;
t->y = me.y + me.hheight + 4;
}
t->scope = 600;
}
else if (t->flag == 4)
{
t->attack = 200;
if (t->dx == 1)
{
t->x = me.x + me.hheight + 4;
t->y = me.y;
}
else if (t->dx == -1)
{
t->x = me.x - me.hheight - 4;
t->y = me.y;
}
else if (t->dy == 1)
{
t->x = me.x;
t->y = me.y - me.hheight - 4;
}
else if (t->dy == -1)
{
t->x = me.x;
t->y = me.y + me.hheight + 4;
}
t->scope = 600;
} //根据子弹的方向不同,判断子弹最初位于我的什么位置
tail->next = t;
t->next = NULL;
tail = tail->next; //在子弹链表中新加一个节点
}
void clearmybullet()
{
struct bullet *hip,*plast; //hip指针用来遍历子弹链表,plast用来辅助删除节点
struct bullet *lll;
hip = head;
plast = head;
lll = head;
hip = hip->next;
while (hip != NULL)
{
if (hip->flag == 0||hip->flag==3)
{
if (hip->flag == 0)
{
setfillcolor(BLACK);
solidcircle(hip->x, hip->y, 3); //如果当前子弹的flag是3,那就画一个半径为3的圆并且用背景黑色盖掉原来的颜色
}
else
{
setfillcolor(BLACK);
solidcircle(hip->x, hip->y, 5);
}
if (hip->dx == 1)
{
hip->x += 5;
}
else if (hip->dx == -1)
{
hip->x -= 5;
}
else if (hip->dy == 1)
{
hip->y -= 5;
}
else if (hip->dy == -1)
{
hip->y += 5;
}
hip->scope -= 5;
if (hip->scope <= 0||hip->x-3<=0||hip->x+3>=swidth||hip->y-3<=0||hip->y+3>=sheight)
{
hip->scope = 0;
lll = plast->next;
}
hip = hip->next;
if (lll->scope == 0||lll==plast->next)
{
plast->next= hip;
if (lll==tail)tail = head;
free(lll);
continue;
}
plast = plast->next;
continue;
}
else if (hip->flag == 4)
{
hip->scope-=300;
if (hip->scope == 0)
{
if (hip->dx == 1)
{
setfillcolor(BLACK);
solidrectangle(hip->x, hip->y - 6, hip->x + 600, hip->y - 3);
setfillcolor(BLACK);
solidrectangle(hip->x, hip->y - 3, hip->x + 600, hip->y + 3);
setfillcolor(BLACK);
solidrectangle(hip->x, hip->y + 3, hip->x + 600, hip->y + 6);
}
else if (hip->dx == -1)
{
setfillcolor(BLACK);
solidrectangle(hip->x - 600, hip->y - 6, hip->x, hip->y - 3);
setfillcolor(BLACK);
solidrectangle(hip->x - 600, hip->y - 3, hip->x, hip->y + 3);
setfillcolor(BLACK);
solidrectangle(hip->x - 600, hip->y + 3, hip->x, hip->y + 6);
}
else if (hip->dy == 1)
{
setfillcolor(BLACK);
solidrectangle(hip->x - 6, hip->y - 600, hip->x - 3, hip->y);
setfillcolor(BLACK);
solidrectangle(hip->x - 3, hip->y - 600, hip->x + 3, hip->y);
setfillcolor(BLACK);
solidrectangle(hip->x + 3, hip->y - 600, hip->x + 6, hip->y);
}
else if (hip->dy == -1)
{
setfillcolor(BLACK);
solidrectangle(hip->x - 6, hip->y, hip->x - 3, hip->y + 600);
setfillcolor(BLACK);
solidrectangle(hip->x - 3, hip->y, hip->x + 3, hip->y + 600);
setfillcolor(BLACK);
solidrectangle(hip->x + 3, hip->y, hip->x + 6, hip->y + 600);
}
hip = hip->next;
lll = plast->next;
plast->next = hip;
if (lll == tail)tail = plast;
free(lll); //一系列操作后,删掉要删的节点
continue;
}
hip = hip->next;
plast = plast->next;
continue;
}
}
}
void printmybullet()
{
struct bullet *hip;
hip = head;
while (hip != NULL) //根据子弹类型不同画不同形状和颜色的子弹
{
if (hip->flag == 0)
{
setfillcolor(BLUE);
solidcircle(hip->x, hip->y, 3);
}
else if (hip->flag == 3)
{
setfillcolor(RED);
solidcircle(hip->x, hip->y, 5);
}
else if (hip->flag == 4)
{
//
if (hip->dx == 1)
{
setfillcolor(LIGHTRED);
solidrectangle(hip->x, hip->y - 6, hip->x + 600, hip->y-3);
setfillcolor(YELLOW);
solidrectangle(hip->x, hip->y - 3, hip->x + 600, hip->y + 3);
setfillcolor(LIGHTGREEN);
solidrectangle(hip->x, hip->y + 3, hip->x + 600, hip->y + 6);
}
else if (hip->dx == -1)
{
setfillcolor(LIGHTRED);
solidrectangle(hip->x-600, hip->y - 6, hip->x, hip->y - 3);
setfillcolor(YELLOW);
solidrectangle(hip->x-600, hip->y - 3, hip->x, hip->y + 3);
setfillcolor(LIGHTGREEN);
solidrectangle(hip->x-600, hip->y + 3, hip->x, hip->y + 6);
}
else if (hip->dy == 1)
{
setfillcolor(LIGHTRED);
solidrectangle(hip->x-6, hip->y - 600, hip->x-3, hip->y );
setfillcolor(YELLOW);
solidrectangle(hip->x -3, hip->y - 600, hip->x+3, hip->y );
setfillcolor(LIGHTGREEN);
solidrectangle(hip->x +3, hip->y -600, hip->x+6, hip->y );
}
else if (hip->dy == -1)
{
setfillcolor(LIGHTRED);
solidrectangle(hip->x - 6, hip->y , hip->x - 3, hip->y+600);
setfillcolor(YELLOW);
solidrectangle(hip->x - 3, hip->y , hip->x + 3, hip->y+600);
setfillcolor(LIGHTGREEN);
solidrectangle(hip->x + 3, hip->y , hip->x + 6, hip->y+600);
}
}
hip = hip->next;
}
}
void printme()
{
setfillcolor(WHITE);
solidrectangle(1000, 10, 1500, 50);
setfillcolor(RED);
//根据我自己的朝向不同,产生不同朝向的我自己dy==1就表示朝向正上方,-1表示正下方dx为1表示朝向正右方-1表示左方
solidrectangle(1000, 10, 1000 + me.health, 50); //显示我的血条,用一个矩形表示
if (me.dy == 1)
{
setfillcolor(GREEN);
solidrectangle(me.x - me.hwidth, me.y - me.hheight, me.x + me.hwidth, me.y + me.hheight);
setfillcolor(BLUE);
solidcircle(me.x, me.y, me.hwidth);
setcolor(RED);
line(me.x, me.y, me.x, me.y - me.hheight - 8);
setfillcolor(LIGHTRED);
solidrectangle(me.x - me.hwidth - 5, me.y - me.hheight - 5,me.x-me.hwidth,me.y+me.hheight+5);
setfillcolor(LIGHTRED);
solidrectangle(me.x + me.hwidth, me.y - me.hheight - 5, me.x + me.hwidth + 5, me.y + me.hheight + 5);
}
else if (me.dy == -1)
{
setfillcolor(GREEN);
solidrectangle(me.x - me.hwidth, me.y - me.hheight, me.x + me.hwidth, me.y + me.hheight);
setfillcolor(BLUE);
solidcircle(me.x, me.y, me.hwidth);
setcolor(RED);
line(me.x, me.y, me.x, me.y + me.hheight + 8);
setfillcolor(LIGHTRED);
solidrectangle(me.x - me.hwidth - 5, me.y - me.hheight - 5, me.x - me.hwidth, me.y + me.hheight + 5);
setfillcolor(LIGHTRED);
solidrectangle(me.x + me.hwidth, me.y - me.hheight - 5, me.x + me.hwidth + 5, me.y + me.hheight + 5);
}
else if (me.dx == 1)
{
setfillcolor(GREEN);
solidrectangle(me.x - me.hheight, me.y - me.hwidth, me.x + me.hheight, me.y + me.hwidth);
setfillcolor(BLUE);
solidcircle(me.x, me.y, me.hwidth);
setcolor(RED);
line(me.x, me.y, me.x + me.hheight + 8, me.y);
setfillcolor(LIGHTRED);
solidrectangle(me.x - me.hheight - 5, me.y - me.hwidth - 5, me.x + me.hheight + 5, me.y - me.hwidth);
setfillcolor(LIGHTRED);
solidrectangle(me.x - me.hheight - 5, me.y + me.hwidth, me.x + me.hheight + 5, me.y + me.hwidth + 5);
}
else if (me.dx == -1)
{
setfillcolor(GREEN);
solidrectangle(me.x - me.hheight, me.y - me.hwidth, me.x + me.hheight, me.y + me.hwidth);
setfillcolor(BLUE);
solidcircle(me.x, me.y, me.hwidth);
setcolor(RED);
line(me.x, me.y, me.x - me.hheight - 8, me.y);
setfillcolor(LIGHTRED);
solidrectangle(me.x - me.hheight - 5, me.y - me.hwidth - 5, me.x + me.hheight + 5, me.y - me.hwidth);
setfillcolor(LIGHTRED);
solidrectangle(me.x - me.hheight - 5, me.y + me.hwidth, me.x + me.hheight + 5, me.y + me.hwidth + 5);
}
}
void moveme()
{
if (me.dy == 1) //先根据我之前的朝向用背景色盖住原来的我,再根据新朝向画出现在的我
{
setfillcolor(BLACK);
solidrectangle(me.x - me.hwidth, me.y - me.hheight, me.x + me.hwidth, me.y + me.hheight);
setfillcolor(BLACK);
solidcircle(me.x, me.y, me.hwidth);
setcolor(BLACK);
line(me.x, me.y, me.x, me.y - me.hheight - 8);
setfillcolor(BLACK);
solidrectangle(me.x - me.hwidth - 5, me.y - me.hheight - 5, me.x - me.hwidth, me.y + me.hheight + 5);
setfillcolor(BLACK);
solidrectangle(me.x + me.hwidth, me.y - me.hheight - 5, me.x + me.hwidth + 5, me.y + me.hheight + 5);
}
else if (me.dy == -1)
{
setfillcolor(BLACK);
solidrectangle(me.x - me.hwidth, me.y - me.hheight, me.x + me.hwidth, me.y + me.hheight);
setfillcolor(BLACK);
solidcircle(me.x, me.y, me.hwidth);
setcolor(BLACK);
line(me.x, me.y, me.x, me.y + me.hheight + 8);
setfillcolor(BLACK);
solidrectangle(me.x - me.hwidth - 5, me.y - me.hheight - 5, me.x - me.hwidth, me.y + me.hheight + 5);
setfillcolor(BLACK);
solidrectangle(me.x + me.hwidth, me.y - me.hheight - 5, me.x + me.hwidth + 5, me.y + me.hheight + 5);
}
else if (me.dx == 1)
{
setfillcolor(BLACK);
solidrectangle(me.x - me.hheight, me.y - me.hwidth, me.x + me.hheight, me.y + me.hwidth);
setfillcolor(BLACK);
solidcircle(me.x, me.y, me.hwidth);
setcolor(BLACK);
line(me.x, me.y, me.x + me.hheight + 8, me.y);
setfillcolor(BLACK);
solidrectangle(me.x - me.hheight - 5, me.y - me.hwidth - 5, me.x + me.hheight + 5, me.y - me.hwidth);
setfillcolor(BLACK);
solidrectangle(me.x - me.hheight - 5, me.y + me.hwidth, me.x + me.hheight + 5, me.y + me.hwidth + 5);
}
else if (me.dx == -1)
{
setfillcolor(BLACK);
solidrectangle(me.x - me.hheight, me.y - me.hwidth, me.x + me.hheight, me.y + me.hwidth);
setfillcolor(BLACK);
solidcircle(me.x, me.y, me.hwidth);
setcolor(BLACK);
line(me.x, me.y, me.x - me.hheight - 8, me.y);
setfillcolor(BLACK);
solidrectangle(me.x - me.hheight - 5, me.y - me.hwidth - 5, me.x + me.hheight + 5, me.y - me.hwidth);
setfillcolor(BLACK);
solidrectangle(me.x - me.hheight - 5, me.y + me.hwidth, me.x + me.hheight + 5, me.y + me.hwidth + 5);
}
ch = _getch();
switch (ch)
{
case 'a':
case 'A':
me.dx = -1;
me.dy = 0;
if(me.x>=20)me.x -= 5;
break;
case 'D':
case 'd':
me.dx = 1;
me.dy = 0;
if(me.x<=1780)me.x += 5;
break;
case 'w':
case 'W':
me.dx = 0;
me.dy = 1;
if(me.y>=400)me.y -= 5;
break;
case 's':
case 'S':
me.dx = 0;
me.dy = -1;
if(me.y<=980)me.y += 5;
break;
case 'j':
case 'J':
makemybullet();
break;
case 'u':
case 'U':
if (num2)
{
makemybullet();
num2--;
}
break;
case 'i':
case'I':
if (num1)
{
makemybullet();
num1--;
}
break;
case'+':
me.health = 500;
break;
case'-':
num1 += 100;
num2 += 100;
break;
case 'h':
case 'H':
while (1)
{
if (_kbhit())
{
_getch();
break;
}
};
break;
}
}
void makeenemy(int a,int b) //产生敌人,x,y坐标上产生敌人
{
enemy = (struct tank *)malloc(sizeof(struct tank));
//enemy->x = 1600;
//enemy->y = 100;
srand((unsigned)time(NULL));
/*int a = rand ()% (800);
int b = rand ()% (1600);*/
enemy->x = a;
enemy->y = b;
enemy->flag = 5;
enemy->hwidth = 9;
enemy->hheight = 9;
enemy->dx = 0;
enemy->dy = -1;
enemy->defend = 70;
enemy->health = 300;
tenemy->next = enemy;
tenemy = enemy;
tenemy->next = NULL;
}
void printenemy()
{
struct tank *hip; //打印敌人,包括显示敌人形状和血量
hip = henemy;
while (hip != NULL)
{
if (hip!=henemy)
{
outtextxy(0, 60, L"当前敌人血量");
if (hip->health < 300)
{
setfillcolor(WHITE);
solidrectangle(40, 10, 340, 50);
setfillcolor(RED);
solidrectangle(40, 10, 40 + hip->health, 50);
}
}
if (hip->flag == 5)
{
if (hip->dy == -1)
{
setfillcolor(RED);
solidrectangle(hip->x - hip->hwidth, hip->y - hip->hheight, hip->x + hip->hwidth, hip->y + hip->hheight);
setfillcolor(WHITE);
solidcircle(hip->x, hip->y, 3);
setcolor(GREEN);
line(hip->x, hip->y, hip->x, hip->y + hip->hwidth + 7);
setfillcolor(LIGHTGRAY);
solidrectangle(hip->x - hip->hwidth - 4, hip->y - hip->hheight - 4, hip->x - hip->hwidth, hip->y + hip->hheight + 4);
setfillcolor(LIGHTGRAY);
solidrectangle(hip->x + hip->hwidth, hip->y - hip->hheight - 4, hip->x + hip->hwidth + 4, hip->y + hip->hheight + 4);
}
else if (hip->dy == 1)
{
setfillcolor(RED);
solidrectangle(hip->x - hip->hwidth, hip->y - hip->hheight, hip->x + hip->hwidth, hip->y + hip->hheight);
setfillcolor(WHITE);
solidcircle(hip->x, hip->y, 3);
setcolor(GREEN);
line(hip->x, hip->y - hip->hwidth - 7, hip->x, hip->y);
setfillcolor(LIGHTGRAY);
solidrectangle(hip->x - hip->hwidth - 4, hip->y - hip->hheight - 4, hip->x - hip->hwidth, hip->y + hip->hheight + 4);
setfillcolor(LIGHTGRAY);
solidrectangle(hip->x + hip->hwidth, hip->y - hip->hheight - 4, hip->x + hip->hwidth + 4, hip->y + hip->hheight + 4);
}
else if (hip->dx == 1)
{
setfillcolor(RED);
solidrectangle(hip->x - hip->hwidth, hip->y - hip->hheight, hip->x + hip->hwidth, hip->y + hip->hheight);
setfillcolor(WHITE);
solidcircle(hip->x, hip->y, 3);
setcolor(GREEN);
line(hip->x, hip->y, hip->x + hip->hwidth + 7,hip->y);
setfillcolor(LIGHTGRAY);
solidrectangle(hip->x - hip->hheight - 4, hip->y - hip->hwidth - 4, hip->x + hip->hheight + 4, hip->y - hip->hwidth);
setfillcolor(LIGHTGRAY);
solidrectangle(hip->x - hip->hwidth - 4, hip->y + hip->hheight, hip->x + hip->hwidth + 4, hip->y + hip->hheight + 4);
}
else if (hip->dx == -1)
{
setfillcolor(RED);
solidrectangle(hip->x - hip->hwidth, hip->y - hip->hheight, hip->x + hip->hwidth, hip->y + hip->hheight);
setfillcolor(WHITE);
solidcircle(hip->x, hip->y, 3);
setcolor(GREEN);
line(hip->x-hip->hwidth-7, hip->y, hip->x , hip->y);
setfillcolor(LIGHTGRAY);
solidrectangle(hip->x - hip->hheight - 4, hip->y - hip->hwidth - 4, hip->x + hip->hheight + 4, hip->y - hip->hwidth);
setfillcolor(LIGHTGRAY);
solidrectangle(hip->x - hip->hwidth - 4, hip->y + hip->hheight, hip->x + hip->hwidth + 4, hip->y + hip->hheight + 4);
}
}
hip = hip->next;
}
}
void makebullet(struct tank* tan)
{ //将子弹设置一些属性并加入链表
struct bullet *ebullet = (struct bullet *)malloc(sizeof(struct bullet));
ebullet->attack = 55;
ebullet->dx = tan->dx;
ebullet->dy = tan->dy;
if (ebullet->dx == 1)
{
ebullet->x = tan->x + 6;
ebullet->y = tan->y;
}
else if (ebullet->dx == -1)
{
ebullet->x = tan->x - 6;
ebullet->y = tan->y;
}
else if (ebullet->dy == 1)
{
ebullet->x = tan->x;
ebullet->y = tan->y - 6;
}
else if (ebullet->dy == -1)
{
ebullet->x = tan->x;
ebullet->y = tan->y + 6;
}
ebullet->flag = 6;
ebullet->scope = 600;
etail->next = ebullet;
etail = etail->next;
etail->next = NULL;
}
void enemymove()
{ //跟我移动在画面上是同理的,移动方式是根据一定的规则自动移动,不懂啥ai算法,下面解释规则
struct tank *hip = henemy;
int order = 0;
while (hip != NULL)
{
if ((hip->x>=me.x-30&&hip->x<=me.x+30)||(hip->y>=me.y-30&&hip->y<=me.y+30))
if(times%50==0)makebullet( hip);
if (hip->flag == 5)
{
if (hip->dy == -1)
{
setfillcolor(BLACK);
solidrectangle(hip->x - hip->hwidth, hip->y - hip->hheight, hip->x + hip->hwidth, hip->y + hip->hheight);
setfillcolor(BLACK);
solidcircle(hip->x, hip->y, 3);
setcolor(BLACK);
line(hip->x, hip->y, hip->x, hip->y + hip->hwidth + 7);
setfillcolor(BLACK);
solidrectangle(hip->x - hip->hwidth - 4, hip->y - hip->hheight - 4, hip->x - hip->hwidth, hip->y + hip->hheight + 4);
setfillcolor(BLACK);
solidrectangle(hip->x + hip->hwidth, hip->y - hip->hheight - 4, hip->x + hip->hwidth + 4, hip->y + hip->hheight + 4);
}
else if (hip->dy == 1)
{
setfillcolor(BLACK);
solidrectangle(hip->x - hip->hwidth, hip->y - hip->hheight, hip->x + hip->hwidth, hip->y + hip->hheight);
setfillcolor(BLACK);
solidcircle(hip->x, hip->y, 3);
setcolor(BLACK);
line(hip->x, hip->y - hip->hwidth - 7, hip->x, hip->y);
setfillcolor(BLACK);
solidrectangle(hip->x - hip->hwidth - 4, hip->y - hip->hheight - 4, hip->x - hip->hwidth, hip->y + hip->hheight + 4);
setfillcolor(BLACK);
solidrectangle(hip->x + hip->hwidth, hip->y - hip->hheight - 4, hip->x + hip->hwidth + 4, hip->y + hip->hheight + 4);
}
else if (hip->dx == 1)
{
setfillcolor(BLACK);
solidrectangle(hip->x - hip->hwidth, hip->y - hip->hheight, hip->x + hip->hwidth, hip->y + hip->hheight);
setfillcolor(BLACK);
solidcircle(hip->x, hip->y, 3);
setcolor(BLACK);
line(hip->x, hip->y, hip->x + hip->hwidth + 7, hip->y);
setfillcolor(BLACK);
solidrectangle(hip->x - hip->hheight - 4, hip->y - hip->hwidth - 4, hip->x + hip->hheight + 4, hip->y - hip->hwidth);
setfillcolor(BLACK);
solidrectangle(hip->x - hip->hwidth - 4, hip->y + hip->hheight, hip->x + hip->hwidth + 4, hip->y + hip->hheight + 4);
}
else if (hip->dx == -1)
{
setfillcolor(BLACK);
solidrectangle(hip->x - hip->hwidth, hip->y - hip->hheight, hip->x + hip->hwidth, hip->y + hip->hheight);
setfillcolor(BLACK);
solidcircle(hip->x, hip->y, 3);
setcolor(BLACK);
line(hip->x - hip->hwidth - 7, hip->y, hip->x, hip->y);
setfillcolor(BLACK);
solidrectangle(hip->x - hip->hheight - 4, hip->y - hip->hwidth - 4, hip->x + hip->hheight + 4, hip->y - hip->hwidth);
setfillcolor(BLACK);
solidrectangle(hip->x - hip->hwidth - 4, hip->y + hip->hheight, hip->x + hip->hwidth + 4, hip->y + hip->hheight + 4);
}
}
//就看敌人在x方向和y方向哪个方向离我远,从离我远的方向上先移动
if (fabs((double)(hip->x - me.x)) > fabs((double)(hip->y - me.y)))
{
if (hip->x>me.x)
{
if(hip->x-4>=10)hip->x -= 5;
hip->dx = -1;
hip->dy = 0;
}
else
{
if(hip->x+4<=1780)hip->x += 5;
hip->dx = 1;
hip->dy = 0;
}
}
else
{
if (hip->y>me.y)
{
if(hip->y-4>=10)hip->y -= 5;
hip->dy = 1;
hip->dx = 0;
}
else
{
if(hip->y+4<=980)hip->y += 5;
hip->dy = -1;
hip->dx = 0;
}
}
hip = hip->next;
}
}
void printbullet()
{ //画子弹
struct bullet *hip = ehead;
while (hip != NULL)
{
if (hip->flag == 6)
{
setfillcolor(WHITE);
solidcircle(hip->x, hip->y, 3);
}
else if (hip->flag == 8)
{
setfillcolor(LIGHTCYAN);
solidcircle(hip->x, hip->y, 40);
setfillcolor(BLACK);
solidcircle(hip->x, hip->y, 30);
}
hip = hip->next;
}
}
void bulletmove()
{ //子弹移动
struct bullet *hip = ehead;
hip = hip->next;
struct bullet *plast = ehead;
struct bullet *lll;
lll = ehead;
while (hip != NULL)
{
if (hip->flag == 6)
{
setfillcolor(BLACK);
solidcircle(hip->x, hip->y, 3);
if (hip->dx == 1)
{
hip->x += 5;
}
else if (hip->dx == -1)
{
hip->x -= 5;
}
else if (hip->dy == 1)
{
hip->y -= 5;
}
else if (hip->dy == -1)
{
hip->y += 5;
}
hip->scope -= 5;
if (hip->scope <= 0 || hip->x - 3 <= 0 || hip->x + 3 >= swidth || hip->y - 3 <= 0 || hip->y + 3 >= sheight)
{
hip->scope = 0;
lll = plast->next;
}
hip = hip->next;
if (lll->scope == 0 || lll == plast->next)
{
plast->next = hip;
if (lll == etail)
{
etail = plast;
}
free(lll);
continue;
}
plast = plast->next;
}
else if (hip->flag == 8)
{
setfillcolor(BLACK);
solidcircle(hip->x, hip->y, 40);
hip->y += 5;
hip->scope -= 5;
if (hip->scope <= 0 || hip->x - 10 <= 0 || hip->x + 10 >= swidth || hip->y - 10 <= 0 || hip->y + 10 >= sheight)
{
hip->scope = 0;
lll = plast->next;
}
hip = hip->next;
if (lll->scope == 0 || lll == plast->next)
{
plast->next = hip;
if (lll == etail)
{
etail = plast;
}
free(lll);
continue;
}
plast = plast->next;
}
}
}
void hit()
{ //根据子弹类型不同判断是和我相撞时减血还是和敌人相撞时减血 flag表示子弹类型,将敌人子弹和我自己子弹放在一个链//表中
struct bullet *hip1 = ehead;
while (hip1 != NULL)
{
if (hip1->flag == 6)
{
if (hip1->x + 3 >= me.x - 22 && hip1->x - 3 <= me.x + 22 && hip1->y + 3 >= me.y - 22 && hip1->y - 3 <= me.y + 22)
{
hip1->scope = 0;
me.health = me.health - (hip1->attack - me.defend);
}
}
else if (hip1->flag == 8)
{
if (hip1->x + 40 >= me.x - 22 && hip1->x - 40 <= me.x + 22 && hip1->y + 40 >= me.y - 22 && hip1->y - 40 <= me.y + 22)
{
hip1->scope = 0;
me.health = me.health - (hip1->attack - me.defend);
}
}
hip1 = hip1->next;
}
struct bullet *hip2 = head;
struct tank * ta;
ta = henemy;
hip2 = hip2->next;
ta = ta->next;
flag = 0;
while (hip2 != NULL)
{
ta = henemy;
ta = ta->next;
while (ta != NULL)
{
if (hip2->flag == 0&&hip2->scope>0)
{
if (hip2->x + 3 >= ta->x - 16 && hip2->x - 3 <= ta->x + 16 && hip2->y + 3 >= ta->y - 16 && hip2->y - 3 <= ta->y + 16 && hip2->scope > 0)
{
if (ta->health > 0)
{
flag = 1;
hip2->scope = 0;
ta->health = ta->health - (hip2->attack - ta->defend);
setfillcolor(WHITE);
solidrectangle(40, 10, 340, 50);
setfillcolor(RED);
solidrectangle(40, 10, 40 + ta->health, 50);
break;
}
else if (ta->health <= 0)
{
flag = 1;
ta->health = 0;
setfillcolor(WHITE);
solidrectangle(40, 10, 340, 50);
setfillcolor(RED);
solidrectangle(40, 10, 40 +ta->health, 50);
setfillcolor(BLACK);
solidrectangle(ta->x - 16, ta->y - 16, ta->x + 16, ta->y + 16);
deleteenemy(ta);
break;
}
}
}
else if (hip2->flag == 3&&hip2->scope>0)
{
if (hip2->x + 5 >= ta->x - 16 && hip2->x - 5 <= ta->x + 16 && hip2->y + 5 >= ta->y - 16 && hip2->y - 5 <= ta->y + 16 && hip2->scope > 0&&hip2->scope%10==0)
{
if (ta->health > 0)
{
flag = 1;
ta->health = ta->health - (hip2->attack - ta->defend);
setfillcolor(WHITE);
solidrectangle(40, 10, 340, 50);
setfillcolor(RED);
solidrectangle(40, 10, 40 + ta->health, 50);
}
else if (ta->health <= 0)
{
flag = 1;
ta->health = 0;
setfillcolor(WHITE);
solidrectangle(40, 10, 340, 50);
setfillcolor(RED);
solidrectangle(40, 10, 40 + ta->health, 50);
setfillcolor(BLACK);
solidrectangle(ta->x - 16, ta->y - 16, ta->x + 16, ta->y + 16);
deleteenemy(ta);
break;
}
}
}
else if (hip2->flag == 4&&ta->health>0)
{
if (hip2->dy == 1)
{
if (ta->y+16>=hip2->y-600&&ta->y-16<=hip2->y&&ta->x+16>=hip2->x-6&&ta->x-16<=hip2->x+6)
{
ta->health = ta->health - (hip2->attack - ta->defend);
flag = 1;
}
}
else if (hip2->dy == -1)
{
if (ta->x + 16 >= hip2->x - 6 && ta->x - 16 <= hip2->x + 6 && ta->y - 16 <= hip2->y + 600 && ta->y + 16 >= hip2->y)
{
ta->health = ta->health - (hip2->attack - ta->defend);
flag = 1;
}
}
else if (hip2->dx == 1)
{
if (ta->y + 16 >= hip2->y - 6 && ta->y - 16 <= hip2->y + 6 && ta->x + 16 >= hip2->x&&ta->x - 16 <= hip2->x + 600)
{
ta->health = ta->health - (hip2->attack - ta->defend);
flag = 1;
}
}
else if (hip2->dx == -1)
{
if (ta->y + 16 >= hip2->y - 6 && ta->y - 16 <= hip2->y + 6 && ta->x + 16 >= hip2->x - 600 && ta->x - 16 <= hip2->x)
{
ta->health = ta->health - (hip2->attack - ta->defend);
flag = 1;
}
}
if (ta->health <= 0)
{
ta->health = 0;
}
setfillcolor(WHITE);
solidrectangle(40, 10, 340, 50);
setfillcolor(RED);
solidrectangle(40, 10, 40 + ta->health, 50);
}
else
{
setfillcolor(BLACK);
solidrectangle(ta->x - 16, ta->y - 16, ta->x + 16, ta->y + 16);
deleteenemy(ta); //敌人死亡时要删除敌人
break;
}
ta = ta->next;
}
if (hip2->flag == 0)
{
if (hip2->y - 3 <= boss.y + 160&&hip2->x+3>=boss.x-200&&hip2->x-3<=boss.x+200)
{
boss.health -= 5;
hip2->scope = 0;
flag = 1;
}
}
else if (hip2->flag == 3)
{
if (hip2->y - 5 <= boss.y + 160 && hip2->x + 5 >= boss.x - 200 && hip2->x - 5 <= boss.x + 200&&hip2->scope%150==0)
{
boss.health -= 10;
flag = 1;
}
}
if (flag)
break;
hip2 = hip2->next;
}
}
void deleteenemy(struct tank *tan)
{
struct tank *hip;
hip = henemy;
imageload(tan->x, tan->y);
//loadboomsound();
isimage = 1;
tx = tan->x;
ty = tan->y;
while (hip != NULL)
{
if (hip->next == tan)
{
struct tank *t;
t = tan;
if (t == tenemy)
{
tenemy = hip;
}
hip->next = tan->next;
free(t);
return;
}
hip = hip->next;
}
}
void makeboss()
{
boss.x = 900;
boss.y = 300;
boss.defend = 99;
boss.health = 3000;
boss.dx = 1;
boss.dy = 0;
boss.flag = 7;
}
void printboss()
{
if (boss.health - 1500 >= 0)
{
setfillcolor(BLUE);
solidrectangle(100, 60, 1600, 100);
setfillcolor(RED);
solidrectangle(100, 60, 100 + boss.health - 1500, 100);
}
else
{
setfillcolor(WHITE);
solidrectangle(100, 60, 1600, 100);
setfillcolor(BLUE);
solidrectangle(100, 60, 100+boss.health, 100);
}
setfillcolor(LIGHTBLUE);
solidellipse(boss.x - 200, boss.y - 160, boss.x + 200, boss.y + 160);
setfillcolor(BLACK);
solidcircle(boss.x, boss.y, 160);
setfillcolor(GREEN);
solidcircle(boss.x, boss.y, 80);
setfillcolor(BLACK);
solidcircle(boss.x, boss.y, 50);
setfillcolor(RED);
solidcircle(boss.x, boss.y, 30);
}
void bossmove()
{
if (boss.x >= me.x - 60 && boss.x <= me.x + 60&×%20==0)
{
makebossbullet();
}
setfillcolor(BLACK);
solidellipse(boss.x - 200, boss.y - 160, boss.x + 200, boss.y + 160);
setfillcolor(BLACK);
solidcircle(boss.x, boss.y, 160);
setfillcolor(BLACK);
solidcircle(boss.x, boss.y, 80);
setfillcolor(BLACK);
solidcircle(boss.x, boss.y, 50);
setfillcolor(BLACK);
solidcircle(boss.x, boss.y, 30);
if (boss.x > me.x+50)
{
boss.x -= 10;
}
else if (boss.x
boss.x += 10;
}
else
{
int a;
srand((unsigned)time(NULL));
a = rand() % (30);
if (a & 1)
boss.x += 10;
else
boss.x -= 10;
}
}
void makebossbullet()
{
struct bullet *t;
t = (struct bullet*)malloc(sizeof(struct bullet));
t->flag = 8;
t->attack = 160;
t->dx = 0;
t->dy = -1;
t->x = boss.x;
t->y = boss.y + 150;
t->scope = 1200;
etail->next = t;
etail = etail->next;
t->next = NULL;
}
标头.h代码如下: //在之中设置一些与背景音乐还有图片有关的属性
#include
#pragma comment(lib,"winmm.lib")
void loadsound()
{
mciSendString(L"open ./res/坦克大战.mp3 alias bgm",0,0,0);
mciSendString(L"play bgm repeat", 0, 0, 0);
}
void imageload(int x,int y)
{
IMAGE img;
loadimage(&img, L"./res/explode2.bmp");
putimage(x-30, y-30, &img);
}
void loadboomsound()
{
mciSendString(L"open ./res/boom.mp3 alias bm", 0, 0, 0);
mciSendString(L"play bm wait", 0, 0, 0);
}
void vic()
{
IMAGE img;
loadimage(&img, L"./res/vic.bmp");
putimage(900,500, &img);
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
