簡單的貪喫蛇遊戲,有高分榜,有無邊界兩種模式,可保存遊戲進度。
用到高分榜.txt,數據.txt,遊戲保存.txt 三個文件保存數據。
程序代碼:
//高分榜,無牆模式,障礙模式,可保存;
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<conio.h>
#include<cstring>
#include<fstream>
#define N 25
using namespace std;
char Map[N][N];
struct Position{ //座標結構體
int x,y;
};
Position snake[4000000],food,next; //蛇身,食物,蛇頭下一步 的座標
int head,tail,lenght,speed,grade; //蛇頭,蛇尾的下標 蛇的長度,速度,遊戲級別
char dirction,dirbef; //方向,上一個方向
int score=0; //分數
int order,wall,order_type; //命令,是否有牆
int rank[5]={0}; //高分榜
char first[20]={"1."};
char second[20]={"2."};
char thirst[20]={"3."};
char forth[20]={"4."};
char fifth[20]={"5."};
void SetFood(){//設置食物
srand((unsigned)time(0));
while(!(Map[food.y][food.x]==' ')){//隨機設置食物(不在蛇身上)
food.x=rand()%20 + 1;
food.y=rand()%20 + 1;
}
Map[food.y][food.x]='*';
}
void ShowRank(){ //顯示界面
system("cls"); //刷新
cout << "\n\n\t\t\t\t■■■■ ■■■ ■ ■ ■ ■\n";
cout << "\t\t\t\t■ ■ ■ ■ ■■ ■ ■ ■ \n";
cout << "\t\t\t\t■ ■ ■ ■ ■ ■ ■ ■ ■ \n";
cout << "\t\t\t\t■■■■ ■■■■ ■ ■ ■ ■■ \n";
cout << "\t\t\t\t■ ■ ■ ■ ■ ■■ ■ ■ \n";
cout << "\t\t\t\t■ ■ ■ ■ ■ ■ ■ ■ \n";
cout << "\t\t\t\t■ ■■ ■ ■ ■ ■ ■ ■\n";
cout<<"\n\t\t\t\t\t\t "<<first<<rank[0];
cout<<"\n\t\t\t\t\t\t "<<second<<rank[1];
cout<<"\n\t\t\t\t\t\t "<<thirst<<rank[2];
cout<<"\n\t\t\t\t\t\t "<<forth<<rank[3];
cout<<"\n\t\t\t\t\t\t "<<fifth<<rank[4];
}
void InitRank(){ //修改高分榜("高分榜".txt)
int i=6,j;
if(rank[0]<score) i=0;
for(j=0;j<5;j++){ //尋找插入位置
if(rank[j]==0&&rank[j-1]>score){//高分榜未滿時
i=j;
break;
}
}
for(j=4;j>=0;j--){
if(rank[j]>=score&&rank!=0){//高分榜滿時
i=j+1;
break;
}
}
if(i<5) cout<<"\n\n\t\t\t\t恭喜進入高分榜!\n\n\n";
for(j=4;j>i;j--){
rank[j]=rank[j-1];
}
rank[i]=score;
ofstream in;
in.open("高分榜.txt");
if(!in){
cout<<"error";
}
//保存到文件
in<<rank[0]<<' ';
in<<rank[1]<<' ';
in<<rank[2]<<' ';
in<<rank[3]<<' ';
in<<rank[4]<<' ';
}
void Save(){ //保存遊戲進度("數據".txt) ("遊戲保存".txt)
ofstream is("數據.txt");
is<<head<<' ';
is<<tail<<' ';
for(int i=tail;i<=head;i++){
is<<snake[i].x<<' ';
is<<snake[i].y<<' ';
}
is<<speed<<' ';
is<<lenght<<' ';
is<<score<<' ';
is<<wall<<' ';
is<<food.x<<' ';
is<<food.y<<' ';
is<<dirbef<<' ';
ofstream isave("遊戲保存.txt");
for(int i=0;i<N;i++){ //保存地圖
for(int j=0;j<N;j++){
isave<<Map[i][j];
}
}
}
void Hello(){ //歡迎界面
cout << "\n\n\t\t\t\t■■■ ■ ■ ■■■ ■ ■ ■■■■\n";
cout << "\t\t\t\t■ ■ ■■ ■ ■ ■ ■ ■ ■\n";
cout << "\t\t\t\t■ ■ ■ ■ ■ ■ ■ ■ ■\n";
cout << "\t\t\t\t ■■■ ■ ■ ■ ■■■■ ■■ ■■■\n";
cout << "\t\t\t\t ■ ■ ■■ ■ ■ ■ ■ ■\n";
cout << "\t\t\t\t■ ■ ■ ■ ■ ■ ■ ■ ■\n";
cout << "\t\t\t\t ■■■ ■ ■ ■ ■ ■ ■ ■■■■\n";
cout << "\n\n\n\t\t\t\t\t\t 1.新遊戲\n";
cout << "\n\t\t\t\t\t\t 2.繼續\n";
cout << "\n\t\t\t\t\t\t 3.高分榜\n";
cout << "\n\t\t\t\t\t\t 4.退出\n";
}
void SetMap(){ //初始化地圖
for(int i=0;i<N;i++){ //初始化地圖
for(int j=0;j<N;j++){
Map[i][j]=' ';
}
}
for(int k=0;k<N;k++){ //設置邊界
Map[0][k]=Map[N-1][k]=Map[k][0]=Map[k][N-1]='+';
}
ifstream fout("高分榜.txt"); //將高分榜載入緩存
for(int k=0;k<5;k++) fout>>rank[k];
SetFood();
Hello();
head=2;
tail=0;
dirction='d'; //默認初始方向向右
dirbef='d';
snake[2].x=3; //初始化蛇身
snake[2].y=1;
snake[0].x=1;
snake[0].y=1;
snake[1].x=2;
snake[1].y=1;
Map[1][3]='O';
Map[1][2]='O';
Map[1][1]='O';
lenght=3;
speed=500;
score=0;
grade=0;
cin>>order;
if(order==1){
system("cls");
cout << "\n\n\t\t\t\t■■■ ■ ■ ■■■ ■ ■ ■■■■\n";
cout << "\t\t\t\t■ ■ ■■ ■ ■ ■ ■ ■ ■\n";
cout << "\t\t\t\t■ ■ ■ ■ ■ ■ ■ ■ ■\n";
cout << "\t\t\t\t ■■■ ■ ■ ■ ■■■■ ■■ ■■■\n";
cout << "\t\t\t\t ■ ■ ■■ ■ ■ ■ ■ ■\n";
cout << "\t\t\t\t■ ■ ■ ■ ■ ■ ■ ■ ■\n";
cout << "\t\t\t\t ■■■ ■ ■ ■ ■ ■ ■ ■■■■\n";
cout<<"\n\n\n\t\t\t\t\t\t 1.普通模式\n";
cout<<"\n\t\t\t\t\t\t 2.無牆模式\n";
cin>>order_type;
if(order_type==1) wall=1;
else wall=0;
}
if(order==2){ //載入保存數據
ifstream os("數據.txt");
os>>head;
os>>tail;
for(int i=tail;i<=head;i++){
os>>snake[i].x;
os>>snake[i].y;
}
os>>speed;
os>>lenght;
os>>score;
os>>wall;
os>>food.x;
os>>food.y;
os>>dirction;
ifstream osave("遊戲保存.txt");
osave>>noskipws;
for(int q=0;q<N;q++){
for(int w=0;w<N;w++){
osave>>Map[q][w];
}
}
}
if(order==4) exit(-1);
if(order!=3){
for(int time=3;time>0;time--) {
system("cls");
cout<<"\n\t\t\t\t遊戲即將開始:"<<time;
double start=(double)clock()/CLOCKS_PER_SEC;
while((double)clock()/CLOCKS_PER_SEC-start<=1);
}
}
}
void ShowMap(){ //顯示地圖
system("cls");
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
cout<<Map[i][j]<<' ';
}
if(i==2) cout<<"\t\tScore="<<score;
if(i==4) cout<<"\t\tLenght="<<lenght;
if(i==6) cout<<"\t\tSpeed="<<600-speed;
if(i==8) cout<<"\t\t 按P保存";
cout<<endl;
}
}
bool Gameover(){ //判斷遊戲結束
if(dirction=='p'){ //保存遊戲
system("cls");
cout<<"\n\n\t\t\t\t已保存\n\n\n";
return 0;
}
if(wall){ //有牆模式
if(Map[next.y][next.x] == 'O' ||Map[next.y][next.x]=='+') {
Map[next.y][next.x] = '@' ;
Map[snake[head].y][snake[head].x]='O';
Map[snake[tail].y][snake[tail].x]=' ';
ShowMap();
system("cls");
cout << "\t\t ■■■■ ■■■ ■ ■ ■■■■ ■■■■ ■ ■ ■■■■ ■■■■ \n";
cout << "\t\t■ ■ ■ ■■ ■■ ■ ■ ■ ■ ■ ■ ■ ■ \n";
cout << "\t\t■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \n";
cout << "\t\t■ ■■■ ■■■■ ■ ■■ ■ ■■■ ■ ■ ■ ■ ■■■ ■■■■ \n";
cout << "\t\t■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \n";
cout << "\t\t■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■■ ■ ■ ■ \n";
cout << "\t\t ■■■■ ■ ■ ■ ■ ■■■■ ■■■■ ■ ■■■■ ■ ■■ \n";
return 0;
}
}
else{ //無牆模式
if(Map[next.y][next.x] == 'O') {
Map[next.y][next.x] = '@' ;
Map[snake[head].y][snake[head].x]='O';
Map[snake[tail].y][snake[tail].x]=' ';
ShowMap();
system("cls");
cout << "\n\n\t\t ■■■■ ■■■ ■■ ■■ ■■■■ ■■■■ ■ ■ ■■■■ ■■■■ \n";
cout << "\t\t■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \n";
cout << "\t\t■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \n";
cout << "\t\t■ ■■■ ■■■■ ■ ■ ■ ■ ■■■ ■ ■ ■ ■ ■■■ ■■■■ \n";
cout << "\t\t■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \n";
cout << "\t\t■ ■ ■ ■ ■ ■■ ■ ■ ■ ■ ■■ ■ ■ ■ \n";
cout << "\t\t ■■■■ ■ ■ ■ ■ ■ ■■■■ ■■■■ ■ ■■■■ ■ ■■ \n";
return 0;
}
}
return 1;
}
int SnakeMove(){//貪喫蛇的移動
if(score%100==0&&score!=0) speed=500-score; //等級(改變速度)
bool timeover = true;
double start = (double)clock() / CLOCKS_PER_SEC; //得到程序目前爲止運行的時間
while ((timeover = ((double)clock() / CLOCKS_PER_SEC - start <= speed / 1000.0)) && !_kbhit());//自動經過1秒或者等待1秒內的鍵盤輸入
//鍵盤輸入
dirbef=dirction;
if (timeover)
{
dirction=_getch(); //獲取方向
}
if(dirction!='w'&&dirction!='a'&&dirction!='s'&&dirction!='d'||((dirction=='w'&&dirbef=='s')||(dirction=='a'&&dirbef=='d')||(dirction=='s'&&dirbef=='w')||(dirction=='d'&&dirbef=='a')))
dirction=dirbef;
switch(dirction){
case 'w':
next.y = snake[head].y-1;
next.x = snake[head].x;
break;
case 's':
next.y = snake[head].y+1;
next.x = snake[head].x;
break;
case 'a':
next.y = snake[head].y;
next.x = snake[head].x-1;
break;
case 'd':
next.y = snake[head].y;
next.x = snake[head].x+1;
break;
case 'p':
Save();
Gameover();
}
if(!wall) {
if(next.y==0) next.y=N-2;
if(next.y==N-1) next.y=1;
if(next.x==0) next.x=N-2;
if(next.x==N-1) next.x=1;
}
}
void Eat(){//判斷貪喫蛇有沒有喫到食物
if(next.x==food.x&&next.y==food.y){//喫到
score+=10;
Map[snake[head].y][snake[head].x]='O';
head++;
snake[head].x=next.x;
snake[head].y=next.y;
Map[snake[head].y][snake[head].x]='@';
lenght++;
SetFood();
ShowMap();
}
else{//沒喫到
Map[snake[tail].y][snake[tail].x]=' ';
tail++;
Map[snake[head].y][snake[head].x]='O';
head++;
snake[head].x=next.x;
snake[head].y=next.y;
Map[snake[head].y][snake[head].x]='@';
ShowMap();
}
}
int main(){
while(1){
SetMap();
if(order==3){
ShowRank();
}
else{
while(1){
SnakeMove();
if(!Gameover()) {
if(dirction!='p') InitRank();
break;
}
Eat();
}
}
cout<<"\n....................按任意鍵繼續\n";
getchar();
getchar();
system("cls");
order=0;
dirction=dirbef;
}
}