时间:2021-05-20
本文实例为大家分享了C++实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下
刚学完了C语言,便尝试的写了贪吃蛇的代码,但是效果不佳,很多的bug,所以,这个学了C++,便重新的写了这个小游戏,用类来封装!
先是头文件:
struct Snake{ int x, y;};class snake{public: snake() //构造函数 { length = 3; s[2].x = 10; s[2].y = 10; s[1].x = 9; s[1].y = 10; s[0].x = 8; s[0].y = 10; up = right = left = down = 0; } ~snake(){} void display(); //显示蛇身函数 void Rightmove(); //右移函数 void Leftmove(); //左移函数 void Upmove(); //上移函数 void Downmove(); //下移函数 int cheak(); //检查是否撞墙或撞到自身 void creat_food(); //产生食物 int eat_food(); //吃食物private: struct Snake s[100]; //先定义蛇身最长100 int length; //当前蛇长度 int x3, y3; //食物坐标 int up, down, right, left; //蛇的状态,是上移还是下移或...}; void make_frame(); //打印框架的函数void show(); //游戏开始倒计时函数void gameover(); //游戏结束函数下面是各个函数的实现的cpp文件:
# include <iostream># include <Windows.h># include <time.h># include "snake.h"# define MaxLen 20# define MaxWen 30using namespace std;HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取句柄void gotoxy(HANDLE hOut, int x, int y) //输出位置的函数{ COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(hOut, pos);} void snake::display() //打印蛇身{ for (int i = length - 1; i >= 0; i--) { if (i == length - 1) //打印蛇头 { gotoxy(hOut, s[i].x, s[i].y); cout << char(15); } else //打印蛇身 { gotoxy(hOut, s[i].x, s[i].y); cout << '*'; } } gotoxy(hOut, 0, 22);}void snake::Rightmove() //右移{ right = 1; up = down = left = 0; int x1, x2, y1, y2; x1 = x2 = s[length - 1].x; y1 = y2 = s[length - 1].y; s[length - 1].x++; //蛇头x坐标自增 for (int i = length - 2; i >= 0; i--) //除了蛇头,其他的结点都等于它的上一个结点的坐标 { x2 = s[i].x; y2 = s[i].y; s[i].x = x1; s[i].y = y1; x1 = x2; y1 = y2; } gotoxy(hOut, x2, y2); //消除蛇移动遗留的 ‘*' cout << ' ';}void snake::Leftmove() //左移{ left = 1; right = up = down = 0; int x1, x2, y1, y2; x1 = x2 = s[length - 1].x; y1 = y2 = s[length - 1].y; s[length - 1].x--; //同上 for (int i = length - 2; i >= 0; i--) { x2 = s[i].x; y2 = s[i].y; s[i].x = x1; s[i].y = y1; x1 = x2; y1 = y2; } gotoxy(hOut, x2, y2); //同上 cout << ' ';}void snake::Downmove() //下移{ down = 1; right = up = left = 0; int x1, x2, y1, y2; x1 = x2 = s[length - 1].x; y1 = y2 = s[length - 1].y; s[length - 1].y++; //同上 for (int i = length - 2; i >= 0; i--) { x2 = s[i].x; y2 = s[i].y; s[i].x = x1; s[i].y = y1; x1 = x2; y1 = y2; } gotoxy(hOut, x2, y2); //同上 cout << ' ';}void snake::Upmove() //上移{ up = 1; down = right = left = 0; int x1, x2, y1, y2; x1 = x2 = s[length - 1].x; y1 = y2 = s[length - 1].y; s[length - 1].y--; //同上 for (int i = length - 2; i >= 0; i--) { x2 = s[i].x; y2 = s[i].y; s[i].x = x1; s[i].y = y1; x1 = x2; y1 = y2; } gotoxy(hOut, x2, y2); //同上 cout << ' ';}int snake::cheak(){ int flag = 0; for (int i = length - 2; i >= 0; i--) //是否撞到自身 { if (s[i].x == s[length - 1].x && s[i].y == s[length - 1].y) { flag = 1; //是,标识符为1 break; } } if (flag == 1 || (s[length - 1].x >= 30 + 1 || s[length - 1].x < 4) || (s[length - 1].y <= 1 || s[length - 1].y >= 20)) { return 0; //检测是否撞自身,或者撞墙 } else { return 1; }}void snake::creat_food() //产生食物坐标{xy: x3 = (rand() % (25)) + 3; y3 = (rand() % (17)) + 2; for (int i = length - 1; i >= 0; i--) //检查食物是否在蛇身上 { if (s[i].x == x3 && s[i].y == y3) //是就重新产生食物坐标 goto xy; } gotoxy(hOut, x3, y3); //显示食物 cout << '*';}int snake::eat_food(){ if (s[length - 1].x == x3 && s[length - 1].y == y3) //蛇头碰到食物 { if (up == 1) //如果蛇是在上移,增加一个结点,为蛇头的上一个结点 { s[length].x = x3; s[length].y = y3 - 1; } else if (down == 1) //同上 { s[length].x = x3; s[length].y = y3 + 1; } else if (right == 1) //同上 { s[length].x = x3 + 1; s[length].y = y3; } else if (left == 1) //同上 { s[length].x = x3 - 1; s[length].y = y3; } length++; //蛇长加1 return 1; } else return 0;}void make_frame() //打印框架函数{ cout << " 贪吃蛇游戏" << endl; gotoxy(hOut, 2, 1); cout << "╔"; for (int i = 4; i < 2 + MaxWen; i++) { gotoxy(hOut, i, 1); printf("="); } for (int i = 2; i < MaxLen; i++) { gotoxy(hOut, 2, i); printf("║"); } gotoxy(hOut, 2 + MaxWen, 1); printf("╗"); for (int i = 2; i < MaxLen; i++) { gotoxy(hOut, 2 + MaxWen, i); printf("║"); } gotoxy(hOut, 2, MaxLen); printf("╚"); gotoxy(hOut, 2 + MaxWen, MaxLen); printf("╝"); for (int i = 4; i < 2 + MaxWen; i++) { gotoxy(hOut, i, MaxLen); printf("="); }}void show() //显示操作方法和游戏开始倒计时{ gotoxy(hOut, 35, 5); cout << "↑:" << 'w'; gotoxy(hOut, 35, 6); cout << "←:" << 'a'; gotoxy(hOut, 35, 7); cout << "↓:" << 's'; gotoxy(hOut, 35, 8); cout << "→:" << 'd'; gotoxy(hOut, 16, 5); cout << '3'; Sleep(1000); gotoxy(hOut, 16, 5); cout << '2'; Sleep(1000); gotoxy(hOut, 16, 5); cout << '1'; Sleep(1000); gotoxy(hOut, 16, 5); cout << ' ';}void gameover() //游戏结束函数{ system("cls"); system("color 3B"); gotoxy(hOut, 14, 5); cout << " GAME OVER!"; gotoxy(hOut, 14, 6); cout << "PLAY AGAIN ? Y(yes) \ N(no)";}主函数的cpp文件:
下面是游戏的截图:
控制台的实现,不是很美观,主要是由于上下和左右的间隙不一样大,所以看起来不是很好看,但总体还是实现了贪吃蛇!
关于C++小游戏的更多精彩内容请点击专题: 《C++经典小游戏》 学习了解
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
我大概在一个多月前把自己上学期写的c代码的贪吃蛇游戏push到csdn上,并且说c风格的贪吃蛇写起来有些麻烦(贪吃蛇游戏的c语言实现),准备用面向对象的c++再
本文实例讲述了贪吃蛇游戏C++命令行版的实现代码,是非常经典的游戏。分享给大家供大家参考。具体实现方法如下:众所周知,贪吃蛇游戏是经典的计算机游戏。游戏描述如下
本文实例为大家分享了C++控制台循环链表实现贪吃蛇的具体代码,供大家参考,具体内容如下-stdafx.h为了简化程序定义一些宏和全局变量#ifndef__STD
游戏操作说明通过方向键控制贪吃蛇上下左右移动。贪吃蛇吃到食物之后会变长一个长度。游戏具体实现游戏难点是怎么模拟贪吃蛇的移动。如果只是一个方块的话显然很简单。但是
本文实例为大家分享了C语言链表实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下贪吃蛇设计思路:屏幕坐标:拓展功能:1.F1,F2控制加速减速空格暂停游戏Esc