具体解析见贪吃蛇_代码+解析
这里只是把几个变量和函数复制了一遍,就不详细讲了
不过输入函数变化较大,代码如下
void getin(){
if(kbhit()!=0){
const int key=224; //方向键的读取 会返回两个int型数据 第一个是224
int tempin; //记录临时输入字符
while(kbhit()!=0){
tempin=getch(); //读取
if(tempin==key)
input2=getch();//读取方向键的第二个int型数据
else
input=tempin; //普通字符直接赋值
}
switch(input){
case 'W':case 'w':{
if(way!=DOWN)
way=UP;
break;
}
case 'S':case 's':{
if(way!=UP)
way=DOWN;
break;
}
case 'A':case 'a':{
if(way!=RIGHT)
way=LEFT;
break;
}
case 'D':case 'd':{
if(way!=LEFT)
way=RIGHT;
break;
}
}
switch(input2){
case 72:{ //上 224 72
if(way2!=DOWN)
way2=UP;
break;
}
case 80:{ //下 224 80
if(way2!=UP)
way2=DOWN;
break;
}
case 75:{ //左 224 75
if(way2!=RIGHT)
way2=LEFT;
break;
}
case 77:{ //右 224 75
if(way2!=LEFT)
way2=RIGHT;
break;
}
}
}
return;
}
完整代码
#include<windows.h>
#include<stdlib.h>
#include<fstream>
#include<stdio.h>
#include<conio.h>
#include<time.h>
using namespace std;
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define X 23
#define Y 40
#define MAXLEN 100
#define MINTIME 75
unsigned int snake[MAXLEN][2];
unsigned int len;
unsigned int last[2];
unsigned int snake2[MAXLEN][2];
unsigned int len2;
unsigned int last2[2];
unsigned int way;
unsigned int way2;
double wait;
int input;
int input2;
unsigned int food[2];
bool empty=true;
void color(int _color){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),_color);
return;
}
void gotoxy(int xx,int yy){
COORD position={yy*2,xx};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
return;
}
void init(){
system("title 贪吃蛇");
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
len=len2=4;
snake[0][0]=X>>1;
snake[0][1]=Y/3;
snake[1][0]=(X>>1)+1;
snake[1][1]=Y/3;
snake[2][0]=(X>>1)+2;
snake[2][1]=Y/3;
snake[3][0]=(X>>1)+3;
snake[3][1]=Y/3;
snake2[0][0]=X>>1;
snake2[0][1]=Y/3<<1;
snake2[1][0]=(X>>1)+1;
snake2[1][1]=Y/3<<1;
snake2[2][0]=(X>>1)+2;
snake2[2][1]=Y/3<<1;
snake2[3][0]=(X>>1)+3;
snake2[3][1]=Y/3<<1;
way=way2=UP;
wait=150.0;
return;
}
void drawmap(){
color(255);
for(unsigned int xx=0;xx<X;xx++){
gotoxy(xx,0);
printf(" ");
gotoxy(xx,Y-1);
printf(" ");
}
for(unsigned int yy=0;yy<Y;yy++){
gotoxy(0,yy);
printf(" ");
gotoxy(X-1,yy);
printf(" ");
}
return;
}
void drawsnake(){
color(255);
gotoxy(0,0);
printf(" ");
color(10);
gotoxy(snake[0][0],snake[0][1]);
printf("□");
gotoxy(snake[1][0],snake[1][1]);
printf("■");
return;
}
void drawsnake2(){
color(255);
gotoxy(0,0);
printf(" ");
color(14);
gotoxy(snake2[0][0],snake2[0][1]);
printf("□");
gotoxy(snake2[1][0],snake2[1][1]);
printf("■");
return;
}
void move(){
last[0]=snake[len-1][0];
last[1]=snake[len-1][1];
last2[0]=snake2[len2-1][0];
last2[1]=snake2[len2-1][1];
for(unsigned int tc=len-1;tc>0;tc--){
snake[tc][0]=snake[tc-1][0];
snake[tc][1]=snake[tc-1][1];
}
for(unsigned int tc=len2-1;tc>0;tc--){
snake2[tc][0]=snake2[tc-1][0];
snake2[tc][1]=snake2[tc-1][1];
}
switch(way){
case UP:{
snake[0][0]--;
break;
}
case DOWN:{
snake[0][0]++;
break;
}
case LEFT:{
snake[0][1]--;
break;
}
case RIGHT:{
snake[0][1]++;
break;
}
}
switch(way2){
case UP:{
snake2[0][0]--;
break;
}
case DOWN:{
snake2[0][0]++;
break;
}
case LEFT:{
snake2[0][1]--;
break;
}
case RIGHT:{
snake2[0][1]++;
break;
}
}
return;
}
void clt(){
color(0);
gotoxy(last[0],last[1]);
printf(" ");
return;
}
void clt2(){
color(0);
gotoxy(last2[0],last2[1]);
printf(" ");
return;
}
void getin(){
if(kbhit()!=0){
const int key=224;
int tempin;
while(kbhit()!=0){
tempin=getch();
if(tempin==key)
input2=getch();
else
input=tempin;
}
switch(input){
case 'W':case 'w':{
if(way!=DOWN)
way=UP;
break;
}
case 'S':case 's':{
if(way!=UP)
way=DOWN;
break;
}
case 'A':case 'a':{
if(way!=RIGHT)
way=LEFT;
break;
}
case 'D':case 'd':{
if(way!=LEFT)
way=RIGHT;
break;
}
}
switch(input2){
case 72:{
if(way2!=DOWN)
way2=UP;
break;
}
case 80:{
if(way2!=UP)
way2=DOWN;
break;
}
case 75:{
if(way2!=RIGHT)
way2=LEFT;
break;
}
case 77:{
if(way2!=LEFT)
way2=RIGHT;
break;
}
}
}
return;
}
void putfood(){
if(empty){
bool flag=true;
srand(time(NULL));
while(flag){
food[0]=rand()%(X-2)+1;
food[1]=rand()%(Y-2)+1;
flag=false;
for(unsigned int tc=0;tc<len;tc++)
if(snake[tc][0]==food[0]&&snake[tc][1]==food[1])
flag=true;
for(unsigned int tc=0;tc<len2;tc++)
if(snake2[tc][0]==food[0]&&snake2[tc][1]==food[1])
flag=true;
}
empty=false;
color(14);
gotoxy(food[0],food[1]);
printf("◆");
}
return;
}
void eatfood(){
if(snake[0][0]==food[0]&&snake[0][1]==food[1]){
empty=true;
if(wait>=MINTIME)
wait-=0.5;
if(len<MAXLEN)
len++;
}
if(snake2[0][0]==food[0]&&snake2[0][1]==food[1]){
empty=true;
if(wait>=MINTIME)
wait-=0.5;
if(len2<MAXLEN)
len2++;
}
return;
}
void gameover(){
bool over=false;
bool over2=false;
if(snake[0][0]==0||snake[0][0]==X-1||snake[0][1]==0||snake[0][1]==Y-1)
over=true;
if(snake2[0][0]==0||snake2[0][0]==X-1||snake2[0][1]==0||snake2[0][1]==Y-1)
over2=true;
for(int tc=1;tc<len;tc++)
if(snake[0][0]==snake[tc][0]&&snake[0][1]==snake[tc][1])
over=true;
for(int tc=0;tc<len2;tc++)
if(snake[0][0]==snake2[tc][0]&&snake[0][1]==snake2[tc][1])
over=true;
for(int tc=1;tc<len2;tc++)
if(snake2[0][0]==snake2[tc][0]&&snake2[0][1]==snake2[tc][1])
over2=true;
for(int tc=0;tc<len;tc++)
if(snake2[0][0]==snake[tc][0]&&snake2[0][1]==snake[tc][1])
over2=true;
if(over==true)
len=len/4.0*3;
if(over2==true)
len2=len2/4.0*3;
if(over==true||over2==true){
_sleep(1000);
system("cls");
if(len>len2)
printf("LEFT WIN!\n");
if(len<len2)
printf("RIGHT WIN!\n");
if(len==len2)
printf("ALL WIN!\n");
while(kbhit()!=0)
getch();
getch();
exit(0);
}
return;
}
int main(){
init();
drawmap();
_sleep(3000);
while(true){
clt();
clt2();
drawsnake();
drawsnake2();
putfood();
eatfood();
getin();
move();
gameover();
_sleep(int(wait));
}
return 0;
}