首页 > 其他分享 >5.18CSDN贪吃蛇

5.18CSDN贪吃蛇

时间:2023-05-18 20:22:51浏览次数:47  
标签:cout void iter height 贪吃蛇 CSDN snake 5.18 dir

贪吃蛇  速度不要调很慢 会影响判断

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<deque>
#include<ctime>
#include<stdexcept>
using namespace std;

struct Snake { //蛇类结构体
char image;
short x, y; //坐标
};

class snakeGame {
public:
snakeGame();
void printMap();
// 控制光标移动
void gotoxy(short x, short y) {
hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取句柄
pos = {x, y};
SetConsoleCursorPosition(hOut, pos); //移动光标
}
//隐藏光标
void HideCursor()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}
// 初始化蛇身,可根据需要更改初始长度
void initSnake() {
snake.push_front({'@', width / 2, height / 2});
for (int i=0; i<2;++i)
snake.push_back({'+', width/2,static_cast<short>(height/2+i+1)});
}
// 判断是否食物产生位置与蛇身冲突
int WrongLocation() {
for (Snake body : snake)
if(body.x == food_x && body.y == food_y) return 0;
return 1;
}
// 产生食物,并打印
void createFood() {
do {
food_x = rand() % (width - 4) + 2;
food_y = rand() % (height - 2) + 1;
} while (!WrongLocation());//处理冲突
gotoxy(food_x,food_y); cout << '*' << endl; //打印食物
}
void printSnake();
// 清除蛇尾
inline void clearSnake(Snake &tail) {
gotoxy(tail.x, tail.y); cout << ' '; //覆盖蛇尾,不使用清屏函数,避免了闪烁
}
void judgeCrash();
void foodEaten();
// 监控用户键盘输入
void userInput() {
char ch;
switch(ch=getch()) {
case 'w':if (dir != 's') dir = ch;break;
case 'a':if (dir != 'd') dir = ch;break;
case 's':if (dir != 'w') dir = ch;break;
case 'd':if (dir != 'a') dir = ch;break;
case 'v':speed*=0.8;break; case 'b':speed*=1.5;break;
case ' ':gotoxy(width / 2, height); cout << "游戏已暂停,任意键继续"; getch();
gotoxy(width / 2, height); cout << " "; break;
default:break;
}
}
private:
// 以下是程序运行当中需要用到的一些中间变量或者是数据变量。
enum MapSize {height = 40,width = 120}; //地图尺寸
HANDLE hOut; COORD pos;
char dir; //direction
bool beg,eatFood=false;
double speed=200;
deque<Snake> snake;
int food_x,food_y;
int score=0;
};
// 处理吃到食物的情况
void snakeGame::foodEaten() {
createFood();
eatFood=true;
speed*=.8;
++score;
}
// 判断蛇是否撞墙或者吃到自己的尾巴
void snakeGame::judgeCrash() {
int flag=0;
if (snake.size()>=5) {
deque<Snake>::iterator iter = snake.begin() + 1;
int x = (iter-1)->x, y = (iter-1)->y;
for (; iter != snake.end(); ++iter) {
if (iter->x == x && iter->y == y) flag=1;
}}
if (flag || snake.front().x == 1 || snake.front().x == width - 2 || snake.front().y == 0 || snake.front().y == height - 1)//检测是否撞墙或者是否吃到自身
{
gotoxy(width / 2 - 10, height /2);
cout << "游戏结束!您的分数是: " << score << "分(回车继续)"<<endl;
while(1) {
dir = getch();
if (dir == '\r') break;}
runtime_error quit("游戏结束,正常退出"); throw quit;
}
}
// 将蛇身打印出来
void snakeGame::printSnake() {
deque<Snake>::const_iterator iter = snake.begin();
for (; iter <= snake.begin() + 1 && iter < snake.end(); ++iter) {
gotoxy(iter->x, iter->y); cout << iter->image;
}
}
// 打印出边框
void snakeGame::printMap() {
int i;
for (i = 0; i != width; i += 2) cout << "■"; //这个图案宽度占2,高度占1
gotoxy(0, 1);
for (i = 1; i != height; ++i) cout << "■" << endl;
for (i = 1; i != height; ++i) {
gotoxy(width - 2, i); cout << "■";}
gotoxy(0, height - 1);
for (i = 0; i != width; i += 2) cout << "■";
cout << "贪吃蛇:1.方向键开始游戏 2.*代表食物 3.空格键暂停游戏\n 4.键入'v'加速 5.键入'b'减速";
}
// 类的构造函数。
// 包含了程序的初始化(地图绘制,蛇身初始化),程序运行,程序结束等内容
// 是程序最关键的部分
snakeGame::snakeGame() {
HideCursor(); // 隐藏光标
srand(static_cast<unsigned int>(time(NULL)));
beg=true;
Snake tmp1,tmp2;
while (1) {
if(beg) { // 判断是不是第一次运行程序,因为第一次运行需要打印边框
printMap();
dir = getch();
initSnake();
createFood();
beg = eatFood=false;
}
tmp2=snake.back();
tmp1=snake.front();
snake.pop_back();
if (eatFood) { // 如果吃到食物...
tmp2.image='+';
snake.push_back(tmp2);
eatFood=false;
}
else clearSnake(tmp2);
// 判断当前的前进方向,根据dir来进行移动
if (dir == 's') ++tmp1.y;
else if (dir == 'a') --tmp1.x;
else if (dir == 'd') ++tmp1.x;
else --tmp1.y;
try{
judgeCrash(); // 判断是否撞墙或者吃到自己
}
catch(runtime_error &quitSignal) {
throw quitSignal;
}
snake.front().image='+';
snake.push_front(tmp1);
printSnake();
Sleep(speed+30);
if (tmp1.x == food_x && tmp1.y == food_y)
foodEaten();
// 监测用户的键入
if(kbhit()) userInput();
}
}
int main() {
// 设置小黑框的一些参数
system("mode con cols=120 lines=42");
try{
snakeGame game;
}
catch(runtime_error &gameEnd) {
system("cls");
cout<<gameEnd.what();
getch();
}
}

标签:cout,void,iter,height,贪吃蛇,CSDN,snake,5.18,dir
From: https://www.cnblogs.com/galileo9527/p/17413188.html

相关文章

  • 2023.5.18
    importosimportpandasaspd#添加测试数据os.makedirs(os.path.join('.','data'),exist_ok=True)data_file=os.path.join('.','data','house_tiny.csv')withopen(data_file,'w')asf:   f.write('N......
  • 5.18打卡
    一、问题描述:骰子是一个有六个面的正方体,每个面分别印有1~6之间的小圆点代表点数。假设这个游戏的规则是:两个人轮流掷骰子6次,并将每次投掷的点数累加起来。点数多者获胜;点数相同则为平局。要求编写程序模拟这个游戏的过程,并求出玩100盘之后谁是最终的获胜者。二、设计思路:由于每......
  • 5.18
    #include<stdio.h>main(){inti,x,y,last=1;printf("Inputxandy:\n");scanf("%d%d",&x,&y);for(i=1;i<=y;i++)last=last*x%1000;printf("Thelastthreedigitsis:%d\n",last);}......
  • 5.18打卡
    #include<bits/stdc++.h>usingnamespacestd;doublef(intn,intx){if(n==0)return1;elseif(n==1)returnx;elsereturn((2*n-1)*x*f(n-1,x)-(n-1)*f(n-2,x))*1.0/n;}intmain(){intn,x;cin>>n>>x;......
  • 2023.5.18——软件工程日报
    所花时间(包括上课):6h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习。我了解到的知识点:1.了解了一些数据库的知识;2.了解了一些python的知识;3.了解了一些英语知识;5.了解了一些Javaweb的知识;4.了解了一些数学建模的知识;6.了解了一些计算机网络的知识;......
  • 2023.5.18 第二阶段冲刺日报(四)
    今天是冲刺第四天,在昨天进行了开发后,在今日的站立会议中,我们进行了内容和问题的总结首先,我们明确了昨天的开发进度:1.在客户端上已经实现了由客户端到服务端的连接2.在安卓端上已经尝试性的使用了一个登录界面,实现视频功能,但是并没有完全成功安卓端截图如下:当前这个页面是尝......
  • 贪吃蛇游戏
    importpygameimportrandom#初始化Pygame库pygame.init()#游戏窗口的大小window_width=800window_height=600#创建游戏窗口game_window=pygame.display.set_mode((window_width,window_height))#游戏窗口的标题pygame.display.set_caption('贪吃蛇游戏')#定义颜色......
  • csdn.net 的搜索功能
    在博客左边的文章搜索mupdf:结果都是一些不相干的讯息该好好改善一下搜索功能了。......
  • PB反编译器(PBKiller) 2.5.18 特别版
    PBKiller是一款非常优秀的PB反编译器,它可以反编译PB6.7.8.9编译出来的所有对象和源码。用PBkiller软件打开相应的PBD文件,显示如下,可以只导出需要的函数、窗口等,也可以全部导出。截图:http://www.mis2erp.com/soft/PBDE03.pnghttp://www.mis2erp.com/soft/PBDE04.pnghttp://www.mis2e......
  • 基于C++的AI贪吃蛇
    访问【WRITE-BUG数字空间】_[内附完整源码和文档]用C++做了个有AI功能的贪吃蛇小游戏,希望大家enjoyit.总体概况开发环境:VIsualStudio2017开发语言:C++和少许WindowsAPI运行环境:Windows1001初始化工作-游戏设置游戏设置和相关初始化放在了一个类里面,并进行了静态声明。主要设......