#include<iostream>
#include<ctime>
#include<algorithm>
#include<graphics.h>
#include<conio.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
using namespace std;
#define x_max 37
#define y_max 23
#define block_size 25
#define SHANG 1
#define XIA 2
#define ZUO 3
#define YOU 4
int kuan = x_max*block_size;
int gao = y_max*block_size;
int PERIOD = 2000;
int map[x_max][y_max] = { 0 };//0为普通地图,1为食物,3为障碍物
int dir;//SHANG,XIA,ZUO,YOU
int len = 3, th = len-1;
int x_new[x_max + 1] = { 0 }, y_new[x_max + 1] = { 0 };
int x_old[x_max + 1] = { 0 }, y_old[x_max + 1] = { 0 };
int counter = 0;
int food_x, food_y;
int obs_x, obs_y;
int x[20], y[20];
int tail_x, tail_y;
int button1_x, button2_x, button_y, buttonwidth, buttonheight;//开始界面的按钮数据
int button3_x, button4_x, button_y2, buttonwidth2, buttonheight2;//选择界面的按钮数据
char key_down;
bool gameover = 0;
int score[100] = { 0 }, num = 0;
MOUSEMSG point;
void initdata()//如果修改了上方数据,也要修改此函数
{
len = 3, th = len - 1;
PERIOD = 2000;
counter = 0;
memset(map, 0, sizeof(map));
memset(x_new, 0, sizeof(x_new));
memset(y_new, 0, sizeof(y_new));
memset(x_old, 0, sizeof(x_old));
memset(y_old, 0, sizeof(y_old));
//清空输入缓冲区,fflush(stdin)不行
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
}
void fillblock(int x, int y)
{
int x1, x2, y1, y2;
x1 = x*block_size;
y1 = y*block_size;
x2 = x1 + block_size;
y2 = y1 + block_size;
fillrectangle(x1, y1, x2, y2);
x_old[th] = x;
y_old[th] = y;
th--;
if (th < 0)
th = len - 1;
map[x][y] = 1;
}
void start_bkg()
{
initgraph(kuan, gao,SHOWCONSOLE);
IMAGE bgd, sta, qui;
loadimage(&bgd, _T("./bkg2.jfif"), kuan, 2 * gao);
buttonwidth = 193;
buttonheight = 105;
loadimage(&sta, _T("./start.png"), buttonwidth, buttonheight);
loadimage(&qui, _T("./quit.png") , buttonwidth, buttonheight);
BeginBatchDraw();
putimage(0, 0, &bgd);
button1_x = kuan / 2 - 193 - 50;
button2_x = kuan / 2 + 50;
button_y = 440;
putimage(button1_x, button_y, &sta);
putimage(button2_x, button_y, &qui);
FlushBatchDraw();
}
void initmap()
{
//背景色
setbkcolor(CYAN);
cleardevice();
//文本
settextcolor(WHITE);
settextstyle(35, 0, _T("黑体"));
setbkmode(TRANSPARENT);
//填充色
setfillcolor(YELLOW);
//网格线
setlinecolor(WHITE);
for (int i = 0; i <= kuan-3*block_size; i += block_size)
line(i, 0, i, gao);
for (int i = 0; i <= gao; i += block_size)
line(0, i, kuan-3*block_size, i);
setlinecolor(RED);
//分数栏
IMAGE currpts;
loadimage(&currpts, _T("./currpoint.png"));
putimage((kuan - 5 * block_size / 2),0, &currpts);
IMAGE high;
loadimage(&high, _T("./highest.png"));
putimage((kuan - 5 * block_size / 2),23*block_size/2, &high);
}
int randomnum(int mod)
{
time_t t;
srand((int)time(&t));
return rand() % mod;
}
void drawfood()
{
int cir_x = food_x * block_size + block_size / 2 + 1;
int cir_y = food_y * block_size + block_size / 2 + 1;
setfillcolor(BLUE);
setlinecolor(WHITE);
fillcircle(cir_x, cir_y, block_size / 2 - 1);
setfillcolor(YELLOW);
setlinecolor(RED);
map[food_x][food_y] = 2;
}
void drawobs(int _x,int _y)
{
int left = _x * block_size;
int top = _y * block_size;
int right = left + block_size;
int floor = top + block_size;
setfillcolor(RED);
setlinecolor(WHITE);
fillrectangle(left, top, right, floor);
setfillcolor(YELLOW);
setlinecolor(RED);
map[_x][_y] = 3;
}
void createfood()
{
food_x = randomnum(x_max-3);
food_y = randomnum(y_max);
while (map[food_x][food_y] != 0)
{
food_x = randomnum(x_max-3);
food_y = randomnum(y_max);
}
drawfood();
}
void createobs()
{
obs_x = randomnum(x_max - 3);
obs_y = randomnum(y_max);
while (map[obs_x][obs_y] != 0)
{
obs_x = randomnum(x_max - 3);
obs_y = randomnum(y_max);
}
drawobs(obs_x, obs_y);
}
void initsnake()
{
//从尾到头,三节
fillblock(x_max / 3 - 2, y_max / 3);
fillblock(x_max / 3 - 1, y_max / 3);
setfillcolor(RGB(128, 0, 255));
fillblock(x_max/3, y_max/3);
setfillcolor(YELLOW);
dir = YOU;
}
void snakemove(int direct)
{
if (direct == SHANG)
{
x_new[0] = x_old[0];
y_new[0] = y_old[0] - 1;
}
if (direct == XIA)
{
x_new[0] = x_old[0];
y_new[0] = y_old[0] + 1;
}
if (direct == ZUO)
{
x_new[0] = x_old[0] - 1;
y_new[0] = y_old[0];
}
if (direct == YOU)
{
x_new[0] = x_old[0] + 1;
y_new[0] = y_old[0];
}
//若吃到食物
if (map[x_new[0]][y_new[0]] == 2)
{
//更新食物
createfood();
//蛇身加长
len++;
//画新增的一段
int x1, y1, x2, y2;
x_old[len - 1] = tail_x;
y_old[len - 1] = tail_y;
x1 = tail_x*block_size;
y1 = tail_y*block_size;
x2 = x1 + block_size;
y2 = y1 + block_size;
fillrectangle(x1, y1, x2, y2);
//变速
PERIOD = PERIOD / 10 * 9;
}
//若撞墙or撞到障碍物or撞到自己
if (x_new[0]<0||x_new[0]>=x_max-3||y_new[0]<0||y_new[0]>=y_max||
map[x_new[0]][y_new[0]] == 3||map[x_new[0]][y_new[0]] == 1)
{
gameover = 1;
}
//画新头
setfillcolor(RGB(128, 0, 255));
fillblock(x_new[0], y_new[0]);
setfillcolor(YELLOW);
}
int getdir(int old_dir)
{
key_down = _getch();
if (key_down == 72 || key_down == 'W' || key_down == 'w')
{
if (old_dir == SHANG || old_dir == XIA)
return old_dir;
return SHANG;
}
if (key_down == 80 || key_down == 'S' || key_down == 's')
{
if (old_dir == SHANG || old_dir == XIA)
return old_dir;
return XIA;
}
if (key_down == 75 || key_down == 'A' || key_down == 'a')
{
if (old_dir == ZUO || old_dir == YOU)
return old_dir;
return ZUO;
}
if (key_down == 77 || key_down == 'D' || key_down == 'd')
{
if (old_dir == ZUO || old_dir == YOU)
return old_dir;
return YOU;
}
return old_dir;
}
void end_of_game()
{
cleardevice();
IMAGE end,choose;
loadimage(&end, _T("./over.png"), kuan / 3, 558 * kuan / 3 / 837);
loadimage(&choose, _T("./ending.png"), kuan / 2, 801 * kuan / 2 / 1485);
putimage(kuan/3, gao/6, &end);
putimage(kuan / 4, gao / 2, &choose);
score[num] = len;
num++;
TCHAR s[5];
_stprintf_s(s, _T("%d"), len);
outtextxy(514, 321, s);
sort(score, score + num);
_stprintf_s(s, _T("%d"), score[num-1]);
outtextxy(514, 366, s);
FlushBatchDraw();
}
bool left_in_area(int x1,int y1,int x2,int y2)
{
if (point.uMsg == WM_LBUTTONDOWN)
{
if (point.x<x2 && point.x>x1 && point.y > y1 && point.y < y2)
return 1;
}
return 0;
}
int main()
{
int mode = 0;
while (1)
{
if (gameover) break;
if (mode == 0)//开始界面
{
//创建开始界面,内含按钮数据
start_bkg();
//检测鼠标输入
while (1)
{
if (MouseHit())
{
point = GetMouseMsg();
if (left_in_area(button1_x, button_y, button1_x + buttonwidth, button_y + buttonheight))
{
mode = 1;
goto MODE1;
}
if (left_in_area(button2_x, button_y, button2_x + buttonwidth, button_y + buttonheight))
{
return 0;
}
}
}
getchar();
}
MODE1: if (mode == 1)//游戏界面
{
//创建地图
initmap();
initsnake();
createfood();
createobs();
x[1] = obs_x, y[1] = obs_y;
createobs();
x[2] = obs_x, y[2] = obs_y;
createobs();
x[3] = obs_x, y[3] = obs_y;
createobs();
x[4] = obs_x, y[4] = obs_y;
/*createobs();
x[5] = obs_x, y[5] = obs_y;
createobs();
x[6] = obs_x, y[6] = obs_y;
createobs();
x[7] = obs_x, y[7] = obs_y;
createobs();
x[8] = obs_x, y[8] = obs_y;
createobs();
x[9] = obs_x, y[9] = obs_y;
createobs();
x[10] = obs_x, y[10] = obs_y;
createobs();
x[11] = obs_x, y[11] = obs_y;
createobs();
x[12] = obs_x, y[12] = obs_y; */
//更新历史最高分
TCHAR f[5];
sort(score, score + num);
_stprintf_s(f, _T("%d"), score[num-1]);
FlushBatchDraw();
while (1)
{

海的猫
- 粉丝: 1
最新资源
- STC89C52RC单片机手册.doc
- lowRISC-硬件开发资源
- 网络安全评估和安全法规.ppt
- 高质量C++编程学习笔记.doc
- 欧司朗普通照明产品网络营销年度方案.pptx
- 某网络系统有限公司商业计划书.docx
- 楼宇自动化论文(1).pdf
- 通信设备公司财务管理手册.doc
- 气象局网络视频监控系统方案.doc
- 2022年MATLAB复习知识点整理版.docx
- 中国网络广告效果营销发展趋势――效果网提供.ppt
- 建立卫生网络体系提升群众医疗保障水平调研思考.pdf
- 网络安全宣传周的活动总结2021年.doc
- 中铁工程项目管理标准化手册检查用表(30个).docx
- 基于AT89C51单片机的16x16LED点阵显示的课程设计.doc
- 中国人民银行招聘笔试计算机习题1.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



评论0