[C++] 纯文本查看 复制代码 #include <graphics.h> //包含EGE的头文件
#include <time.h> //包含时间、随机数的头文件
#include <bits/stdc++.h>
using namespace std;
int mousePosX = -1, mousePosY = -1;//鼠标位置
mouse_msg msgRecord = { 0 };//鼠标结构体初始化
int n,count2=0,num=0;
int temp=0xffff;
float t=0;
int height=600,width=400;
float rnx(int min,int max) //随机数函数
{
float t;
t=rand()%(max-min)+min;
return t;
}
struct ballon
{
int r;
double g,y,x,vy,vx;
}ball[1000];
void csh() //初始化函数
{
initgraph(width, height); //初始化图形界面
setcolor(BLACK); //设置画图线为黑色
setbkcolor(YELLOW); //设置背景颜色为黄色
srand(time(0)); //随机种子函数
setfont(25,0,"宋体");//(字体高度,字体宽度(为0即为自适应),字形)设置字体为25,宋体
setbkmode(TRANSPARENT); //设置文字背景色为透明(默认为有背景色)
}
void new_ball()
{
num++;
ball[num].x = rnx(30,width-30);
ball[num].y = 5;
ball[num].vx = rnx(-2,2);
ball[num].vy = rnx(1,3);
ball[num].r = 5;
}
void update()
{
for(int i =0;i<num;i++)
{
ball[i].x += ball[i].vx;
ball[i].y += ball[i].vy;
if(ball[i].x<=ball[i].r || ball[i].x>=width-ball[i].r)ball[i].vx = -ball[i].vx;
if(ball[i].y<=ball[i].r || ball[i].y>=height-ball[i].r)ball[i].vy = -ball[i].vy;
}
while (mousemsg())
{
msgRecord = getmouse();//getmouse 获取鼠标消息
mousePosX = msgRecord.x;
mousePosY = msgRecord.y;
}
if(count2%2000==0) new_ball();
}
void draw_ball() //画球函数
{
cleardevice();
for(int i=0;i<num;i++)
{
setfillcolor(RED);
fillellipse(ball[i].x, ball[i].y, ball[i].r, ball[i].r);
}
setfillcolor(GREEN);
fillellipse(mousePosX, mousePosY, 7, 7);
xyprintf(20,20,"计时:%d",count2/1000);
count2=count2+10;
Sleep(10);
}
bool game_over()
{
for(int i=0;i<num;i++)
{
if((abs(mousePosX-ball[i].x)+abs(mousePosY-ball[i].y))<=(7+ball[i].r)) //距离够近
{
return true;
}
}
return false;
}
int main()
{
csh();
while(1)
{
update();
draw_ball();
if(game_over()) break;
}
xyprintf(10,200,"GAMEOVER");
getch();
return 0;
} |