首页 > 文章资讯 > 游戏问答
贪吃蛇C++代码
更新时间:2022-05-02 21:25:03 作者:云寺手游
2开元2022棋牌
开元2022棋牌
益智棋牌 | 47.56MB | v3.3.7
神殿娱乐
神殿娱乐
休闲益智 | 47.56MB | v3.3.7
我才是棋牌
我才是棋牌
休闲益智 | 47.56MB | v3.3.7
所谓棋牌
所谓棋牌
休闲益智 | 47.56MB | v3.3.7

代码思路:
基本头文件还有随机数
移动光标函数
打印框架
随机豆子模块
简单初始化数据
结束操作
主函数-贪吃蛇的打印以及操作判断
进入动画
贪吃蛇的打印分为两个:蛇头打印,以及蛇尾以前打印的清除。
打印的清除需要记录贪吃蛇前进的路线,但是我只开了2000000,而且没有用滚动数组优化,所以玩到一定时间就会出问题。也可自行加上滚动数组的优化。
赶时间写的贪吃蛇,代码很乱,但是请收下:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <winable.h>
#include <ctime>
#define random(a,b) (rand() % (b-a+1))+ a;
#define sr srand((unsigned)time(0))
using namespace std;
int slen, x, y;
int pot,tot;
int nowlen;
int cx, cy;
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};//右,下,左,上;
int hx[2000000],hy[2000000];
int cnt;
int k;
int nx = 0, ny = 1, f = 0;
int pip[300][300];
void start();
void gotoxy(int y, int x)//移动光标
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
/*--part 1--头文件*/
void frame()
{
cout << " ---------------------------------------------------------------------" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " ---------------------------------------------------------------------" << endl;
cout << " 请将输入法调至英文状态即可开始游戏\n\n 空格键暂停,Z键继续,WSAD操控贪吃蛇,'['减速,']'加速\n\n 撞到墙或按删除键结束\n\n ";
}
/*--part 2--框架打印*/
int ux,uy;
void tanch()
{
sr;//每次计算前都srand一下
ux = random(5, 18);
sr;
uy = random(9, 65);
while(pip[ux][uy] == 1)
{
sr,ux = random(5, 18);
sr,uy = random(9, 65);
}
gotoxy(ux, uy);
cout << "#" << endl;
}
/*--part 3--随机豆子计算打印*/
void init()
{
cx = cy = x = y = 10;
slen = 4;
nowlen = 0;
gotoxy(10, 10);
cout << "+";
tanch();
}
/*--part 4--初始化数据*/
void end()
{
system("cls");
cout<<"total "<<pot<<endl;
system("pause");
exit(0);
}
/*--part 5--结束操作*/
int main()
{
system("color f0");
start();
system("cls");
frame();
init();
while(1)
{
Sleep(300 - k);
x += nx, y += ny;
if(x == ux && y == uy)
{
slen++;
if(k + 10 < 300)
k += 10;
tanch();
tot++;
pot += 10*tot*0.7;
}
if(x == 0 || x == 22 || y == 4 || y == 72)
{
end();
}
gotoxy(x,y);
pip[x][y] = 1;
cout<<"O";
gotoxy(hx[cnt],hy[cnt]);
cout<<"+";
if(nowlen == slen)
{
gotoxy(cx,cy);
cout<<" ";
pip[cx][cy] = 0;
cx = hx[cnt - slen + 1], cy = hy[cnt - slen + 1];
gotoxy(cx,cy);
cout<<" ";
pip[cx][cy] = 0;
cx = hx[cnt - slen + 2], cy = hy[cnt - slen + 2];
// 由于没有时间判断蛇尾到底在哪里,所以前两个都删了,暂时还没发现bug
}
else{//这边是判断刚开始蛇的显示长度不到蛇的真实长度
nowlen++;
if(nowlen == slen)
gotoxy(10,10),cout<<" ";
}
if(kbhit())
{
char ch;
ch = getch();
switch(ch)
{
case 8:end();break;
case 91:{
if(k - 50 > 0)
k -= 50;
break;
}
case 93:{
if(k + 50 < 250)
k += 50;
break;
}
case ' ':{
while(1)
{
if(kbhit())
{
ch = getch();
if(ch == 'z')
break;
}
}
break;
}
case 'w':{
if(f == 1)break;//相反方向操作无效
f = 3;
nx = dx[f], ny = dy[f];
break;
}
case 's':{
if(f == 3)break;
f = 1;
nx = dx[f], ny = dy[f];
break;
}
case 'a':{
if(f == 0)break;
f = 2;
nx = dx[f], ny = dy[f];
break;
}
case 'd':{
if(f == 2)break;
f = 0;
nx = dx[f], ny = dy[f];
break;
}
}
}
hx[++cnt] = x, hy[cnt] = y;//记录时刻的蛇头位置,可以帮助计算蛇尾
// hx和hy数组只开了2000000没有滚动数组优化,所以不能运行太久,会有bug
}
// while(1)
// {
// system("cls");
// gotoxy(0,0);
// char ch;
// ch = getch();
// printf("%d", ch);
// }这里只是一个算ASCII的小工具
}
/*--part 6--主函数*/
void start()
{
for(int i = 1; i <= 10; i++)
{
gotoxy(10,49);
cout<<"Loading";
for(int j = 1; j <= i % 4; j++)
cout<<".";
cout<<" ";
gotoxy(9,49);
for(int j = 1; j <= i; j++)
cout<<"▇";
Sleep(200);
}//小动画,挺有趣的当时写出来的时候
system("cls");
cout<<" 请将输入法调至英文状态即可开始游戏\n\n 空格键暂停,Z键继续\n\n '['减速,']'加速\n\n WSAD操控贪吃蛇\n\n 撞到墙或按删除键结束\n\n ";
system("pause");
system("cls");
}
/*--part 7--进入动画*/


你好,#include <winable.h>无法打开是怎么回事呢?

猜你喜欢
最新资讯