#include<graphics.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include <iostream>
#include<easyx.h>
#include<string.h>
#include<mmsystem.h>
#pragma comment (lib,"winmm.lib")
//#include<stdint.h>
//using namespace std;
//#include"struct.h"
#define max_fish 7 //鱼最大数目
#define role1 0 //角色1
#define role2 1 //角色2
#define left 0 //左
#define right 2 //右
#define boundry 200 //边界
#define TIMER_MAX 10 //定时器的数目
#define unit 10 //单位
#define VK_A 65
#define VK_D 68
#define VK_W 87
#define VK_S 83
#define bigger 10 //吃一条鱼变大多少
//#define 单人模式 0
//#define 双人模式 1
//#define 退出游戏 2
int mode; //模式
int graph_x = 1200, graph_y = 700; //窗口大小
int min_w = 50, min_h = 30; //最小的鱼的大小
int somatotype1_w = min_w + 40; //p1体型
int somatotype1_h = min_h + 40;
int somatotype2_w = min_w + 40; //p2体型
int somatotype2_h = min_h + 40;
int p1_x=0, p1_y=0,p1_dir=2; //p1位置坐标
int p2_x= graph_x - 150, p2_y=0,p2_dir=0;
int x = 800, y = 0;//初始位置
TCHAR text[4][20] = { "单人模式","双人模式","游戏说明","退出游戏"};
int button_w = 150, button_h = 40;//按钮大小
IMAGE background; //背景图片
IMAGE fishimg[max_fish][4]; //鱼
struct Fish
{
int x, y; //坐标
int dir; //方向
int type; //类型
int w, h; //大小
};
struct Fish fish[max_fish];
void initfish(int type)
{
if (type == role1)
{
fish[type].x = p1_x;
fish[type].y = p1_y;
fish[type].dir =p1_dir;
fish[type].type = role1;
fish[type].w = somatotype1_w;
fish[type].h = somatotype1_h;
}
else if (type == role2)
{
if (mode == 2)
{
fish[type].x = p2_x;
fish[type].y = p2_y;
fish[type].dir = p2_dir;
fish[type].type = role2;
fish[type].w = somatotype2_w;
fish[type].h = somatotype2_h;
}
else if (mode == 1)
{
fish[type].type = rand() % (max_fish - 2) + 2;
int dir = (rand() % 10) < 5 ? left : right;
fish[type].dir = dir;
fish[type].y = rand() % graph_y + 50;
fish[type].x = dir == left ? rand() % boundry + 1000 : (-1) * (rand() % boundry);
fish[type].w = min_w + 10 * type;
fish[type].h = min_h + 10 * type;
}
}
else
{
fish[type].type = rand() % (max_fish - 2) + 2;
int dir = (rand() % 10) < 5 ? left : right;
fish[type].dir = dir;
fish[type].y = rand() % graph_y;
fish[type].x = dir == left ? rand() % boundry + 1000 : (-1) * (rand() % boundry);
fish[type].w = min_w + 10 * type;
fish[type].h = min_h + 10 * type;
}
}
void loadfish_and_background()
{
loadimage(&background, "./picture/background1.jpg", graph_x, graph_y);
char filename[20] = { "" };
for (int i = 0; i < max_fish; i++)
{
initfish(i);
for (int j = 0; j < 4; j++)
{
if (j == 0)
{
sprintf(filename, "./picture/%d0.jpg", i);
loadimage(&fishimg[i][j], filename, fish[i].w, fish[i].h);
}
else if (j == 1)
{
sprintf(filename, "./picture/%d1.jpg", i);
loadimage(&fishimg[i][j], filename, fish[i].w, fish[i].h);
}
else if (j == 2)
{
sprintf(filename, "./picture/%d2.jpg", i);
loadimage(&fishimg[i][j], filename, fish[i].w, fish[i].h);
}
else if (j == 3)
{
sprintf(filename, "./picture/%d3.jpg", i);
loadimage(&fishimg[i][j], filename, fish[i].w, fish[i].h);
}
}
}
}
void loadfish1(int type)//加载p1鱼
{
char filename[20] = { "" };
initfish(type);
for (int j = 0; j < 4; j++)
{
if (j == 0)
{
sprintf(filename, "./picture/%d0.jpg", type);
loadimage(&fishimg[type][j], filename, somatotype1_w, somatotype1_h);
}
else if (j == 1)
{
sprintf(filename, "./picture/%d1.jpg", type);
loadimage(&fishimg[type][j], filename, somatotype1_w, somatotype1_h);
}
else if (j == 2)
{
sprintf(filename, "./picture/%d2.jpg", type);
loadimage(&fishimg[type][j], filename, somatotype1_w, somatotype1_h);
}
else if (j == 3)
{
sprintf(filename, "./picture/%d3.jpg", type);
loadimage(&fishimg[type][j], filename, somatotype1_w, somatotype1_h);
}
}
}
void loadfish2(int type)//加载p2鱼
{
char filename[20] = { "" };
initfish(type);
for (int j = 0; j < 4; j++)
{
if (j == 0)
{
sprintf(filename, "./picture/%d0.jpg", type);
loadimage(&fishimg[type][j], filename, somatotype2_w, somatotype2_h);
}
else if (j == 1)
{
sprintf(filename, "./picture/%d1.jpg", type);
loadimage(&fishimg[type][j], filename, somatotype2_w, somatotype2_h);
}
else if (j == 2)
{
sprintf(filename, "./picture/%d2.jpg", type);
loadimage(&fishimg[type][j], filename, somatotype2_w, somatotype2_h);
}
else if (j == 3)
{
sprintf(filename, "./picture/%d3.jpg", type);
loadimage(&fishimg[type][j], filename, somatotype2_w, somatotype2_h);
}
}
}
void drawfish()//贴图
{
for (int i = 0; i < max_fish; i++)
{
putimage(fish[i].x, fish[i].y, &fishimg[i][fish[i].dir], SRCAND);
putimage(fish[i].x, fish[i].y, &fishimg[i][fish[i].dir + 1], SRCPAINT);
}
}
void control()//角色移动 p1 为wasd移动 p2为上下左右移动
{
if (GetAsyncKeyState(VK_A))
{
if (fish[role1].x > 0) fish[role1].x--;
fish[role1].dir = left;
}
if (GetAsyncKeyState(VK_D))
{
if (fish[role1].x < graph_x - fish[role1].w) fish[role1].x++;
fish[role1].dir = right;
}
if (GetAsyncKeyState(VK_W))
{
if (fish[role1].y > 0) fish[role1].y--;
}
if (GetAsyncKeyState(VK_S))
{
if (fish[role1].y < graph_y - fish[role1].h) fish[role1].y++;
}
if (mode == 2)
{
if (GetAsyncKeyState(VK_LEFT))
{
if (fish[role2].x > 0) fish[role2].x--;
fish[role2].dir = left;
}
if (GetAsyncKeyState(VK_RIGHT))
{
if (fish[role2].x < graph_x - fish[role2].w) fish[role2].x++;
fish[role2].dir = right;
}
if (GetAsyncKeyState(VK_UP))
{
if (fish[role2].y > 0) fish[role2].y--;
}
if (GetAsyncKeyState(VK_DOWN))
{
if (fish[role2].y < graph_y - fish[role2].h) fish[role2].y++;
}
}
}
void npcmove()//npc移动
{
for (int i = mode; i < max_fish; i++)
{
switch (fish[i].dir)
{
case left:
fish[i].x--;
break;
case right:
fish[i].x++;
break;
}
}
}
void resetfish()//重新生成鱼
{
for (int i = mode; i < max_fish; i++)
{
if (fish[i].x > graph_x+boundry|| fish[i].x < -400)
{
initfish(i);
}
}
}
int OnTimer(int duration, int id)//定时器
{
static int startTime[TIMER_MAX];
int endTime = clock();
if (endTime - startTime[id] >= duration)
{
startTime[id] = endTime;
return 1;
}
return 0;
}
int eatfish(int type)
{
int maxx1 = max(fish[role1].x, fish[type].x);
int maxy1 = max(fish[role1].y, fish[type].y);
int minx1 = min(fish[role1].x + fish[role1].w, fish[type].x + fish[type].w);
int miny1 = min(fish[role1].y + fish[role1].h, fish[type].y + fish[type].h);
if (maxx1 > minx1 || maxy1 > miny1)//不碰撞
{
if (mode == 2)
{
int maxx2 = max(fish[role2].x, fish[type].x);
int maxy2 = max(fish[role2].y, fish[type].y);
int minx2 = min(fish[role2].x + fish[role2].w, fish[type].x + fish[type].w);
int miny2 = min(fish[role2].y + fish[role2].h, fish[type].y + fish[type].h);
if (maxx2 > minx2 || maxy2 > miny2)//不碰撞
{
return 0;
}
else
{
if (fish[role2].w * fish[role2].h > fish[type].w * fish[type].h)
{
mciSendString("play ./music/eat.mp3", NULL, 0, NULL);
somatotype2_w += 0;
somatotype2_h += 0;
fish[role2].w = somatotype2_w;
fish[role2].h = somatotype2_h;
p2_x = fish[role2].x;
p2_y = fish[role2].y;
p2_dir = fish[role2].dir;
loadfish2(role2);
initfish(role2);
initfish(type);
return 0;
}
else
{
mciSendString("play ./music/die.mp3", NULL, 0, NULL);
return 1;
}
}
}
return 0;
}
else
{
if (fish[role1].w * fish[role1].h > fish[type].w * fish[type].h)
{
mciSendString("play ./music/eat.mp3", NULL, 0, NULL);
somatotype1_w += 0;
somatotype1_h += 0;
fish[role1].w = somatotype1_w;
fish[role1].h = somatotype1_h;
p1_x = fish[role1].x;
p1_y = fish[role1].y;
p1_dir = fish[role2].dir;
loadfish1(role1);
initfish(role1);
initfish(type);
return 0;
}
else
{
mciSendString("play ./music/die.mp3", NULL, 0, NULL);
return 1;
}
}
}
int gameover()
{
for (int i = mode; i < max_fish; i++)
{
if (eatfish(i) == 1) return 1;
}
return 0;
}
struct rect//按钮坐标
{
int l,t,r,b;
};
rect Button[4];
int num=0;
void button(int x,int y,TCHAR text[])
{
setbkmode(TRANSPARENT);
setfillcolor(GREEN);
fillroundrect(x, y, x + button_w, y + button_h, 10, 10);
TCHAR s1[] = "Arial BLACK";//字体样式名称
settextstyle(30, 0, s1);//字体高度、宽度、样式
settextcolor(WHITE);//字体颜色
int tx = x + (button_w - textwidth(text)) / 2;
int ty = y + (button_h - textheight(text)) / 2;
outtextxy(tx, ty, text);
if (strcmp(text, "单人模式")==0)num = 0;
else if (strcmp(text, "双人模式") == 0)num = 1;
else if (strcmp(text, "退出游戏") == 0)num = 2;
/*closegraph();
printf("%s\n", &text[0]);*/
Button[num].l = x;
Button[num].l = y;
Button[num].l = x + button_w;
Button[num].l = y + button_h;
/*if (num == 1)
clearrectangle(x, y, x + button_w, y + button_h);*/
}
void tipInfo(rect rc, TCHAR* titles)
{
clearrectangle(rc.l, rc.t, rc.r, rc.b);
int tx = rc.l + (button_w - textwidth(titles)) / 2;
int ty = rc.t + (button_h - textheight(titles)) / 2;
outtextxy(tx, ty, titles);
}
void Main_interface()//开始界面
{
HWND hwnd1 = initgraph(graph_x, graph_y);
setbkcolor(0x3B0000);//背景颜色BGR
cleardevice();//使用当前背景颜色清空绘图设备
button((graph_x - button_w) / 2, 200, text[0]);
button((graph_x - button_w) / 2, 300, text[1]);
button((graph_x - button_w) / 2, 400, text[2]);
button((graph_x - button_w) / 2, 500, text[3]);
ExMessage msg; //声明一个消息指针
while (true)
{
if (peekmessage(&msg, EM_MOUSE))
{
switch (msg.message)
{
case WM_LBUTTONDOWN:
if (msg.x >= (graph_x - button_w) / 2 && msg.x <= (graph_x - button_w) / 2 + button_w && msg.y >= 200 && msg.y <= 200 + button_h)
{
mciSendString("play ./music/button.mp3", NULL, 0, NULL);
mode = 1;
/*closegraph();*/
goto here;
/*printf("芝士单人模式大鱼吃小鱼\n");*/
break;
}
else if (msg.x >= (graph_x - button_w) / 2 && msg.x <= (graph_x - button_w) / 2 + button_w && msg.y >= 300 && msg.y <= 300 + button_h)
{
mciSendString("play ./music/button.mp3", NULL, 0, NULL);
mode = 2;
goto here;
/*closegraph();*/
/*printf("芝士双人模式大鱼吃小鱼\n");*/
break;
}
else if (msg.x >= (graph_x - button_w) / 2 && msg.x <= (graph_x - button_w) / 2 + button_w && msg.y >= 400 && msg.y <= 400 + button_h)
{
mciSendString("play ./music/button.mp3", NULL, 0, NULL);
MessageBox(hwnd1, "p1为wasd控制移动,p2为上下左右控制移动", "芝士大鱼吃小鱼", MB_OK);
}
else if (msg.x >= (graph_x - button_w) / 2 && msg.x <= (graph_x - button_w) / 2 + button_w && msg.y >= 500 && msg.y <= 500 + button_h)
{
mciSendString("play ./music/button.mp3", NULL, 0, NULL);
mode = 0;
closegraph();
goto here;
/*printf("芝士关闭大鱼吃小鱼\n");*/
break;
}
case WM_MOUSEMOVE:
break;
default:break;
}
}
}
here:
printf("再见!\n");
/*getchar();*/
}
int main()
{
mciSendString("open ./music/bgm.mp3", NULL, 0, NULL); //打开bgm音乐
Main_interface();
if (mode == 0)return 0;
mciSendString("play ./music/bgm.mp3 repeat", NULL, 0, NULL); //播放bgm
HWND hwnd=initgraph(graph_x, graph_y);
loadfish_and_background();
BeginBatchDraw();//双缓冲
int flag = 1;
loadfish1(role1);
if (mode == 2)loadfish2(role2);
while (1)
{
putimage(0, 0, &background);
drawfish();
FlushBatchDraw();//显示每一帧
control();//控制移动
if(OnTimer(5,0)) npcmove();
resetfish();
if (gameover()==1)
{
mciSendString("stop ./music/bgm.mp3", NULL, 0, NULL); //停止播放
if (MessageBox(hwnd, "游戏结束", "大鱼吃掉了你的脑子", MB_OKCANCEL) == IDOK)
{
/*goto start;*/
closegraph();
return 0;
}
else
{
MessageBox(hwnd, "已经结束哩", "", MB_OK);
closegraph();
return 0;
}
break;
}
}
EndBatchDraw();//关闭双缓冲
while (1);
//mciSendString("stop ./music/bgm.mp3", NULL, 0, NULL); //停止播放
closegraph();
return 0;
}