【历史遗留】存一下中学时期玩的贪吃蛇cpp源代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <climits>
#include <string>
#include <string.h>
#include <conio.h>
#include<windows.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
void gotoxy(int x, int y)
{
COORD pos = {x*2,y};
SetConsoleCursorPosition(hConsole, pos);
}
void SetColor(int a,int b=0)
{
int color=(a%=16)+(b%=16)*16;
SetConsoleTextAttribute(hConsole, color);
}
#define Func_ inline void
#define rnt register int
#define L_ 20
#define W_ L_
#define Boom_ -140
#define Apple_ -41
#define Speed_min_ 5
#define Speed_max_ 155
void B_out(char a[],int ms=7)
{
int a_len=strlen(a);
for(int i=0;i<a_len;i++)
{
Sleep(ms);
printf("%c",a[i]);
}
}
void B_load(int ms=1000,int xxx=50,char w='|',char f='>')
{
int a=0;
if(xxx)ms/=xxx;
else
{
ms/=100;
for(int i=0;i<=100;i++)
{
printf("[%3i%c]",i,37);
Sleep(ms);
printf("\b\b\b\b\b\b");
}
return;
}
for(int i=0;i<xxx;i++)
{
printf("%c",w);
Sleep(3);
}
printf("<");
B_out(" 0%",10);
for(int i=0;i<xxx;i++)
{
Sleep(ms);
printf("\b\b\b\b");
printf("%3i%c",++a,37);
for(int j=i;j<xxx+5;j++)
printf("\b");
printf("%c",f);
for(int j=i+1;j<xxx;j++)
printf("%c",w);
for(int k=1;k<100/xxx;k++)
{
Sleep(ms);
printf("<%3i%c",++a,37);
}
}
printf("\n\n");
}
typedef unsigned int unt;
char cc,ccc;
unt i,j,k,
x=W_/2,y=W_/2,
z,
l=1,
em=W_*L_-1,
app,
aa,bb,a1,b1,
tt,ttt=1,
tm,
score,
level,
faster=100,
tim;
short Shepi='1',
map[W_][L_],
SC[400];
bool guang;
Func_ Put_apple()
{
aa=(aa+clock())%W_+1;
a1=(a1+clock())%(W_-2)+2;
bb=(bb+clock())%L_+1;
b1=(b1+clock())%(L_-2)+2;
if(clock()-tt<ttt)
return;
if(clock()%17>3)
{
if(map[aa][bb]>0)
return;
if(map[aa][bb]!=Apple_)app++;
map[aa][bb]=Apple_;
gotoxy(aa,bb);
SetColor(rand()%5+10);
if(clock()%3)
printf("¤");
else
printf("◎");
}
else
{
if(map[a1][b1]!=0||abs((long)(a1-x))<3||abs((long)(b1-y))<3)
return;
map[a1][b1]=Boom_;
gotoxy(a1,b1);
SetColor(0,15);
printf("■");
}
tt=clock();
em--;
}
Func_ Eat_apple()
{
map[x][y]=l;
tim++;app--;
if(tim==l)
l++,tim=0;
em++;
}
Func_ Build_map()
{
Sleep(100);
SetColor(14);
B_out("┌");
for(i=1;i<=W_;i++)
B_out("┈┈");
B_out("┐");
for(i=1;i<=L_;i++)
gotoxy(0,i),
B_out("┊"),
gotoxy(W_+1,i),
B_out("┊");
B_out("\n╰");
for(i=1;i<=W_;i++)
B_out("┈┈");
B_out("╯");
Eat_apple();
return;
}
int main();
Func_ Game_over()
{
gotoxy(W_+8,2);
SetColor(12);
B_out("Game Over !");
SetColor(7);
gotoxy(W_+2,L_+1);
B_out("Saving Data . . .\n",20);
B_load();
gotoxy(W_,L_+2);
B_out("\nOne more game ? Y/N \n",20);
while(ccc=getch())
{
switch(ccc)
{
case 'Y':case 'y':
for(i=1;i<=W_;i++)
for(j=1;j<=L_;j++)
map[i][j]=0;system("cls");system("C#贪吃蛇.exe");
case 'N':case 'n':exit(0);
default :continue;
}
}
}
Func_ Map_print()
{
for(i=1;i<=W_;i++)
for(j=1;j<=L_;j++)
if(map[i][j]>0)
{
gotoxy(i,j);
map[i][j]--;
switch(Shepi)
{
case '0': SetColor(map[i][j]%14+(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang); break;
case '1': SetColor(15*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang); break;
case '2': SetColor( 8*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang); break;
case '3': SetColor( 9*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang); break;
case '4': SetColor(10*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang); break;
case '5': SetColor(11*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang); break;
case '6': SetColor(12*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang); break;
case '7': SetColor( 7*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang); break;
case '8': SetColor(14*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang); break;
case '9': SetColor( 6*(bool)(map[i][j]>0),rand()*(bool)(map[i][j]>0)*guang); break;
}
printf("■");
}
gotoxy(x,y);
switch(Shepi)
{
case '0': SetColor(15,rand()*(bool)(map[i][j]>0)*guang); break;
case '1': SetColor(15,rand()*(bool)(map[i][j]>0)*guang); break;
case '2': SetColor( 8,rand()*(bool)(map[i][j]>0)*guang); break;
case '3': SetColor( 9,rand()*(bool)(map[i][j]>0)*guang); break;
case '4': SetColor(10,rand()*(bool)(map[i][j]>0)*guang); break;
case '5': SetColor(11,rand()*(bool)(map[i][j]>0)*guang); break;
case '6': SetColor(12,rand()*(bool)(map[i][j]>0)*guang); break;
case '7': SetColor( 7,rand()*(bool)(map[i][j]>0)*guang); break;
case '8': SetColor(14,rand()*(bool)(map[i][j]>0)*guang); break;
case '9': SetColor( 6,rand()*(bool)(map[i][j]>0)*guang); break;
}
printf("◆");
gotoxy(W_+4,L_/5);
SetColor(11);
printf("Score : %05i",score*5);
gotoxy(W_+4,L_/5*2);
SetColor(7);
printf("Speed : %05i",(Speed_max_-faster)*200);
gotoxy(W_+4,L_/5*3);
SetColor(6);
printf("Lenth : %05i",l-1);
gotoxy(W_+4,L_/5*4);
SetColor(5);
printf("Times : %05i",clock()/1000);
map[x][y]=l;
}
Func_ Wait_move()
{
if(clock()-tm<faster)
return;
tm=clock();
if(kbhit())
{cc=getch();
if(cc==ccc)
faster=(faster-5)<Speed_min_?Speed_min_:faster-5;
if(cc>='0'&&cc<='9')
Shepi=cc;
if(cc=='+')
Shepi+=Shepi<'9';
if(cc=='-')
Shepi-=Shepi>'0';
if(cc==' ')
guang=!guang;
}
if(
((cc=='w'||cc=='W')&&(ccc=='s'||ccc=='S'))||
((cc=='s'||cc=='S')&&(ccc=='w'||ccc=='W'))||
((cc=='a'||cc=='A')&&(ccc=='d'||ccc=='D'))||
((cc=='d'||cc=='D')&&(ccc=='a'||ccc=='A'))
)
{
cc=ccc;
faster=(faster+5)>Speed_max_?Speed_max_:faster+5;
}
switch(cc)
{
case 'w':case 'W':
case 'a':case 'A':
case 's':case 'S':
case 'd':case 'D':
ccc=cc;
default :
cc=ccc;
switch(cc)
{
case 'w':case 'W':
y=y>1?y-1:L_;
break;
case 'a':case 'A':
x=x>1?x-1:W_;
break;
case 's':case 'S':
y=y<L_?y+1:1;
break;
case 'd':case 'D':
x=x<W_?x+1:1;
break;
}
break;
}
if((map[x][y]==Boom_||map[x][y]>0)&&cc)
Game_over();
if(map[x][y]==Apple_)
Eat_apple(),score++;
Map_print();
}
int main()
{
i=j=k=0,
x=W_/2,y=W_/2,
l=1,
em=W_*L_-1,
app=0,
tt=0,ttt=1000,
tm=0,
score=0,
level=0,
faster=100,
tim=0;
cc=0;
system("title C#Snake.Game% tech_by_041");
Build_map();
Wait_move();
Put_apple();
while(!kbhit());
do{
Wait_move();
Put_apple();
}while(true);
return 0;
}