判断子弹是否击中敌机一运行就卡住求助折磨2天了
#include<stdio.h>#include<time.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
//妆面宽高
#define width 500
#define high 760
#define sudu 15
#define ball_y 2
DWORD t1, t2, tt1, tt2,t3,tt3;
IMAGE beijing, beijing1, diji, diji1, zidan, zidan1, feiji, feiji1;
//飞机初始位置
struct Node
{
int x;
int y;
} play = {width/2,high-50};
//创建 炮弹和敌机的结构体
struct node
{
int x;
int y;
struct node *Pnext;
}*ball,*plane;
//子弹和敌机头指针
void BALL()
{
//子弹的头指针
ball = (node *)malloc(sizeof(node));
ball->Pnext = NULL;
//敌机头指针
plane = (node *)malloc(sizeof(node));
plane->Pnext = NULL;
}
//判断子弹是否击中敌机
void ball_xy()
{ //子弹临时遍历
node *zd = ball->Pnext;
node *zdfree = ball;
//敌机临时遍历
node *dj = plane->Pnext;
node *djfree = plane;
while (dj != NULL)///遍历敌机
{
zd = ball->Pnext;
zdfree = ball;
while (zd != NULL)//遍历子弹
{
if (zd->x >= dj->x - 10 && zd->x <= dj->x + 50 && zd->y >= dj->y - 15 &&
zd->y <= dj->y + 15)//判断子弹是否击中敌机
{
zdfree->Pnext = zd->Pnext; //链接子弹
free(zd);
djfree->Pnext = dj->Pnext; //链接敌机
free(dj);
dj = plane->Pnext; //重新指向敌机
djfree = plane;
break;
}
else
{
zd = zd->Pnext;
zdfree = zdfree->Pnext;
}
}
dj = plane->Pnext;
djfree = plane;
}
}
//增加子弹
void ZBall(int flag)
{
node *p = (node *)malloc(sizeof(node));
node *p1 = plane;
if (flag == 1) //造子弹
{
p->x = play.x + 18;
p->y = play.y - 4;
p->Pnext =ball->Pnext;
ball->Pnext = p;
}
else if (flag == 0)
{
p->x = rand() % width - 50;
p->y = 0;
p->Pnext = plane->Pnext;
plane->Pnext = p;
if (p->x >= width - 50||p->x<=50)
{
p1->Pnext = p->Pnext;
free(p);
}
}
}
//控制子弹和敌机移动速度
void ball_zidan()
{
//控制子弹移速
node *p = ball->Pnext;
while (p != NULL)
{
p->y -= ball_y;
p = p->Pnext;
}
//控制敌机移速
p = plane->Pnext;
static int Sepp = 0;
if (Sepp < 20)
{
Sepp++;
}
if (Sepp ==20)
{
while (p != NULL)
{
p->y += 1;
p = p->Pnext;
}
Sepp = 0;
}
}
//控制飞机移动
void Shot()
{
char key;
if (_kbhit())
{
key = _getch();
switch (key)
{
case 72:
if (play.y >= 2)
{
play.y -= sudu;
}
break;
case 80:
if (play.y <= high - 70)
{
play.y += sudu;
}
break;
case 75:
if (play.x >= 2)
{
play.x -= sudu;
}
break;
case77:
if (play.x <= width - 70)
{
play.x += sudu;
}
break;
}
}
}
//加 载 资 源
void Loadimag()
{
loadimage(&beijing, "beijing.jpg");
loadimage(&beijing1, "beijing1.jpg");
loadimage(&diji, "diji.png",60,60);
loadimage(&diji1, "diji1.png",60,60);
loadimage(&zidan, "zidan.png",15,30);
loadimage(&zidan1, "zidan1.png",15,30);
loadimage(&feiji, "feiji.png",50,50);
loadimage(&feiji1, "feiji1.png",50,50);
}
//显示资源
void Putimage()
{
BeginBatchDraw();
putimage(0, 0, &beijing);
putimage(play.x, play.y, &feiji,SRCAND);
putimage(play.x, play.y, &feiji1, SRCPAINT);
//输出子弹
node *p = ball->Pnext;
//遍历贴子弹图
while (p != NULL)
{
putimage(p->x, p->y, &zidan1, SRCAND);
putimage(p->x, p->y, &zidan, SRCPAINT);
p = p->Pnext;
}
p = plane->Pnext;
while (p != NULL)
{
putimage(p->x, p->y, &diji1, SRCAND);
putimage(p->x, p->y, &diji, SRCPAINT);
p = p->Pnext;
}
EndBatchDraw();
}
int main()
{
srand(unsigned int(time(NULL)));
initgraph(width,high);
//加载资源
Loadimag();
//显示背景1
putimage(0, 0, &beijing1);
//子弹和敌机头节点
BALL();
//获取系统时间
t1 = GetTickCount();
tt1 = GetTickCount();
while (1)
{
t2 = GetTickCount();
//显示资源
Putimage();
//控制飞机移动
Shot();
//造子弹
if (t2 - t1 >= 200)
{
ZBall(1);
t1 = t2;
}
tt2 = GetTickCount();
//造敌机
if (tt2 - tt1 >= 1500)
{
ZBall(0);
tt1 = tt2;
}
//子弹的射速
ball_zidan();
//子弹命中敌机
ball_xy();
}
getchar();
return 0;
} 检查了无数次 一运行void ball_xy() 就卡住 求助啊 大佬们!!!!!!!!!!!!!!!!!!
页:
[1]