小遊戲:球球大作戰

2020-10-11 11:01:07

#include<stdio.h>
#include<graphics.h>
#include<Windows.h>
#include<stdlib.h>
#include<time.h>
#include <math.h>

#define WIN_WIDTH 1024 //介面寬度
#define WIN_HEIGHT 640 //介面高度
#define MAP_WIDTH WIN_WIDTH3 //地圖寬度
#define MAP_HEIGHT WIN_HEIGHT
3 //地圖高度
#define FOOD_NUM 500
#define AI_NUM 200
#define SPEND 2

struct post
{
int x;
int y;
};
struct post camera;
struct ball
{
int x; //x軸座標
int y; //y軸座標
int r; //半徑
int flag;//是否存活(1/0)
DWORD color;//顏色,DWORD(unsigned long型)
};
struct ball player ;
struct ball food[FOOD_NUM];
struct ball AI[AI_NUM ];
IMAGE map(MAP_WIDTH,MAP_HEIGHT);
void GameInit(){
srand( time(NULL));
//初始化玩家
player.x = rand()%MAP_WIDTH;
player.y = rand()%MAP_HEIGHT;
player.r = rand()%10+5;
player.flag=1;
player.color=RGB(rand()%256,rand()%256,rand()%256); //返回一個長整型數
//初始化食物
for(int i = 0;i<FOOD_NUM;i++)
{
food[i].x = rand()%MAP_WIDTH;
food[i].y = rand()%MAP_HEIGHT;
food[i].r = rand()%5+1;
food[i].flag=1;
food[i].color=RGB(rand()%256,rand()%256,rand()%256);
//防止超出邊界異常
while(food[i].x+food[i].r>MAP_WIDTH&&food[i].x-food[i].r<0&&food[i].y+food[i].r>MAP_HEIGHT&&food[i].y-food[i].r<0)
{
food[i].x= rand()%MAP_WIDTH;
food[i].r = rand()%5+1;
food[i].y = rand()%MAP_HEIGHT;
}
}
//初始化AI
for(int i = 0;i<AI_NUM;i++)
{
AI[i].x = rand()%MAP_WIDTH;
AI[i].y = rand()%MAP_HEIGHT;
AI[i].r = rand()%10+10;
AI[i].flag=1;
AI[i].color=RGB(rand()%256,rand()%256,rand()%256);
//防止超出邊界異常
while( AI[i].x+ AI[i].r>MAP_WIDTH&& AI[i].x- AI[i].r<0&& AI[i].y+ AI[i].r>MAP_HEIGHT&& AI[i].y- AI[i].r<0)
{
AI[i].x= rand()%MAP_WIDTH;
AI[i].r = rand()%5+1;
AI[i].y = rand()%MAP_HEIGHT;
}
}
}
void getcamera()
{
camera.x=player.x-WIN_WIDTH/2;
camera.y=player.y-WIN_HEIGHT/2;
if(camera.x<0)
{
camera.x=0;
}
if(camera.y<0)
{
camera.y=0;
}
if(camera.x>MAP_WIDTH-WIN_WIDTH)
{
camera.x=MAP_WIDTH-WIN_WIDTH;
}
if(camera.y>MAP_HEIGHT-WIN_HEIGHT)
{
camera.y=MAP_HEIGHT-WIN_HEIGHT;
}
}
void GameDraw()
{
SetWorkingImage(&map);
setbkcolor(WHITE);
cleardevice();
//畫食物
for(int i = 0;i<FOOD_NUM;i++)
{
if(food[i].flag1)
{
setfillcolor(food[i].color);
solidcircle(food[i].x,food[i].y,food[i].r);
}
else
{
food[i].x = rand()%MAP_WIDTH;
food[i].y = rand()%MAP_HEIGHT;
food[i].r = rand()%5+1;
food[i].flag=1;
food[i].color=RGB(rand()%256,rand()%256,rand()%256);
//防止超出邊界異常
while(food[i].x+food[i].r>MAP_WIDTH&&food[i].x-food[i].r<0&&food[i].y+food[i].r>MAP_HEIGHT&&food[i].y-food[i].r<0)
{
food[i].x= rand()%MAP_WIDTH;
food[i].r = rand()%5+1;
food[i].y = rand()%MAP_HEIGHT;
}
}
}
//畫電腦
for(int i = 0;i<AI_NUM;i++)
{
if(AI[i].flag
1)
{
setfillcolor(AI[i].color);
solidcircle(AI[i].x,AI[i].y,AI[i].r);
}
else
{
AI[i].x = rand()%MAP_WIDTH;
AI[i].y = rand()%MAP_HEIGHT;
AI[i].r = rand()%10+10;
AI[i].flag=1;
AI[i].color=RGB(rand()%256,rand()%256,rand()%256);
//防止超出邊界異常
while( AI[i].x+ AI[i].r>MAP_WIDTH&& AI[i].x- AI[i].r<0&& AI[i].y+ AI[i].r>MAP_HEIGHT&& AI[i].y- AI[i].r<0)
{
AI[i].x= rand()%MAP_WIDTH;
AI[i].r = rand()%5+1;
AI[i].y = rand()%MAP_HEIGHT;
}
}
}
//畫玩家
setfillcolor(player.color);
solidcircle(player.x,player.y,player.r);
//顯示玩家名字
setbkmode(TRANSPARENT);
outtextxy(player.x,player.y,「李哥」);
SetWorkingImage();
getcamera();
putimage(0,0,WIN_WIDTH,WIN_HEIGHT,&map,camera.x,camera.y);
}
void move(int a)
{
if((GetAsyncKeyState(VK_UP)||GetAsyncKeyState(‘w’))&&player.y-player.r>0)
{
player.y=player.y-a;
}
if((GetAsyncKeyState(VK_DOWN)||GetAsyncKeyState(‘s’))&&player.y+player.r<MAP_HEIGHT)
{
player.y=player.y+a;
}
if((GetAsyncKeyState(VK_LEFT)||GetAsyncKeyState(‘a’))&&player.x-player.r>0)
{
player.x=player.x-a;
}
if((GetAsyncKeyState(VK_RIGHT)||GetAsyncKeyState(‘d’))&&player.x+player.r<MAP_WIDTH)
{
player.x=player.x+a;
}
}
double Distance(struct ball a ,struct ball b)
{
double suma=(a.x-b.x)(a.x-b.x);
double sumb=(a.y-b.y)
(a.y-b.y);
return sqrt(suma+sumb);
}
void EatFood()
{
for(int i=0;i<FOOD_NUM;i++)
{
if(food[i].flag1&&Distance(food[i],player)<player.r)
{
food[i].flag=0;
player.r += food[i].r / 2;
break;
}
}
}
void EatAi()
{
for(int i=0;i<AI_NUM;i++)
{
if(AI[i].flag
0)
{
continue;
}
if(AI[i].r>player.r&&Distance(AI[i],player)<AI[i].r+2)
{
player.flag=0;
AI[i].r+=player.r/2;
HWND h = GetHWnd();
if(MessageBox(h,「您已死亡,按下確認重生」,「很遺憾!」,MB_OKCANCEL)IDOK)
{
player.x = rand()%MAP_WIDTH;
player.y = rand()%MAP_HEIGHT;
player.r = rand()%10+5;
player.flag=1;
player.color=RGB(rand()%256,rand()%256,rand()%256);
}
else if(MessageBox(h,「您已死亡,按下確認重生」,「很遺憾!」,MB_OKCANCEL)IDCANCEL)
{
MessageBox(h,「成功退出」,「提示:」,MB_OKCANCEL);
exit(0);
}
}
else if(AI[i].r<player.r&&Distance(AI[i],player)<player.r+2)
{
AI[i].flag=0;
player.r+=AI[i].r/3;
break;
}
}
}
void chase(struct ball *chase ,struct ball *run)
{
if (rand() % 2 == 0) {
if (chase->x < run->x)
chase->x += 2;
else
chase->x -=2;
}
else {
if (chase->y < run->y)
chase->y += 2;
else
chase->y -= 2;
}
}
void AiMove()
{
for(int i = 0;i<AI_NUM;i++)
{
int min=MAP_WIDTH;
int index=-1;
if(AI[i].flag
0)
{
continue;
}
for(int j=i+1;j<AI_NUM;j++)
{
if(Distance(AI[i],AI[j])<min&&AI[j].flag
1)
{
min=Distance(AI[i],AI[j]);
index=j;
}
}
if(index!=-1)
{
chase(&AI[i],&AI[index]);
}
}
}
void AiEatAi()
{
for(int i=0;i<AI_NUM;i++)
{
if(AI[i].flag0)
{
continue;
}
for(int j=i+1;j<AI_NUM;j++)
{
if(Distance(AI[i],AI[j])<AI[i].r&&AI[j].flag
1)
{
AI[j].flag=0;
AI[i].r+=AI[j].r/4;
}
}
}
}
void AiEatFood()
{
for(int i=0;i<AI_NUM;i++)
{
if(AI[i].flag==0)
{
continue;
}
for(int j=0;j<FOOD_NUM;j++)
{
if(Distance(AI[i],food[j])0&&food[j].flag1)
{
food[j].flag=0;
AI[i].r+=food[j].r/4;
}
}
}
}
void pdwin()
{
if(player.r>WIN_WIDTH||player.r>WIN_HEIGHT)
{
HWND h=GetHWnd();
MessageBox(h,「你贏了!」,「恭喜!」,MB_OK);
exit(0);
}
}
void pdlose()
{
for(int i=0;i<AI_NUM;i++)
{
if(AI[i].r>WIN_WIDTH2||AI[i].r>WIN_HEIGHT2)
{
HWND h=GetHWnd();
MessageBox(h,「你輸了!」,「菜雞!」,MB_OK);
exit(0);
}
}
}
int main()
{
initgraph(WIN_WIDTH,WIN_HEIGHT);
GameInit();
while(1)
{
GameDraw();
move(SPEND);
EatFood();
EatAi();
AiMove();
AiEatAi();
AiEatFood();
pdwin();
pdlose();
}
getchar();
closegraph();
return 0;
}