int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
#include "stdafx.h"
#include<windows.h>
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
using namespace std;
// 刷新當前屏幕 //見博客http://www.cnblogs.com/uniqueliu/archive/2011/07/10/2102238.html
inline void Refresh(char q[][22], int grade, int gamespeed) //c語言 inline函數的總結http://blog.csdn.net/yuan1125/article/details/6225993
{
system("cls"); // 清屏 實際上是把屏幕翻頁 看不到而已int i,j;
cout << endl; //就是回車的意思~相當於C語言裏面的printf("");
for(i=0;i<22;i++)
{
cout << "\t"; // \t \r \n都是轉義字符,空格就是單純的空格,輸入時可以輸入空格 \t 的意思是橫向跳到下一製表符位置 \r 的意思是回車 \n 的意思是回車換行for(j=0;j<22;j++)
cout<<q[i][j]<<' '; // 輸出貪喫蛇棋盤
if(i==0) cout << "\t等級爲:" << grade;
if(i==4) cout << "\t自動前進時間";
if(i==6) cout << "\t間隔爲:" << gamespeed << "ms";
cout<<endl;
}
}
int main()
{
char tcsQipan[22][22]; // 貪喫蛇棋盤是一個二維數組(如22*22,包括牆壁)int i,j;
for(i=1;i<=20;i++)
for(j=1;j<=20;j++)
tcsQipan[i][j]=' '; // 初始化貪喫蛇棋盤中間空白部分
for(i=0;i<=21;i++)
tcsQipan[0][i] = tcsQipan[21][i] = '-'; //初始化貪喫蛇棋盤上下牆壁
for(i=1;i<=20;i++)
tcsQipan[i][0] = tcsQipan[i][21] = '|'; //初始化貪喫蛇棋盤左右牆壁
int tcsZuobiao[2][100]; //蛇的座標數組 //int tcsZuobiao[2][100];後面的[100]是數組???
for(i=0; i<4; i++)
{
tcsZuobiao[0][i] = 1;tcsZuobiao[1][i] = i + 1;
}
int head = 3,tail = 0;
for(i=1;i<=3;i++)
tcsQipan[1][i]='*'; //蛇身
tcsQipan[1][4]='#'; //蛇頭
int x1, y1; // 隨機出米
srand(time(0)); // srand(time(0)) 就是給這個算法一個啓動種子,也就是算法的隨機種子數,有這個數以後纔可以產生隨機數
do
{
x1=rand()%20+1;y1=rand()%20+1;
}
while(tcsQipan[x1][y1]!=' ');
tcsQipan[x1][y1]='*';cout<<"\n\n\t\t貪喫蛇遊戲即將開始 !"<<endl;//準備開始;;
long start;
int grade = 1, length = 4;
int gamespeed = 500; //前進時間間隔
for(i=3;i>=0;i--)
{
start=clock();while(clock()-start<=1000);
system("cls");
if(i>0)
cout << "\n\n\t\t進入倒計時:" << i << endl;
else
Refresh(tcsQipan,grade,gamespeed);
}
int timeover;
char direction = 77; // 初始情況下,向右運動
int x,y;
while(1)
{
timeover = 1;start = clock();
while((timeover=(clock()-start<=gamespeed))&&!kbhit());
//如果有鍵按下或時間超過自動前進時間間隔則終止循環
if(timeover)
{
getch();direction = getch();}
switch(direction)
{
case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break; // 向上case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break; // 向下
case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break; // 向左
case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1; // 向右
}
if(!(direction==72||direction==80||direction==75 ||direction==77)){ // 按鍵非方向鍵
cout << "\tGame over!" << endl;return 0;
}
if(x==0 || x==21 ||y==0 || y==21) // 碰到牆壁
{
cout << "\tGame over!" << endl;
return 0;
}if(tcsQipan[x][y]!=' '&&!(x==x1&&y==y1)){ // 蛇頭碰到蛇身
cout << "\tGame over!" << endl;
return 0;
}if(x==x1 && y==y1) // 喫米,長度加1
{
length ++;if (length>=8)
{
length -= 8;grade ++;
if(gamespeed>=200)
gamespeed = 550 - grade * 50; // 改變自動前進時間間隔
}
tcsQipan[x][y]= '#';
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '*';
head = (head+1)%100;
tcsZuobiao[0][head] = x;
tcsZuobiao[1][head] = y;
do
{
x1=rand()%20+1;
y1=rand()%20+1;
}while(tcsQipan[x1][y1]!=' ');
tcsQipan[x1][y1]='*';
Refresh(tcsQipan,grade,gamespeed);
}
else // 不喫米
{
tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=' ';tail=(tail+1)%100;
tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]='*';
head=(head+1)%100;
tcsZuobiao[0][head]=x;
tcsZuobiao[1][head]=y;
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]='#';
Refresh(tcsQipan,grade,gamespeed);
}
}
return 0;
}