貪食蛇
老師年輕時的作品(淚 稍微玩過就可以了,不需要現在專研
//貪食蛇
#include<iostream>
#include<windows.h>
#include<conio.h>
using namespace std;
void gotoxy(int x,int y)
{
SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE),
(COORD) {2*x , y });
}
void SetColor(unsigned short ForeColor=4,unsigned short BackGroundColor=0)
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,ForeColor|BackGroundColor);
};
class Node
{
public:
int data_x;
int data_y;
Node(int,int);
Node *next;
Node *pre;
friend class Snake;
};
Node::Node(int x,int y):data_x(x),data_y(y)
{}
class Snake
{
private:
Node *front;
Node *rear;
Node *ptr;
public:
int x,y;
int BODY_LONG;
int map[25][25];
int map1[25][25];
int map2[25][25];
int map3[25][25];
int alive,c,d,speed,body,stage,score,select,e;
char direction;
Snake();
void StageOutput(int);
void Move(int,int);
int Game();
void Start();
void CreateEgg();
void GameOver();
void initial();
void StageClear(int);
friend class Node;
};
Snake::Snake():alive(1),x(12),y(12),direction('N'),BODY_LONG(4)
{
front=new Node(12,12);
rear=front;
rear->next=NULL;
rear->pre=NULL;
for(int i=0;i<25;i++)
{
for(int j=0;j<25;j++)
{
if(i==0||i==24||j==0||j==24)
map1[i][j]=1;
else
map1[i][j]=0;
}
}
for(int i=0;i<25;i++)
{
for(int j=0;j<25;j++)
{
if(i==0||i==24||j==0||j==24)
map2[i][j]=1;
else
map2[i][j]=0;
}
}
for(int a=6;a<19;a++)
if(a<=9||a>=15)
{
map2[6][a]=1;
map2[a][6]=1;
}
for(int a=6;a<19;a++)
if(a<=9||a>=15)
{
map2[18][a]=1;
map2[a][18]=1;
}
for(int i=0;i<25;i++)
{
for(int j=0;j<25;j++)
{
if(i==0||i==24||j==0||j==24)
map3[i][j]=1;
else
map3[i][j]=0;
}
}
for(int a=3;a<9;a++)
{
map3[a][9]=1;
map3[a][15]=1;
}
for(int a=16;a<23;a++)
{
map3[a][7]=1;
map3[a][17]=1;
}
for(int a=7;a<18;a++)
map3[15][a]=1;
map3[10][6]=1;
map3[10][7]=1;
map3[9][8]=1;
map3[9][15]=1;
map3[10][15]=1;
map3[10][16]=1;
map3[10][17]=1;
map3[10][18]=1;
}
void Snake::Move(int x,int y)
{
//新增頭部
ptr=new Node(x,y);
rear->next=ptr;
ptr->pre=rear;
rear=ptr;
rear->next=NULL;
//計算BODY_LONG
int i=0;
ptr=front;
while(ptr!=NULL)
{
ptr=ptr->next;
i++;
}
//消去尾部
if(i>BODY_LONG)//如果未超過身長就不消去尾巴
{
gotoxy(front->data_x,front->data_y);
cout<<" ";
map[front->data_y][front->data_x]=0;
ptr=front;
front=front->next;
delete ptr;
front->pre=NULL;
}
gotoxy(rear->data_x,rear->data_y);
SetColor(10,0);
cout<<"●";
}
int Snake::Game()
{
direction='N';
while(1)
{
switch(getch())
{
case 72:
direction='U';//up
break;
case 80:
direction='D';//down
break;
case 75:
direction='L';//left
break;
case 77:
direction='R';//right
break;
}
if(direction=='N')
continue;
break;
}
CreateEgg();
while(1)
{
map[y][x]=1;
if(kbhit())
{
c=getch();
if(c==224)
{
d=getch();
switch(d)
{
case 72: if(direction=='D')
break;
direction='U';//up
break;
case 80: if(direction=='U')
break;
direction='D';//down
break;
case 75: if(direction=='R')
break;
direction='L';//left
break;
case 77: if(direction=='L')
break;
direction='R';//right
break;
}
}
}
Sleep(speed);
switch(direction)
{
case 'U': Move(x,--y);
break;
case 'D': Move(x,++y);
break;
case 'L': Move(--x,y);
break;
case 'R': Move(++x,y);
break;
}
if(map[y][x]==3)//吃到蛋
{
BODY_LONG=BODY_LONG+body;
score=score+10;
SetColor(15,0);
gotoxy(28,5);
cout<<"Score: "<<score<<" / 100";
gotoxy(28,7);
cout<<"Body Length: "<<BODY_LONG;
if(score==100)
{
StageClear(stage++);
return 0;
}
CreateEgg();
}
if(map[y][x]==1)//撞到東西
{
GameOver();
alive=0;
return 0;
}
}
}
void Snake::StageOutput(int stage)
{
//讀取關卡
if(stage==1)
for(int i=0;i<25;i++)
{
for(int j=0;j<25;j++)
{
map[i][j]=map1[i][j];
}
}
else
if(stage==2)
for(int i=0;i<25;i++)
{
for(int j=0;j<25;j++)
{
map[i][j]=map2[i][j];
}
}
else
for(int i=0;i<25;i++)
{
for(int j=0;j<25;j++)
{
map[i][j]=map3[i][j];
}
}
//印出關卡
system("cls");
SetColor(12,0);
for(int i=0;i<25;i++)
{
for(int j=0;j<25;j++)
{
if(map[i][j]==1)
cout<<"■";
else
cout<<" ";
}
if(i<24)cout<<endl;
}
gotoxy(28,3);
cout<<"Stage: "<<stage;
gotoxy(28,5);
SetColor(15,0);
cout<<"Score: 0 / 100";
gotoxy(28,7);
cout<<"Body Length: "<<4;
gotoxy(12,12);
SetColor(10,0);
cout<<"●";
}
//生蛋函式
void Snake::CreateEgg()
{
int a,b;
while(1)
{
a=rand()%25;
b=rand()%25;
if(map[b][a]>0||(b==front->data_y&&a==front->data_x))
continue;
map[b][a]=3;
gotoxy(a,b);
SetColor(14,0);
cout<<"★";
break;
}
}
//遊戲結束函式
void Snake::GameOver()
{
ptr=rear;
while(ptr!=NULL)
{
Sleep(50);
gotoxy(ptr->data_x,ptr->data_y);
cout<<"○";
ptr=ptr->pre;
}
gotoxy(27,10);
SetColor(14,0);
cout<<" ⊕ GAME OVER ⊕";
gotoxy(27,12);
cout<<"請按任意鍵回到選單";
getch();
system("cls");
}
//初始函式
void Snake::initial()
{
x=12;
y=12;
score=0;
BODY_LONG=4;
alive=1;
Node *x;
ptr=front;
while(ptr!=NULL)
{
x=ptr;
ptr=ptr->next;
delete x;
}
front=new Node(12,12);
rear=front;
rear->next=NULL;
rear->pre=NULL;
}
//關卡結束函式
void Snake::StageClear(int s)
{
gotoxy(27,10);
SetColor(14,0);
cout<<" ⊕ YOU WIN ⊕";
cout<<s;
if(s<3)
{ cout<<"a";
gotoxy(27,12);
cout<<" 請按任意鍵繼續接關";
getch();
system("cls");
}
else
{
gotoxy(27,12);
cout<<"恭喜你已破了所有關卡";
gotoxy(27,14);
cout<<" 請按任意鍵回到選單";
getch();
system("cls");
}
}
int main()
{
//srand(time(NULL));
Snake s;
while(1)
{
s.Start();
while(1)
{
s.initial();
s.StageOutput(s.stage);
while(s.Game())
continue;
if(s.stage==4||s.alive==0)
break;
}
}
}
void Snake::Start()
{
select=0;
stage=1;
SetColor(12,0);
cout<<endl;
cout<<" ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■"<<endl;
cout<<endl;
SetColor(14,0);
cout<<" ■■■ ■ ■ ■ ■ ■ ■■"<<endl;
cout<<" ■ ■■ ■ ■ ■ ■ ■ ■ ■"<<endl;
cout<<" ■■■ ■ ■ ■ ■ ■■ ■■■■"<<endl;
cout<<" ■ ■ ■ ■ ■ ■ ■ ■"<<endl;
cout<<" ■■■ ■ ■ ■ ■ ■ ■ ■■"<<endl;
cout<<endl;
SetColor(12,0);
cout<<" ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■"<<endl;
cout<<endl<<endl;
cout<<" 〒 按 SPACE 來 決 定 〒"<<endl;
cout<<endl;
SetColor(14,0);
cout<<" ★ 直 接 開 始 ★"<<endl;
cout<<endl;
SetColor(15,0);
cout<<" 選 擇 關 卡"<<endl;
while(1)
{
e=getch();
if(e==32)
break;
if(e==80||e==72)
{
if(select==1)
{
select=0;
gotoxy(15,14);
SetColor(14,0);
cout<<"★ 直 接 開 始 ★";
gotoxy(15,16);
SetColor(15,0);
cout<<" 選 擇 關 卡 ";
}
else
{
select=1;
gotoxy(15,14);
SetColor(15,0);
cout<<" 直 接 開 始 ";
gotoxy(15,16);
SetColor(14,0);
cout<<"★ 選 擇 關 卡 ★";
}
}
}
gotoxy(15,14);
cout<<" ";
gotoxy(15,16);
cout<<" ";
//選擇關卡
if(select==1)
{
gotoxy(17,14);
cout<<"請選擇關卡";
gotoxy(15,16);
SetColor(14,0);
cout<<" 【1】";
SetColor(15,0);
cout<<" 2 3";
while(1)
{
e=getch();
if(e==32)
break;
if(stage==1)
{
if(e==77)
{
gotoxy(15,16);
SetColor(15,0);
cout<<" 1";
SetColor(14,0);
cout<<" 【2】";
SetColor(15,0);
cout<<" 3"<<endl;
stage=2;
}
}
else
if(stage==2)
{
if(e==77)
{
gotoxy(15,16);
SetColor(15,0);
cout<<" 1 2";
SetColor(14,0);
cout<<" 【3】"<<endl;
stage=3;
}
if(e==75)
{
gotoxy(15,16);
SetColor(14,0);
cout<<" 【1】";
SetColor(15,0);
cout<<" 2 3"<<endl;
stage=1;
}
}
else
if(stage==3)
{
if(e==75)
{
gotoxy(15,16);
SetColor(15,0);
cout<<" 1";
SetColor(14,0);
cout<<" 【2】";
SetColor(15,0);
cout<<" 3"<<endl;
stage=2;
}
}
}
}
//選擇速度
gotoxy(16,14);
SetColor(14,0);
cout<<"請選擇蛇的速度";
select=1;
gotoxy(14,16);
SetColor(15,0);
cout<<" SLOW";
SetColor(14,0);
cout<<" NORMAL";
SetColor(15,0);
cout<<" FAST"<<endl;
while(1)
{
e=getch();
if(e==32)
break;
if(select==0)
{
if(e==77)
{
gotoxy(14,16);
SetColor(15,0);
cout<<" SLOW";
SetColor(14,0);
cout<<" NORMAL";
SetColor(15,0);
cout<<" FAST"<<endl;
select=1;
}
}
else
if(select==1)
{
if(e==77)
{
gotoxy(14,16);
SetColor(15,0);
cout<<" SLOW NORMAL";
SetColor(14,0);
cout<<" FAST"<<endl;
select=2;
}
if(e==75)
{
gotoxy(14,16);
SetColor(14,0);
cout<<" SLOW";
SetColor(15,0);
cout<<" NORMAL FAST"<<endl;
select=0;
}
}
else
if(select==2)
if(e==75)
{
gotoxy(14,16);
SetColor(15,0);
cout<<" SLOW";
SetColor(14,0);
cout<<" NORMAL";
SetColor(15,0);
cout<<" FAST"<<endl;
select=1;
}
}
if(select==0)
speed=150;
else
if(select==1)
speed=100;
else
speed=50;
//選擇長度
gotoxy(16,18);
SetColor(14,0);
cout<<"請選擇蛇的長度";
select=1;
gotoxy(14,20);
SetColor(15,0);
cout<<"SHORT";
SetColor(14,0);
cout<<" NORMAL";
SetColor(15,0);
cout<<" LONG"<<endl;
while(1)
{
e=getch();
if(e==32)
break;
if(select==0)
{
if(e==77)
{
gotoxy(14,20);
SetColor(15,0);
cout<<"SHORT";
SetColor(14,0);
cout<<" NORMAL";
SetColor(15,0);
cout<<" LONG"<<endl;
select=1;
}
}
else
if(select==1)
{
if(e==77)
{
gotoxy(14,20);
SetColor(15,0);
cout<<"SHORT NORMAL";
SetColor(14,0);
cout<<" LONG"<<endl;
select=2;
}
if(e==75)
{
gotoxy(14,20);
SetColor(14,0);
cout<<"SHORT";
SetColor(15,0);
cout<<" NORMAL LONG"<<endl;
select=0;
}
}
else
if(select==2)
{
if(e==75)
{
gotoxy(14,20);
SetColor(15,0);
cout<<"SHORT";
SetColor(14,0);
cout<<" NORMAL";
SetColor(15,0);
cout<<" LONG"<<endl;
select=1;
}
}
}
if(select==0)
body=1;
else
if(select==1)
body=2;
else
body=3;
}