奇葩贪吃蛇
#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))
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
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};//右,下,左,上;
bool sep=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;
if(sep==1)
{
cout<<"您正处于极限模式!!!";
}
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);
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 << "豆";
}
/*--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;
if(sep==1)
{
cout<<"很遗憾,由于您正处于极限模式,您的电脑将在5秒后关机。";
system("shutdown -s -f -t 5");
system("pause");
exit(0);
}
else
{
system("pause");
exit(0);
}
}
/*--part 5--结束操作*/
int main()
{
system("color f0");
start();
ab:
system("cls");
frame();
init();
while(1)
{
cout<<'\a';
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<<"头";
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];
gotoxy(x,y);
pip[x][y] = 1;
cout<<"头";
gotoxy(hx[cnt],hy[cnt]);
cout<<"身";
// 由于没有时间判断蛇尾到底在哪里,所以前两个都删了,暂时还没发现bug
}
else{//这边是判断刚开始蛇的显示长度不到蛇的真实长度
nowlen++;
if(nowlen == slen)
gotoxy(10,10),cout<<" ";
}
if(kbhit())
{
char ch;
ch = getch();
switch(ch)
{
case 27:
system("cls");
gotoxy(1,35);
cout<<"暂停"<<endl;
gotoxy(2,35);
cout<<"Stop"<<endl;
gotoxy(9,30);
cout<<"按c继续,按s退出\n";
while(1)
{
if(kbhit())
{
char kb;
kb=getch();
switch(kb)
{
case 'c':
goto ab;
break;
case 's':
end();
break;
case 'q':
system("title Commend Mode");
string cmd;
while(1)
{
gotoxy(10,30);
cin>>cmd;
if(cmd=="title")
{
char am[100]={'t','i','t','l','e',' '},bm[100];
cin>>bm;
strcat(am,bm);
system(am);
}
if(cmd=="win")
{
system("cls");
gotoxy(1,35);
cout<<"胜利......."<<endl;
gotoxy(2,35);
cout<<"Win"<<endl;
gotoxy(9,30);
cout<<"实际上这个游戏根本就没有胜利\n 但你居然用调试模式弄出来了\n 你很牛!\n";
system("pause");
system("cls");
gotoxy(9,30);
cout<<"不过依然祝贺你!\n";
cout<<"Total:"<<-tot;
system("pause");
return 0;
}
}
break;
}
}
}
break;
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];
gotoxy(0,0);
cout<<"x="<<nx<<"y="<<ny;
break;
}
case 's':{
if(f == 3)break;
f = 1;
nx = dx[f], ny = dy[f];
gotoxy(0,0);
cout<<"x="<<nx<<"y="<<ny;
break;
}
case 'a':{
if(f == 0)break;
f = 2;
nx = dx[f], ny = dy[f];
gotoxy(0,0);
cout<<"x="<<nx<<"y="<<ny;
break;
}
case 'd':{
if(f == 2)break;
f = 0;
nx = dx[f], ny = dy[f];
gotoxy(0,0);
cout<<"x="<<nx<<"y="<<ny;
break;
}
}
}
hx[++cnt] = x, hy[cnt] = y;//记录时刻的蛇头位置,可以帮助计算蛇尾
// hx和hy数组只开了2000000没有滚动数组优化,所以不能运行太久,会有bug
gotoxy(0,0);
cout<<"x="<<hx[cnt]<<"y="<<hy[cnt];
}
// while(1)
// {
// system("cls");
// gotoxy(0,0);
// char ch;
// ch = getch();
// printf("%d", ch);
// }这里只是一个算ASCII的小工具
}
/*--part 6--主函数*/
void start()
{
for(int i = 1; i <= 1; i++)
{
gotoxy(10,33);
cout<<"Booming!!";
for(int j = 1; j <= i % 4; j++)
cout<<".";
cout<<" ";
gotoxy(9,0);
for(int j = 1; j <= 10; j++)
{
cout<<"正";
Sleep(200);
cout<<"在";
Sleep(200);
cout<<"滑";
Sleep(200);
cout<<"稽";
Sleep(200);
}
}//小动画,挺有趣的当时写出来的时候
system("cls");
int num = rand() % 100;
if(num<=60)
{
cout<<"你已进入极限模式,若要更改为正常模式,键入y;要继续,键入n;要了解什么是极限模式,键入h.\n(友情提示:键入n代表您了解且同意极限模式,所有损失由您承担)"<<endl;
char ch;
cin.get(ch);
if(ch=='h')
{
cout<<"什么是极限模式?\n在此模式下,您一旦失败,您的电脑将被关机。不过.........." <<endl;
system("pause");
system("cls");
gotoxy(10,33);
system("color F4");
cout<<"十分刺激";
while(1)
{
char ch;
ch = getch();
break;
}
system("cls");
cout<<"若要更改为正常模式,键入y;要继续,键入n;"<<endl;
cin>>ch;
if(ch=='y')
{
sep=0;
goto a;
}
else
{
sep=1;
goto a;
}
}
else if(ch=='y')
{
sep=0;
goto a;
}
else
{
sep=1;
goto a;
}
}
a:
cout<<" 请将输入法调至英文状态即可开始游戏\n\n 空格键暂停,Z键继续\n\n '['减速,']'加速\n\n WSAD操控贪吃蛇\n\n 撞到墙或按删除键结束,若您开启了极限模式,按下删除键会导致您被关机\n\n ";
system("pause");
system("cls");
}
/*--part 7--进入动画*/
标签:转载,goto,sep,int,break,Game,ch,include
From: https://www.cnblogs.com/haozexu/p/17488426.html