当前位置: 首页 > news >正文

网站建议公司电子网站建设ppt模板

网站建议公司,电子网站建设ppt模板,有什么做任务的网站吗,seo文章优化方法一、题目 贪吃蛇游戏机制是通过控制蛇上下左右移动并吃到食物得分。 蛇头碰到墙壁或者碰到蛇身就游戏结束。 食物随机生成,蛇吃到食物之后蛇身变长,蛇速加快。 二、算法 1. 初始化游戏地图并打印,地图的边缘是墙,地图的每个坐…

一、题目

贪吃蛇游戏机制是通过控制蛇上下左右移动并吃到食物得分。

蛇头碰到墙壁或者碰到蛇身就游戏结束。

食物随机生成,蛇吃到食物之后蛇身变长,蛇速加快。

二、算法

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 cols=84 lines=23");    //设置终端窗口大小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("pause>nul");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;vector<Pos> body;int len;int rate;
};int main()
{SysInit();MapInit();RandFood();Snack s;s.Play();return 0;
}

四、测试

http://www.yayakq.cn/news/890714/

相关文章:

  • 如何在国外网站做免费推广建网站 备案
  • dhl做运单的网站360网站备案查询
  • 无棣住房建设局网站希爱力双效片
  • 做瞹瞹瞹视频网站页面设计的步骤
  • 网页设计教学网站做邀请函的网站
  • 站长工具综合查询站长工具淄博网站建设公司有多少家
  • 长沙php的网站建设公司网站结构的规划
  • 合肥网站制作报怎么做自己的优惠价网站
  • 网页游戏网站开发软件开发培训学校三八妇女节
  • 北京食药局网站年检怎么做网站建设中 目录
  • 网站建设gon网络游戏名
  • 百度关键词推广方案绍兴网站优化
  • 个人网站建设的流程店面设计薪酬
  • 网站建设开发收费在线阅读小说网站开发
  • 药业集团网站策划方案范文外包公司不给员工发工资怎么办
  • 厦门做网站的公司中山人才招聘网官网
  • 江苏省建设厅网站建筑电工证青岛的seo服务公司
  • 钦州建设网站济南哪家公司可以做网站
  • 网站诊断seo当前数据是指网络营销方案策划论文
  • 做网站编辑大专可以吗app应用程序
  • 一般网站建设大概需要多少钱wordpress 视频 模版
  • 酒店官方网站建设书北京做网站公司排
  • 晋中建设网站seo 网站排名
  • 调查问卷网站建设方案卖网站赚钱吗
  • 有专门做牙膏的网站吗学校响应式网站模板
  • 有自己的域名怎么建设网站哪里制作网站好
  • 做零食网站的选题理由湖北建设银行官方网站首页
  • 广州专门做网站自助建站系统是怎么实现
  • 企业网站制作优化网站视觉风格
  • 网站已有备案了 现在换空间商还用备案么百度竞价登录入口