#include<stdio.h>
#include<easyx.h> //easyx图形库
#include<math.h>
#include<time.h>
#define FoodNum 200
#define PeerNum 20
//求两点之间的距离
#define distance(b1,b2) \
sqrt((b1.x-b2.x)*(b1.x-b2.x)+(b1.y-b2.y)*(b1.y-b2.y))
void chase(struct Ball* chase, struct Ball* run);
struct Ball //球
{
int x;
int y;
float r;
bool isLive; //是否存在
COLORREF color; //unsigned long
};
//食物
struct Ball foods[FoodNum];
//玩家 是个球
struct Ball gamer = { 50,50,10 };
//敌方小球
struct Ball peers[PeerNum];
void init()
{
for (size_t i = 0; i < FoodNum; i++)
{
foods[i].x = rand() % getwidth();
foods[i].y = rand() % getheight();
foods[i].r = rand() % 5 + 1; //[1,5]
foods[i].isLive = true;
foods[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
for (size_t i = 0; i < PeerNum; i++)
{
peers[i].x = rand() % getwidth();
peers[i].y = rand() % getheight();
peers[i].r = rand() % 10 + 5;
peers[i].isLive = true;
peers[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
}
void draw()
{
//绘制所有食物
for (size_t i = 0; i < FoodNum; i++)
{
if (foods[i].isLive)
{
setfillcolor(foods[i].color);
solidcircle(foods[i].x, foods[i].y, foods[i].r);
}
}
//绘制对手
for (size_t i = 0; i < PeerNum; i++)
{
if (peers[i].isLive)
{
setfillcolor(peers[i].color);
solidcircle(peers[i].x, peers[i].y, peers[i].r);
}
}
//设置填充颜色
setfillcolor(GREEN);
//绘制玩家
solidcircle(gamer.x, gamer.y, gamer.r);
}
//按键操作
void keyEvent(int speed)
{
//移动玩家就是改变坐标 vk virtual key 虚拟键值
if (GetAsyncKeyState(VK_UP) && gamer.y - gamer.r >= 0) //获取异步按键状态
{
gamer.y-=speed;
}
if (GetAsyncKeyState(VK_DOWN) && gamer.y + gamer.r <= getheight())
{
gamer.y+=speed;
}
if (GetAsyncKeyState(VK_LEFT) && gamer.x - gamer.r >= 0)
{
gamer.x-=speed;
}
if (GetAsyncKeyState(VK_RIGHT) && gamer.x + gamer.r <= getwidth())
{
gamer.x+=speed;
}
}
//玩家吃食物
void eatFood()
{
for (size_t i = 0; i < FoodNum; i++)
{
if (!foods[i].isLive)
continue;
if (distance(foods[i], gamer) < gamer.r)
{
//食物消失
foods[i].isLive = false;
//玩家增大 自己随意发挥
gamer.r += foods[i].r / 4.0;
}
}
}
//让peer移动
void peerMove()
{
for (size_t i = 0; i < PeerNum; i++)
{
if (!peers[i].isLive)
continue;
//搜索半径
float 距离 = getheight();
//找比自己小的,朝它奔
for (size_t k = 0; k < PeerNum; k++)
{
/*//把自己排除掉,不能自己追自己
if (i != k &&
peers[i].r > peers[k].r)
{
chase(&peers[i], &peers[k]);
}*/
if (peers[k].isLive && peers[i].r > peers[k].r &&
distance(peers[k], peers[i]) < 距离)
{
距离 = distance(peers[k], peers[i]);
chase(&peers[i], &peers[k]);
}
}
}
}
//让peer互相吃,或者吃食物
void peerEat()
{
for (size_t i = 0; i < PeerNum; i++)
{
if (!peers[i].isLive)
continue;
//peereatpeer
for (size_t k = 0; k < PeerNum; k++)
{
if (peers[k].isLive && i != k &&
peers[i].r > peers[k].r &&
distance(peers[i], peers[k]) < peers[i].r)
{
peers[i].r += peers[k].r / 4;
peers[k].isLive = false;
}
}
//eatfood
for (size_t m = 0; m < FoodNum; m++)
{
if (foods[i].isLive && distance(peers[i], foods[m]) < peers[i].r)
{
//食物消失
foods[m].isLive = false;
//玩家增大 自己随意发挥
//peers[i].r += foods[m].r / 4.0;
}
}
}
}
//地图很大需要去移动
/*
其他功能:分裂
飞机大战,黄金矿工,迷宫小游戏。。。吃豆人
C语言 游戏,王者荣耀
easyx 给新手使用的,做大型一点的游戏比较难
*/
int main()
{
//创建一个窗口
initgraph(1024, 640);
//设置随机数种子
srand(time(NULL));
//设置窗口背景颜色
setbkcolor(WHITE);
cleardevice();
init();
//循环处理
//双缓冲绘图防止闪屏
BeginBatchDraw();
while (true)
{
cleardevice();
draw();
keyEvent(5);
eatFood();
peerMove();
peerEat();
Sleep(10);
FlushBatchDraw();
}
EndBatchDraw();
getchar();
return 0;
}
void chase(struct Ball* chase, struct Ball* run)
{
//x
if (chase->x < run->x)
{
chase->x++;
}
else
{
chase->x--;
}
//y
if (chase->y < run->y)
{
chase->y++;
}
else
{
chase->y--;
}
}
04-05
13万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)