做网站公司 上海,顺德网站建设哪家好,在线网站seo优化,wordpress显示作者一、题目
贪吃蛇游戏机制是通过控制蛇上下左右移动并吃到食物得分。
蛇头碰到墙壁或者碰到蛇身就游戏结束。
食物随机生成#xff0c;蛇吃到食物之后蛇身变长#xff0c;蛇速加快。 二、算法
1. 初始化游戏地图并打印#xff0c;地图的边缘是墙#xff0c;地图的每个坐…一、题目
贪吃蛇游戏机制是通过控制蛇上下左右移动并吃到食物得分。
蛇头碰到墙壁或者碰到蛇身就游戏结束。
食物随机生成蛇吃到食物之后蛇身变长蛇速加快。 二、算法
1. 初始化游戏地图并打印地图的边缘是墙地图的每个坐标都有属性EMPTY、WALL、FOOD、HEAD、BODY通过Window.h库里面的函数控制光标跳转和颜色。
2. 初始化蛇蛇是一个单独的类类里面的属性有蛇头、蛇身、长度、速度蛇头一个SnakeNode节点蛇身是一个SnakeNode指针每个SnakeNode都是一个x、y坐标用于表示蛇在地图上的位置。
3. 随机生成食物蛇移动的下一步如果是食物则得分若下一步是墙壁或蛇身则游戏失败。
4. 通过键盘输入控制方向若键盘没有输入则保持方向不变。 三、代码
#define _CRT_SECURE_NO_WARNINGS 1#pragma warning (disable:4996)
#include iostream
#include Windows.h
#include conio.h
#include ctime
#include vector
using namespace std;#define ROW 22
#define COL 42#define EMPTY 0
#define WALL 1
#define FOOD 2
#define HEAD 3
#define BODY 4#define COL_WALL 6
#define COL_FOOD 12
#define COL_SNAKE 10#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define SPACE 32
#define ESC 27
#define ENTER 13int g_map[ROW][COL] { 0 };
int g_grade 0;void CursorJump(int x, int y)
{COORD pos; //定义光标位置的结构体变量pos.X x;pos.Y y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); //设置光标位置
}void Color(int x)
{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); //设置颜色// 6——土黄色 7——白色 10——绿色 12——红色
}void SysInit()
{srand((unsigned int)time(NULL));system(title 贪吃蛇);system(mode con cols84 lines23); //设置终端窗口大小CONSOLE_CURSOR_INFO curInfo; //光标信息结构体变量curInfo.dwSize 1;curInfo.bVisible FALSE; //光标光标隐藏不可见SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), curInfo); //设置光标信息
}void MapInit()
{for (int i 0; i ROW; i){for (int j 0; j COL; j){CursorJump(2 * j, i);if (i 0 || i ROW - 1 || j 0 || j COL - 1){Color(COL_WALL);g_map[i][j] WALL;cout ■;}else{g_map[i][j] EMPTY;cout ;}}}Color(7);CursorJump(0, ROW);cout 当前得分是 g_grade;
}void RandFood()
{int row, col;do{row rand() % ROW;col rand() % COL;} while (g_map[row][col] ! EMPTY);g_map[row][col] FOOD;Color(COL_FOOD);CursorJump(2 * col, row);cout ●;
}class Snack
{
public:Snack(){len 2;rate 3000;head.x COL / 2;head.y ROW / 2;g_map[head.y][head.x] HEAD;body.resize(ROW * COL, Pos(0, 0));for (int i 0; i len; i){body[i].x head.x - i - 1;body[i].y head.y;g_map[body[i].y][body[i].x] BODY;}}void PrintSnake(int flag){if (flag){// 打印蛇Color(COL_SNAKE);CursorJump(2 * head.x, head.y);cout ◆;for (int i 0; i len; i){CursorJump(2 * body[i].x, body[i].y);cout ◇;}}else{// 覆盖蛇if (body[len - 1].x ! 0){CursorJump(2 * body[len - 1].x, body[len - 1].y);cout ;}}}void Judge(int x, int y){if (g_map[head.y y][head.x x] FOOD){// 得分g_grade 10;len;if (rate 1000)rate - 50;Color(7);CursorJump(0, ROW);cout 当前得分是 g_grade;RandFood();}else if (g_map[head.y y][head.x x] WALL|| g_map[head.y y][head.x x] BODY){// 失败Sleep(2000);Color(7);system(cls);cout GAME OVER! endl;cout 游戏失败! endl;exit(0);}}void Move(int x, int y){Judge(x, y);PrintSnake(0);int tail len - 1;g_map[body[tail].y][body[tail].x] EMPTY;while (tail 0){body[tail].x body[tail - 1].x;body[tail].y body[tail - 1].y;--tail;}body[0].x head.x;body[0].y head.y;g_map[body[0].y][body[0].x] BODY;head.x x;head.y y;g_map[head.y][head.x] HEAD;PrintSnake(1);}void Run(int x, int y){int t 0;while (1){if (t 0)t rate;while (--t){if (kbhit() ! 0)break;}if (t 0)Move(x, y);elsebreak;}}void Play(){int dir RIGHT;int old dir;while (1){switch (dir){case w:case W:case UP:Run(0, -1);old dir;break;case s:case S:case DOWN:Run(0, 1);old dir;break;case a:case A:case LEFT:Run(-1, 0);old dir;break;case d:case D:case RIGHT:Run(1, 0);old dir;break;case SPACE:system(pausenul);break;case ESC:system(cls);cout ESC 退出游戏 endl;exit(0);}dir getch();switch (dir){case w:case W:case UP:case s:case S:case DOWN:if (old UP || old DOWN)dir old;break;case a:case A:case LEFT:case d:case D:case RIGHT:if (old LEFT || old RIGHT)dir old;break;case SPACE:case ESC:break;default:dir old;}}}private:struct Pos{int x, y;Pos() {}Pos(int x1, int y1): x(x1), y(y1){}};Pos head;vectorPos body;int len;int rate;
};int main()
{SysInit();MapInit();RandFood();Snack s;s.Play();return 0;
} 四、测试
文章转载自: http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.ndhxn.cn.gov.cn.ndhxn.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.pqwrg.cn.gov.cn.pqwrg.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.tnfyj.cn.gov.cn.tnfyj.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.cwjsz.cn.gov.cn.cwjsz.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.nwzcf.cn.gov.cn.nwzcf.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.fgrkc.cn.gov.cn.fgrkc.cn http://www.morning.wfzlt.cn.gov.cn.wfzlt.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.wnqfz.cn.gov.cn.wnqfz.cn http://www.morning.bgrsr.cn.gov.cn.bgrsr.cn http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.xclgf.cn.gov.cn.xclgf.cn http://www.morning.rdzgm.cn.gov.cn.rdzgm.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.pnfwd.cn.gov.cn.pnfwd.cn http://www.morning.ktrzt.cn.gov.cn.ktrzt.cn http://www.morning.xrwbc.cn.gov.cn.xrwbc.cn http://www.morning.cwgn.cn.gov.cn.cwgn.cn http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.trrpb.cn.gov.cn.trrpb.cn http://www.morning.nwrzf.cn.gov.cn.nwrzf.cn http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.ttdbr.cn.gov.cn.ttdbr.cn http://www.morning.nrftd.cn.gov.cn.nrftd.cn http://www.morning.wrdpj.cn.gov.cn.wrdpj.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.ykshx.cn.gov.cn.ykshx.cn http://www.morning.rdmz.cn.gov.cn.rdmz.cn http://www.morning.qqnh.cn.gov.cn.qqnh.cn http://www.morning.xxlz.cn.gov.cn.xxlz.cn http://www.morning.qxljc.cn.gov.cn.qxljc.cn http://www.morning.21r000.cn.gov.cn.21r000.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.tzzfy.cn.gov.cn.tzzfy.cn http://www.morning.tkzqw.cn.gov.cn.tkzqw.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.knpbr.cn.gov.cn.knpbr.cn http://www.morning.ypjjh.cn.gov.cn.ypjjh.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.fgkrh.cn.gov.cn.fgkrh.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.sglcg.cn.gov.cn.sglcg.cn http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.pfmsh.cn.gov.cn.pfmsh.cn http://www.morning.phgz.cn.gov.cn.phgz.cn http://www.morning.fppzc.cn.gov.cn.fppzc.cn http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.rzysq.cn.gov.cn.rzysq.cn http://www.morning.rtkgc.cn.gov.cn.rtkgc.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.qshxh.cn.gov.cn.qshxh.cn http://www.morning.wnnts.cn.gov.cn.wnnts.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.bjjrtcsl.com.gov.cn.bjjrtcsl.com http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn