已完成部分:1.设置一个登录界面输入用户名和密码从而登录游戏。
2.设置了界面有开始游戏的功能和退出游戏的功能。
3.点击开始游戏就能够进入游戏中去
游戏包括食物,Ai小球,玩家
玩家可以去吃掉食物和比自己小的Ai,Ai会自动追逐比自己小的小球如果追到会吃 掉它并且在追逐过程中会吃掉周围的食物,食物被吃掉以后会自动再次生成,Ai也 会不断自动生成,吃了其它球或者食物的小球会变大。
4.如果玩家不小心被比它大的球吃掉会跳出界面从而进行选择是继续游戏还是退出 游戏并且吃掉它的Ai球会变大。
未完成部分:1.还需去做一个注册页面,如果已经有账号直接进行登录,如果没有就需要进行注 册。
2.在游戏过程中加入一个播放音乐的功能,并且还要随时能切换音乐,还需有一个 暂停音乐的按钮和继续音乐的按钮。
3.还需做一个排行榜,用于观察当前排行情况。
4.需加一个游戏结束的条件,例如玩家碰到边界了,和时间到了,所以这里还需设 计一个倒计时,再游戏结束后展示出排行榜。
5.如果可以还可以多设计集中食物的形状,Ai和玩家的皮肤。
#include<stdio.h>
#include<easyx.h>
#include<graphics.h>
#include<string.h>
#include<math.h>
#define WIN_WIDTH 1024
#define WIN_HEIGTH 640
#define MAP_WIDTH 1024*3//地图宽度
#define MAP_HEIGTH 640*3//地图高度
#define FOOD_NUM 500
#define AI_NUM 200
/*while (1) {
//1.定义鼠标变量获取鼠标消息
MOUSEMSG msg = GetMouseMsg();
//2.分类讨论鼠标消息
switch (msg.uMsg)
{
case WM_LBUTTONDOWN:
break;
case WM_RBUTTONDOWN:
break;
}
}*/
typedef struct Admin
{
char UserName[64];
char PassWord[64];
}Admin;
Admin AdminUser;
struct button
{
int x;
int y;
int width;
int heigh;
COLORREF color;
char* text;
};
struct Ball//球结构,食物,玩家,Ai
{
float x;
float y;
int r;//半径
DWORD color;
int flag;//是否存活标志
};
void fun1();
//登录页面
/*void FirstPage()
{
void AdminSignIn();
fun1();
initgraph(760, 760);
MOUSEMSG m;
cleardevice();
IMAGE picture;
loadimage(&picture, "E:\\简单,困难,非常困难.jpg", 760, 760);
putimage(0, 0, &picture);
setbkmode(TRANSPARENT);
setfillcolor(GREEN);
//大标题
char FirstTitle[20] = { "球球大作战" };
settextstyle(60, 0, "黑体");
outtextxy(150, 100, FirstTitle);
//两个选项背景
fillrectangle(230, 445, 560, 490);
fillrectangle(230, 565, 560, 610);
setbkmode(TRANSPARENT);
//两个选项的文字
settextstyle(40, 0, "黑体");
//两个选项
char FirstSelec1[20] = { "用户操作界面" };
char FirstSelec2[20] = { "退出程序" };
outtextxy(240, 450, FirstSelec1);
outtextxy(240, 570, FirstSelec2);
//进入主界面选项操作界面
while (1)
{
m = GetMouseMsg();//获取鼠标操作
if (m.x >= 230 && m.x <= 560 && m.y >= 445 && m.y <= 490)
{
setlinecolor(RED);
rectangle(230, 445, 560, 490);
if (m.uMsg == WM_LBUTTONDOWN)
{
AdminSignIn();
}
}
else if (m.x >= 230 && m.x <= 560 && m.y >= 565 && m.y <= 610)//退出
{
setlinecolor(RED);
rectangle(230, 565, 560, 610);
if (m.uMsg == WM_LBUTTONDOWN)
{
exit(0);
}
}
}
}*/
//登录
void AdminSignIn()
{
//打开文件将账号和密码读过来
errno_t err;
FILE* fp;
err = fopen_s(&fp,"A.txt", "r");
if (fp == NULL)
{
return;
}
//读取到对应的使用者结构体数组
fscanf(fp, "%s\t%s\t\n", AdminUser.UserName, AdminUser.PassWord);
fclose(fp);
char ReceAcctNumber[64];
TCHAR InputAcct[] = _T("请输入用户名");
InputBox(ReceAcctNumber, 10, InputAcct);
char ReceAcctPassWord[64];
TCHAR InputPass[] = _T("请输入密码");
InputBox(ReceAcctPassWord, 10, InputPass);
//如果用户名和密码都正确才进度到管理员界面
//否则弹窗提示错误
if (strcmp(AdminUser.UserName, ReceAcctNumber) == 0 && strcmp(AdminUser.PassWord, ReceAcctPassWord) == 0)
{
fun1();
}
else
{
HWND SignError = GetHWnd();
int isok = MessageBox(SignError, "用户名或密码错误!", "提示", MB_OK);
}
}
struct Ball player;
struct Ball food[FOOD_NUM];
struct Ball ai[AI_NUM];
IMAGE map(MAP_WIDTH, MAP_HEIGTH);
POINT g_CameraPos;//定义摄像机坐标,左上角的位置,地图贴图位置
//摄像机位置
void CameraPos()
{
g_CameraPos.x = player.x - WIN_WIDTH / 2;
g_CameraPos.y = player.y - WIN_HEIGTH / 2;
if (g_CameraPos.x < 0)
{
g_CameraPos.x = 0;
}
if (g_CameraPos.y < 0)
{
g_CameraPos.y = 0;
}
if (g_CameraPos.x > MAP_WIDTH - WIN_WIDTH)
{
g_CameraPos.x = MAP_WIDTH - WIN_WIDTH;
}
if (g_CameraPos.y > MAP_HEIGTH - WIN_HEIGTH)
{
g_CameraPos.y = MAP_HEIGTH - WIN_HEIGTH;
}
}
//初始化球球数据
void GameInit()
{
srand(GetTickCount());
//初始化玩家
player.x = rand() % MAP_WIDTH;
player.y = rand() % MAP_HEIGTH;
player.r = rand() % 10 + 10;
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_HEIGTH;
food[i].r = rand() % 10 + 1;
food[i].flag = 1;
food[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
//初始化Ai
for (int i = 0; i < AI_NUM; i++)
{
ai[i].x = rand() % MAP_WIDTH;
ai[i].y = rand() % MAP_HEIGTH;
ai[i].r = rand() % 10 + 10;
ai[i].flag = 1;
ai[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
}
void GameDraw()
{
SetWorkingImage(&map);
setbkcolor(BLACK);
cleardevice();
//画食物
for (int i = 0; i < FOOD_NUM; i++)
{
if (food[i].flag == 1)
{
setfillcolor(food[i].color);
solidcircle(food[i].x, food[i].y, food[i].r);
}
}
//画Ai
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);
}
}
//画玩家
setfillcolor(player.color);
solidcircle(player.x, player.y, player.r);
//显示名字
setbkmode(TRANSPARENT);
settextcolor(RGB(200, 191, 231));
outtextxy(player.x + 20, player.y, "哈哈哈哈");
SetWorkingImage();
CameraPos();
putimage(0, 0, WIN_WIDTH, WIN_HEIGTH, &map, g_CameraPos.x, g_CameraPos.y);
}
//初始化按钮属性
struct button* creatButton(int x, int y, int width, int heigh, COLORREF color, const char* text)
{
struct button* pB = (struct button*)malloc(sizeof(struct button));
pB->x = x;
pB->y = y;
pB->width = width;
pB->heigh = heigh;
pB->color = color;
pB->text = (char*)malloc(strlen(text) + 1);
strcpy(pB->text, text);
return pB;
}
//画按钮:矩形 rectangle(int x,int y,int x1,int y1);
void drawButton(struct button* pB)
{
int w, h;
settextcolor(BLACK);
setfillcolor(pB->color);
settextstyle(50, 0, "楷体");
w = (pB->width - textwidth(pB->text)) / 2;
h = (pB->heigh - textheight(pB->text)) / 2;
setbkmode(TRANSPARENT);
solidrectangle(pB->x, pB->y, pB->x + pB->width, pB->y + pB->heigh);
outtextxy(pB->x + w, pB->y + h, pB->text);
}
int mouse(struct button* pB, MOUSEMSG msg)
{
if (pB->x <= msg.x && msg.x <= pB->x + pB->width && pB->y <= msg.y && msg.y <= pB->heigh + pB->y)
{
return 1;
}
else
return 0;
}
void imag()
{
void fun();
/*initgraph(480, 640);
void fun();
ExMessage monse;
IMAGE img;
loadimage(&img, "E:\\球球大作战.jpg", 480, 640);
putimage(0, 0, &img);
struct button* pB = creatButton(120, 150, 240, 70, RGB(153, 217, 234), "开始游戏");
//struct button* so = creatButton(120, 240, 240, 70, RGB(0, 162, 232), "继续游戏");
struct button* hard = creatButton(120, 330, 240, 70, RGB(63, 72, 204), "退出游戏");
while (1)
{
BeginBatchDraw();
drawButton(pB);
//drawButton(so);
drawButton(hard);
MOUSEMSG msg = GetMouseMsg();
while (1)
{
if (peekmessage(&monse, EM_MOUSE))
{
if (monse.x >= 120 && monse.x <= 120 + 240 && monse.y >= 150 && monse.y <= 150 + 70)
{
pB->color = RGB(255, 174, 201);
}
else
{
pB->color = RGB(153, 217, 234);
}
if (mouse(hard, msg))
{
hard->color = RGB(255, 174, 201);
}
else
{
hard->color = RGB(63, 72, 204);
}
switch (monse.message)
{
case WM_LBUTTONDOWN:
if (monse.x >= 120 && monse.x <= 120 + 240 && monse.y >= 150 && monse.y <= 150 + 70)
{
fun();
}
}
}
}
if (mouse(so, msg))
{
so->color = RGB(255, 174, 201);
}
else
{
so->color = RGB(0, 162, 232);
}
FlushBatchDraw();
}*/
initgraph(480, 640);
cleardevice();
IMAGE img;
loadimage(&img, "E:\\球球大作战.jpg", 480, 640);
putimage(0, 0, &img);
setbkmode(TRANSPARENT);
struct button* pB = creatButton(120, 150, 240, 70, RGB(153, 217, 234), "开始游戏");
struct button* hard = creatButton(120, 330, 240, 70, RGB(63, 72, 204), "退出游戏");
drawButton(pB);
drawButton(hard);
ExMessage monse;
while (1)
{
if (peekmessage(&monse, EM_MOUSE))
{
if (monse.x >= 120 && monse.x <= 120 + 240 && monse.y >= 150 && monse.y <= 150 + 70)
{
pB->color = RGB(255, 174, 201);
}
else pB->color = RGB(153, 217, 234);
if (monse.x >= 120 && monse.x <= 120 + 240 && monse.y >= 330 && monse.y <= 330 + 70)
{
hard->color = RGB(255, 174, 201);
}
else hard->color = RGB(63, 72, 204);
switch (monse.message)
{
case WM_LBUTTONDOWN:
if (monse.x >= 120 && monse.x <= 120 + 240 && monse.y >= 150 && monse.y <= 150 + 70)
{
fun();
}
if (monse.x >= 120 && monse.x <= 120 + 240 && monse.y >= 330 && monse.y <= 330 + 70)
{
exit(0);
}
}
}
}
}
//玩家被吃掉
void Eat()
{
void fun();
initgraph(480, 640);
cleardevice();
IMAGE img;
loadimage(&img, "E:\\球球大作战2.png", 480, 640);
putimage(0, 0, &img);
setbkmode(TRANSPARENT);
struct button* so = creatButton(120, 150, 240, 70, RGB(0, 162, 232), "继续游戏");
struct button* hard = creatButton(120, 330, 240, 70, RGB(63, 72, 204), "退出游戏");
drawButton(so);
drawButton(hard);
ExMessage monse;
while (1)
{
if (peekmessage(&monse, EM_MOUSE))
{
if (monse.x >= 120 && monse.x <= 120 + 240 && monse.y >= 150 && monse.y <= 150 + 70)
{
so->color = RGB(255, 174, 201);
}
else so->color = RGB(0, 162, 232);
if (monse.x >= 120 && monse.x <= 120 + 240 && monse.y >= 330 && monse.y <= 330 + 70)
{
hard->color = RGB(255, 174, 201);
}
else hard->color = RGB(63, 72, 204);
switch (monse.message)
{
case WM_LBUTTONDOWN:
if (monse.x >= 120 && monse.x <= 120 + 240 && monse.y >= 150 && monse.y <= 150 + 70)
{
fun();
}
if (monse.x >= 120 && monse.x <= 120 + 240 && monse.y >= 330 && monse.y <= 330 + 70)
{
exit(0);
}
}
}
}
}
void GameMove(int speed)
{
if (GetAsyncKeyState(VK_UP) && player.y - player.r > 0)
{
player.y -= speed;
}
if (GetAsyncKeyState(VK_DOWN) && player.y + player.r < MAP_HEIGTH)
{
player.y += speed;
}
if (GetAsyncKeyState(VK_LEFT) && player.x - player.r > 0)
{
player.x -= speed;
}
if (GetAsyncKeyState(VK_RIGHT) && player.x + player.r < MAP_WIDTH)
{
player.x += speed;
}
//作弊测试
if (GetAsyncKeyState(VK_SPACE) && player.y - player.r > 0)
{
player.r += speed;
}
if (GetAsyncKeyState('A') && player.y - player.r > 0)
{
player.r -= speed;
}
}
//两点之间的距离
float DisTance(struct Ball b1, struct Ball b2)
{
return sqrt((float)(b1.x - b2.x) * (b1.x - b2.x) + (b1.y - b2.y) * (b1.y - b2.y));
}
//追逐算法
void ChaseAlgorithom(struct Ball* chase, struct Ball* run)
{
if (rand() % 2 == 0)
{
if (chase->x < run->x)
{
chase->x += 0.2;
}
else
{
chase->x -= 0.2;
}
}
else
{
if (chase->y < run->y)
{
chase->y += 0.2;
}
else
{
chase->y -= 0.2;
}
}
}
//吃食物
void EatFood()
{
for (int i = 0; i < FOOD_NUM; i++)
{
if (food[i].flag == 1 && DisTance(food[i], player) < player.r)
{
food[i].flag = 0;
player.r += food[i].r / 30;
}
if (food[i].flag == 0)
{
food[i].x = rand() % MAP_WIDTH;
food[i].y = rand() % MAP_HEIGTH;
food[i].r = rand() % 10 + 1;
food[i].flag = 1;
food[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
}
}
//吃Ai
void EatAi()
{
for (int i = 0; i < AI_NUM; i++)
{
if (ai[i].flag == 0)
continue;
if (DisTance(ai[i], player) <= player.r - ai[i].r)
{
ai[i].flag = 0;
player.r += ai[i].r / 30;
break;
}
else if (DisTance(ai[i], player) <= ai[i].r - player.r)
{
/*player.x = rand() % MAP_WIDTH;
player.y = rand() % MAP_HEIGTH;
player.r = rand() % 10 + 10;
//player.flag = 1;
player.color = RGB(rand() % 256, rand() % 256, rand() % 256);*/
Eat();
ai[i].r += player.r / 30;
break;
}
}
}
//追逐算法
void AiMove()
{
for (int i = 0; i < AI_NUM; i++)
{
float min = MAP_WIDTH;
int index = -1;
if (ai[i].flag == 1)
{
for (int j = 0; j < AI_NUM; j++)
{
if (ai[i].r > ai[j].r && ai[i].flag == 1 && i != j)
{
if (DisTance(ai[i], ai[j]) <= min)
{
min = DisTance(ai[i], ai[j]);
index = j;
}
}
}
}
if (index != -1)
{
ChaseAlgorithom(&ai[i], &ai[index]);
}
}
}
//Ai吃Ai
void EatAiTwo()
{
for (int i = 0; i < AI_NUM; i++)
{
for (int j = 0; j < AI_NUM; j++)
{
if (ai[i].flag == 0)
{
break;
}
if (ai[j].flag == 0 || i == j)
continue;
else if (DisTance(ai[i], ai[j]) <= ai[i].r - ai[j].r / 3)
{
ai[j].flag = 0;
ai[i].r += ai[j].r / 30;
for (int k = 0; k < AI_NUM; k++)
{
if (ai[k].flag == 0)
{
ai[k].x = rand() % MAP_WIDTH;
ai[k].y = rand() % MAP_HEIGTH;
ai[k].r = rand() % 10 + 10;
ai[k].flag = 1;
ai[k].color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
}
break;
}
}
}
}
//Ai吃食物
void EatAiFood()
{
for (int j = 0; j < AI_NUM; j++)
{
for (int i = 0; i < FOOD_NUM; i++)
{
if (food[i].flag == 1 && DisTance(food[i], ai[j]) < ai[j].r)
{
food[i].flag = 0;
ai[j].r += food[i].r / 30;
}
}
}
}
void fun()
{
initgraph(WIN_WIDTH, WIN_HEIGTH);
GameInit();
while (1)
{
EatFood();
EatAi();
EatAiFood();
EatAiTwo();
GameDraw();
GameMove(1);
AiMove();
}
getchar();
closegraph();
}
void fun1()
{
imag();
getchar();
closegraph();
}
int main()
{
AdminSignIn();
return 0;
}