C++贪吃蛇(c++贪吃蛇课题简述)
VS2013
自制
运行一下试试看
#include<windows.h>#include<conio.h>#include<time.h>#include<string>using namespace std;/*=============== all the structures ===============*/typedef struct Frame
{
COORD position[2];//位置
int flag;
}Frame;//帧/*=============== all the functions ===============*/void SetPos(COORD a)// set cursor 设置光标{
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, a);
}/* set cursor设置光标*/void SetPos(int i, int j){
COORD pos = { i, j };
SetPos(pos);
}void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}//把第y行,[x1, x2) 之间的坐标填充为 chvoid drawRow(int y, int x1, int x2, char ch){
SetPos(x1, y); for (int i = 0; i <= (x2 - x1); i++) cout << ch;
}//在a, b 纵坐标相同的前提下,把坐标 [a, b] 之间填充为 chvoid drawRow(COORD a, COORD b, char ch){ if (a.Y == b.Y)
drawRow(a.Y, a.X, b.X, ch); else
{
SetPos(0, 25); cout << "error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等";
system("pause");
}
}//把第x列,[y1, y2] 之间的坐标填充为 chvoid drawCol(int x, int y1, int y2, char ch){ int y = y1; while (y != y2 + 1)
{
SetPos(x, y); cout << ch;
y++;
}
}//在a, b 横坐标相同的前提下,把坐标 [a, b] 之间填充为 chvoid drawCol(COORD a, COORD b, char ch){ if (a.X == b.X)
drawCol(a.X, a.Y, b.Y, ch); else
{
SetPos(0, 25); cout << "error code 02:无法填充列,因为两个坐标的横坐标(y)不相等";
system("pause");
}
}//左上角坐标、右下角坐标、用row填充行、用col填充列void drawFrame(COORD a, COORD b, char row, char col){
drawRow(a.Y, a.X + 1, b.X - 1, row);
drawRow(b.Y, a.X + 1, b.X - 1, row);
drawCol(a.X, a.Y + 1, b.Y - 1, col);
drawCol(b.X, a.Y + 1, b.Y - 1, col);
}//点覆盖void drawFrame(COORD a, char r){
SetPos(a.X, a.Y); cout << r;
}void drawFrame(int x1, int y1, int x2, int y2, char row, char col){
COORD a = { x1, y1 };
COORD b = { x2, y2 };
drawFrame(a, b, row, col);
}void drawFrame(Frame frame, char row, char col){
COORD a = frame.position[0];
COORD b = frame.position[1];
drawFrame(a, b, row, col);
}void drawPlaying(){
drawFrame(0, 0, 48, 24, '=', '|');// draw map frame;
drawFrame(49, 0, 77, 24, '-', '|');// draw output frame
SetPos(52, 6); cout << "玩家1长度:";
SetPos(52, 8); cout << "玩家2长度:";
SetPos(52, 14); cout << "操作方式:";
SetPos(52, 16); cout << " 玩家1 上下左右";
SetPos(52, 17); cout << " 玩家2 a,s,d,w";
SetPos(52, 19); cout << " p 暂停游戏。";
SetPos(52, 20); cout << " esc 退出游戏。";
}//在[a, b)之间产生一个随机整数int random(int a, int b){ int c = (rand() % (a - b)) + a; return c;
}//在两个坐标包括的矩形框内随机产生一个坐标COORD random(COORD a, COORD b){ int x = random(a.X, b.X); int y = random(a.Y, b.Y);
COORD c = { x, y }; return c;
}//判断是否撞到蛇身或者墙壁 点对点bool judgeCoordSnake(COORD spot1, COORD spot2){ if (spot1.X == spot2.X) if (spot1.Y == spot2.Y) return true; if (spot1.X == 0 || spot1.X == 48 || spot1.Y == 0 || spot1.Y == 24) return true; return false;
}//判断是否撞到蛇身或者墙壁 头对身子bool judgeCoordSnake(COORD spot1, COORD *snake1, COORD *snake2){ for (int i = 1; i < 250; i++)
{ if (spot1.X == snake1[i].X) if (spot1.Y == snake1[i].Y) return false; if (spot1.X == snake2[i].X) if (spot1.Y == snake2[i].Y) return false; if (spot1.X == 0 || spot1.X == 48 || spot1.Y == 0 || spot1.Y == 24) return false;
} return true;
}bool judgeCoordSnakeFood(COORD spot1, COORD spot2, COORD spot3,int& a){ if (spot1.X == spot2.X) if (spot1.Y == spot2.Y)
{
a = 0; return true;
} if (spot1.X == spot3.X) if (spot1.Y == spot3.Y)
{
a = 1; return true;
} return false;
}//因为在判断时头部的问题所以单列出来
void printCoord(COORD a){ cout << "( " << a.X << " , " << a.Y << " )";
}int drawMenu(){
SetPos(30, 1); cout << "贪吃的大蟒蛇";
drawRow(3, 0, 79, '-');
drawRow(5, 0, 79, '-');
SetPos(28, 4); cout << "w 和 s 选择, k 确定";
SetPos(15, 11); cout << "开始游戏";
SetPos(15, 13); cout << "结束游戏";
drawRow(20, 0, 79, '-');
drawRow(22, 0, 79, '-');
SetPos(47, 11); cout << "本游戏为双人游戏:";
SetPos(51, 13); cout << "wasd和ijkl控制";
SetPos(24, 21); cout << "制作: 威微游戏制造厂厂长 唐宇威"; int j = 11;
SetPos(12, j); cout <<"->"; while (1)
{ if (_kbhit())
{ char x = _getch(); switch (x)
{ case 'w':
{ if (j == 13)
{
SetPos(12, j); cout << " ";
j = 11;
SetPos(12, j); cout << "->";
SetPos(51, 13); cout << " ";
SetPos(47, 11); cout << "准备好了吗?";
SetPos(51, 13); cout << "wasd和ijkl控制";
} break;
} case 's':
{ if (j == 11)
{
SetPos(12, j); cout << " ";
j = 13;
SetPos(12, j); cout << "->";
SetPos(51, 13); cout << " ";
SetPos(47, 11); cout << "结束游戏:";
SetPos(51, 13); cout << "要走了吗?";
} break;
} case 'k':
{ if (j == 11)
return 1; else exit(0);
}
}
}
}
}/*
DWORD WINAPI MusicFun(LPVOID lpParamte)
{
//DWORD OBJ;
sndPlaySound(TEXT("bgm.wav"), SND_FILENAME|SND_ASYNC);
return 0;
}
*//*================== the Game Class ==================*/class Game
{public:
COORD snake1[250];
COORD snake2[250];
COORD food[2]; int length1; int length2; int direction1;//蛇1的头 1上2下3左4右
int direction2;//蛇2的头 1上2下3左4右
string title; //初始化所有
void initFood(); COORD randomFood(); //初始化其中一个
//void initThisBullet( COORD );
//void initThisEnemy( Frame );
void Playing(); void Pause(); void judgeSnake(); void GameOver(); void snake1ChangeDirection(char x); void snake2ChangeDirection(char x); void drawSnake1(int direction); void drawSnake2(int direction); void drawSnake1ToNull(); void drawSnake2ToNull(); void drawFood(); void drawFoodToNull();
Game()
{
direction1 = 1,direction2 = 1;
snake1[0] = { 8, 15 }, snake1[1] = { 8, 16 }, snake1[2] = { 8, 17 };
snake2[0] = { 40, 15 }, snake2[1] = { 40, 16 }, snake2[2] = { 40, 17 };
length1 = 3, length2 = 3; for (int i = 3; i < 249; i++)
{
snake1[i] = { 0, 0 };
snake2[i] = { 0, 0 };
}
}
};//画第一条蛇void Game::drawSnake1(int direction)
{ for (int i = 0; i<240; i++)
{
SetPos(snake1[i]); if (i != 0) cout << "O"; else if (i == 0)
{ if (direction==1)
{ cout << "^";
} else if(direction == 2)
{ cout << "v";
} else if (direction == 3)
{ cout << "<";
} else if (direction == 4)
{ cout << ">";
}
}
}
}void Game::drawSnake2(int direction)
{ for (int i = 0; i<240; i++)
{
SetPos(snake2[i]); if (i != 0) cout << "X"; else if (i == 0)
{ if (direction == 1)
{ cout << "^";
} else if (direction == 2)
{ cout << "v";
} else if (direction == 3)
{ cout << "<";
} else if (direction == 4)
{ cout << ">";
}
}
}
}//把第一条蛇消失,行动时使用,只让蛇头蛇尾消失,这样不会一闪一闪void Game::drawSnake1ToNull()
{
SetPos(snake1[0]); cout << " ";
SetPos(snake1[length1]); cout << " ";
}void Game::drawSnake2ToNull()
{ /*for (int i = length2; i<240; i++)
{
SetPos(snake2[i]);
cout << " ";
}*/
SetPos(snake2[0]); cout << " ";
SetPos(snake2[length2]); cout << " ";
}//初始食物void Game::initFood()
{
COORD a = { 2, 2 };
COORD b = { 24,12 };
COORD c = { 25, 2 };
COORD d = { 47,14 };
food[0] = random(a, b);
food[1] = random(c, d);
}//随机食物,如果有墙需要修改。COORD Game::randomFood()
{
COORD food;
COORD a = { 2, 2 };
COORD b = { 47, 23 }; while (1)
{
food = random(a, b); if (judgeCoordSnake(food, snake1, snake2)) return food;
}
}//画出食物
void Game::drawFood()
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入学习交流群
639368839,我们一起学C/C++!
版权声明:
作者: FreeClashNode
链接: https://www.freeclashnode.com/news/article-3349.htm
来源: www.freeclashnode.com
文章版权归作者所有,未经允许请勿转载。
热门文章
- 11月2日免费节点分享|22.5M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 10月14日免费节点分享|22.5M/S,Clash节点/V2ray节点/Singbox节点/SSR节点|免费上网梯子每天更新
- 10月11日免费节点分享|18.6M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 10月13日免费节点分享|22.1M/S,V2ray节点/Clash节点/Singbox节点/Shadowrocket节点|免费上网梯子每天更新
- 10月25日免费节点分享|18.1M/S,SSR节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月9日免费节点分享|22.3M/S,Singbox节点/Clash节点/Shadowrocket节点/V2ray节点|免费上网梯子每天更新
- 10月12日免费节点分享|22.7M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月10日免费节点分享|18.3M/S,V2ray节点/Singbox节点/Clash节点/SSR节点|免费上网梯子每天更新
- 10月27日免费节点分享|20M/S,Shadowrocket节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 10月28日免费节点分享|22.8M/S,Singbox节点/SSR节点/V2ray节点/Clash节点|免费上网梯子每天更新
最新文章
- 当Vmess突然失效:一场技术与封锁的无声博弈
- 11月7日免费节点分享|22.9M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 梅林固件科学上网完全指南:从入门到精通,零成本解锁网络自由
- 11月6日免费节点分享|20.9M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 幸运破解器科学上网全解析:解锁数字世界的终极指南
- 11月5日免费节点分享|21.9M/S,Shadowrocket节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 11月4日免费节点分享|18M/S,Clash节点/Singbox节点/SSR节点/V2ray节点|免费上网梯子每天更新
- 穿越数字迷雾:深度解析V2Ray中转迟滞之谜与极速优化指南
- 跨越数字边界:科学上网的终极指南与深度思考
- 从连接到掌控:深入理解 Quantumult 的工作流程与网络管理哲学
- 11月2日免费节点分享|22.5M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 深度解析《荒野冲突》:征服与策略的终极对决
- 11月1日免费节点分享|20.3M/S,V2ray节点/Shadowrocket节点/Clash节点/Singbox节点|免费上网梯子每天更新
- 斐讯路由器刷机配置v2ray全攻略:打造安全高效的翻墙网络环境
- 10月31日免费节点分享|18.7M/S,Shadowrocket节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 谷歌浏览器科学上网扩展全攻略:从入门到精通
- 10月30日免费节点分享|20.4M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 苹果手机V2Ray完全指南:从零开始打造安全上网环境
- 10月29日免费节点分享|21.6M/S,SSR节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 探索数字世界的门户:电脑科学上网贴吧完全指南
- 10月28日免费节点分享|22.8M/S,Singbox节点/SSR节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 穿越数字边界:科学上网的全面指南与深度思考
- 10月27日免费节点分享|20M/S,Shadowrocket节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 数字世界的破壁之旅:科学上网全指南与实用工具深度解析
- 10月26日免费节点分享|21.5M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 跨越知识边界:浙大学子的全球学术网络探索指南
- 10月25日免费节点分享|18.1M/S,SSR节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月24日免费节点分享|20.5M/S,V2ray节点/Clash节点/Singbox节点/Shadowrocket节点|免费上网梯子每天更新
- 刘大猫科学上网全攻略:解锁全球网络的终极指南
- 10月23日免费节点分享|22.2M/S,Shadowrocket节点/Clash节点/Singbox节点/V2ray节点|免费上网梯子每天更新
- 全面解析Quantumult安卓版:功能、安装与配置教程
- 双重防护,畅享无界:AdGuard联手V2RayNG,打造极致安全的上网体验
- 10月22日免费节点分享|18.8M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 掌握Clash白名单设置:精准控制代理流量的艺术
- 10月21日免费节点分享|22M/S,Clash节点/Singbox节点/V2ray节点/SSR节点|免费上网梯子每天更新
- 突破网络边界:技术科学上网的终极安全指南
- 10月20日免费节点分享|22.9M/S,SSR节点/V2ray节点/Clash节点/Singbox节点|免费上网梯子每天更新
- Shadowrocket 选购指南:从入门到精通,找到你的专属代理神器
- 10月19日免费节点分享|19.3M/S,Singbox节点/V2ray节点/Clash节点/Shadowrocket节点|免费上网梯子每天更新
- 深入解读:V2Ray为何比SSR更安全?一场网络隐私的全方位较量
- 10月18日免费节点分享|20.6M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 深度指南:Shadowrocket从入门到精通——iOS用户的网络自由密钥
- 10月17日免费节点分享|18M/S,Clash节点/SSR节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 深入解析科学上网套:全方位使用指南与常见问题解答
- 10月16日免费节点分享|22M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月15日免费节点分享|21.2M/S,Clash节点/SSR节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 全局掌控:安卓设备上 Clash 全局模式完整设置指南与深度解析
- 解锁全球游戏体验:育碧平台科学上网全攻略与深度解析
- 10月14日免费节点分享|22.5M/S,Clash节点/V2ray节点/Singbox节点/SSR节点|免费上网梯子每天更新
- 华为Mate 10科学上网全攻略:突破限制,畅游全球网络
归档
- 2026-11 13
- 2026-10 61
- 2026-09 58
- 2026-08 63
- 2026-07 61
- 2026-06 55
- 2026-05 56
- 2026-04 51
- 2026-03 60
- 2026-02 52
- 2026-01 56
- 2025-12 59
- 2025-11 55
- 2025-10 56
- 2025-09 55
- 2025-08 49
- 2025-07 31
- 2025-06 30
- 2025-05 31
- 2025-04 30
- 2025-03 388
- 2025-02 360
- 2025-01 403
- 2024-12 403
- 2024-11 390
- 2024-10 403
- 2024-09 388
- 2024-08 402
- 2024-07 427
- 2024-06 442
- 2024-05 181
- 2024-04 33
- 2024-03 31
- 2024-02 29
- 2024-01 51
- 2023-12 52
- 2023-11 32
- 2023-10 32
- 2023-09 3