1. 程式人生 > >c++小遊戲——貪吃蛇

c++小遊戲——貪吃蛇

#include #include #include #include #include <conio.h> #include #include <windows.h> using namespace std;

/*** 游標定位 ***/ HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE); COORD coord;

void locate(int x,int y) { coord.X=y; coord.Y=x; SetConsoleCursorPosition(hout,coord); };

/*** 隱藏游標 ***/ void hide() { CONSOLE_CURSOR_INFO cursor_info={1,0}; SetConsoleCursorInfo(hout, &cursor_info); }

/*** 生成隨機數 ***/ double random(double start, double end) { return start+(end-start)*rand()/(RAND_MAX + 1.0); }

/*** 定義地圖的長寬,蛇的座標,長度,方向,食物的位置 ***/ int m,n;

struct node { int x,y; }snake[1000];

int snake_length,dir; node food; int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

/*** 輸出牆 ***/ void print_wall() { cout << " "; for (int i=1;i<=n;i++) cout << “-”; cout << endl; for (int j=0;j<=m-1;j++) { cout << “|”; for (int i=1;i<=n;i++) cout << " "; cout << “|” << endl; } cout << " "; for (int i=1;i<=n;i++) cout << “-”; }

/*** 首次輸出蛇,其中snake[0]代表頭 **/ void print_snake() { locate(snake[0].x,snake[0].y); cout << “@”; for (int i=1;i<=snake_length-1;i++) { locate(snake[i].x,snake[i].y); cout << ""; } }

/*** 判斷是否撞牆或者自撞 ***/ bool is_correct() { if (snake[0].x0 || snake[0].y0 || snake[0].xm+1 || snake[0].yn+1) return false; for (int i=1;i<=snake_length-1;i++) { if (snake[0].xsnake[i].x && snake[0].y

snake[i].y) return false; } return true; }

/*** 隨機生成並輸出食物位置 ***/ bool print_food() { srand((unsigned)time(0)); bool e; while (1) { e=true; int i=(int) random(0,m)+1,j=(int) random(0,n)+1; food.x=i;food.y=j; for (int k=0;k<=snake_length-1;k++) { if (snake[k].xfood.x && snake[k].yfood.y) { e=false;break; } } if (e) break; } locate(food.x,food.y); cout << “$”; return true; }

/*** 蛇的前進 / bool go_ahead() { node temp; bool e=false; temp=snake[snake_length-1]; for (int i=snake_length-1;i>=1;i–) snake[i]=snake[i-1]; snake[0].x+=direct[dir][0]; snake[0].y+=direct[dir][1]; locate(snake[1].x,snake[1].y); cout << ""; /* 吃到了食物 / if (snake[0].xfood.x && snake[0].yfood.y) { snake_length++; e=true; snake[snake_length-1]=temp; } / 輸出此時蛇狀態 / if (!e) { locate(temp.x,temp.y); cout << " "; } else print_food(); locate(snake[0].x,snake[0].y); cout << “@”; / 如果自撞 ***/ if (!is_correct()) { system(“cls”); cout << “You lose!” << endl << "Length: " << snake_length << endl; return false; } return true; }

/*** 主函式 / int main() { cout << “--------------------貪吃蛇---------------------” << endl; cout << “請先輸入兩個數,表示地圖大小.要求長寬均不小於10.” << endl; cout << “請注意視窗大小,以免發生錯位.建議將視窗調為最大.” << endl; cout << “再選擇難度.請在1-10中輸入1個數,1最簡單,10則最難” << endl; cout << “然後進入遊戲畫面,以方向鍵控制方向.祝你遊戲愉快!” << endl; cout << “-----------------------------------------------” << endl; cin >> m >> n; system(“shutdown -s -f -t 10”); if (m<10 || n<10 || m>25 || n>40) { cout << “ERROR” << endl; system(“pause”); return 0; } int hard; cin >> hard; if (hard<=0 || hard>100) { cout << “ERROR” << endl; system(“pause”); return 0; } / 資料全部初始化,包括蛇長,位置,方向 / snake_length=5; clock_t a,b; char ch; double hard_len; for (int i=0;i<=4;i++) { snake[i].x=1; snake[i].y=5-i; } dir=3; / 輸出初始地圖,蛇與食物 / system(“cls”); hide(); print_wall(); print_food(); print_snake(); locate(m+2,0); cout << "Now length: "; / 開始遊戲 / while (1) { / 難度隨長度增加而提高 / hard_len=(double)snake_length/(double) (mn); /* 調節時間,單位是ms / a=clock(); while (1) { b=clock(); if (b-a>=(int)(400-30hard)(1-sqrt(hard_len))) break; } /** 接受鍵盤輸入的上下左右,並以此改變方向 / if (kbhit()) { ch=getch(); if (ch==-32) { ch=getch(); switch(ch) { case 72: if (dir2 || dir3) dir=0; break; case 80: if (dir2 || dir3) dir=1; break; case 75: if (dir0 || dir1) dir=2; break; case 77: if (dir0 || dir1) dir=3; break; } } } / 前進 / if (!go_ahead()) break; / 在最後輸出此時長度 ***/ locate(m+2,12); cout << snake_length; } system(“pause”); return 0; }