赞
踩
不废话,直接上代码:
- #include<stdio.h>
- #include<easyx.h>
- #include<stdlib.h>
- #include<time.h>
- #include<math.h>
- /*
- 项目内容:球球大作战
- 开发环境:vs2019+easyx
- */
-
-
- #define SCREEN_TICK_PER_FRAME (1000/60.0) //每一帧需要的毫秒数
- #define FOODNUM 200 //食物数量
- #define AINUM 10 //人机数量
- #define distance(spr1,spr2) sqrt(((spr1)->x-(spr2)->x)*((spr1)->x-(spr2)->x)+((spr1)->y-(spr2)->y)*((spr1)->y-(spr2)->y))
-
- //创建球球结构体
- struct Sprite//精灵
- {
- double x;
- double y;
- double r;
- double speed;
- bool isdie=false;//是否godie了
- COLORREF color;//颜色
-
- int index;
- };
- //初始化精灵
- void spr_init(Sprite* spr, int x, int y, int r, int speed)
- {
- spr->x = x;
- spr->y = y;
- spr->r = r;
- spr->speed = speed;
- spr->color = RGB(rand()%256,rand()%256,rand()%256); //随机生成颜色
-
- spr->isdie = false;
- spr->index = -1; //人机食物坐标
-
- }
- void spr_draw(Sprite* spr)
- {
- if (!spr->isdie)
- {
- //设置填充颜色
- setfillcolor(spr->color);
- //绘制一个圆
- solidcircle(spr->x, spr->y, spr->r);
- }
- }
-
- void gamer_move(Sprite* spr)
- {
- //获取键盘按键
- if (GetAsyncKeyState(VK_UP)) //GetAsyncKeyState获取异步按键的状态(检测某个健是否按下)
- {
- spr->y -= spr->speed;
- }
- if (GetAsyncKeyState(VK_DOWN)) //GetAsyncKeyState获取异步按键的状态(检测某个健是否按下)
- {
- spr->y += spr->speed;
- }
- if (GetAsyncKeyState(VK_LEFT)) //GetAsyncKeyState获取异步按键的状态(检测某个健是否按下)
- {
- spr->x -= spr->speed;
- }
- if (GetAsyncKeyState(VK_RIGHT)) //GetAsyncKeyState获取异步按键的状态(检测某个健是否按下)
- {
- spr->x += spr->speed;
- }
- }
-
- void gamer_eatFood(Sprite* gamer, Sprite* foods)
- {
- //遍历食物数组
- for (int i = 0; i < FOODNUM; i++)
- {
- //圆心距小于玩家的半径
- if (!foods[i].isdie&&distance(gamer, foods+i) < gamer->r)
- {
- //玩家变大
- gamer->r += foods[i].r/8;
-
- //食物消失
- foods[i].isdie = true;
- }
- }
-
- }
-
- void ai_eatFood(Sprite* ais, Sprite* foods)
- {
- for (int i = 0; i < AINUM; i++)
- {
- if (ais[i].isdie)
- continue;
- for (int k = 0; k < FOODNUM; k++)
- {
- if (!foods[k].isdie && distance(ais + i, foods + k) < ais[i].r)
- {
- ais[i].r += foods[ais[i].index].r / 8;
- foods[k].isdie = true;
- }
- }
- }
- }
-
- //定义全局变量
- Sprite gamer;//玩家
- Sprite food[FOODNUM]; //食物的数组
- Sprite ai[AINUM];//人工智障 联机
-
- //追击方法
- void chase(Sprite* chase, Sprite* run)
- {
- if (chase->x > run->x)
- {
- chase->x--;
- }
- else
- {
- chase->x++;
- }
-
- }
-
- void ai_move(Sprite* ais,Sprite* foods)
- {
- int minDis = getwidth();
- //吃最近的食物
- int k;
- for (int i = 0; i < FOODNUM; i++) //食物
- {
- if (food[i].isdie)
- continue;
-
- for (k = 0; k < AINUM; k++)
- {
- if (ais[k].isdie)
- continue;
- //开始判断
- double dis = distance(foods + i, ais + k);
- if (dis < minDis)
- {
- minDis = dis;
- ais[k].index = k;
- }
- }
- }
-
- for (int i = 0; i < AINUM; i++)
- {
- if (!foods[ais[i].index].isdie)
- {
- //开始追击
- chase(ais + i, foods + ais[i].index);
- 能吃了,圆心距小于我的半径
- //if (distance(ais + i, foods + ais[i].index) < ais[i].r)
- //{
- // ais[i].r = foods[ais[i].index].r / 8;
- // foods[ais[i].index].isdie = true;
- //}
-
-
-
- }
- }
- }
- void draw()
- {
- spr_draw(&gamer);
- //绘制所有食物
- for (int i = 0; i < FOODNUM; i++)
- {
- spr_draw(food + i);
- }
- for (int i = 0; i < AINUM; i++)
- {
- spr_draw(ai + i);
- }
- }
-
- //初始化所有数据
- void init()
- {
- srand(time(NULL));
-
- spr_init(&gamer, 1024 / 2, 640 / 2, 10, 3);
-
- //初始化所有的食物
- for (int i = 0; i < FOODNUM; i++)
- {
- int r = rand() % 5 + 2;
- int x = rand() % (getwidth());
- int y = rand() % (getheight());
- spr_init(food + i, x, y, r, 0);
- }
- //初始化人机
- for (int i = 0; i < AINUM; i++)
- {
- int r = rand() % 10 + 10;
- int x = rand() % (getwidth()-r);
- int y = rand() % (getheight()-r);
- spr_init(ai + i, x, y, r,3);
- }
-
- }
-
-
- int main()
- {
- //创建窗口
- initgraph(1024, 640);
- //设置背景颜色
- setbkcolor(WHITE);
- cleardevice();
-
- init();
-
-
- while (true) {
- //获取一下时间
- int startTime = clock();
-
-
- //绘制太快,未完成的步骤都已经被看到,所以会闪屏
- BeginBatchDraw(); //开启双缓冲
- //在绘制前先清屏
- cleardevice();
- draw();
-
- gamer_move(&gamer);
- ai_move(ai,food);
- gamer_eatFood(&gamer, food);
- ai_eatFood(ai, food);
- EndBatchDraw();//结束双缓冲
-
- //控制游戏帧率
- int frameTime = clock() - startTime; //计算一帧用了多久
- //提前就执行完了当前帧了 frametime 10 16.6666
- if (frameTime < SCREEN_TICK_PER_FRAME)
- {
- Sleep(SCREEN_TICK_PER_FRAME - frameTime);
- }
-
-
- }
-
- getchar();//防止闪退
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。