c++ 貪吃蛇小遊戲

來源:互聯網
上載者:User

標籤:amp   時間   ios   out   names   efi   window   rand   ++   

(地圖大小為25*25,蛇初始位置為數組的map[3][3]~map[3][6],蛇頭為map[3][6],方向向右)

#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <time.h> 
#include <conio.h>
using namespace std;

#define MAX_WIDE 25
#define MAX_HIGH 25

char map[MAX_HIGH][MAX_WIDE];  //地圖大小

struct node
{
int x, y;
};
node snack[(MAX_WIDE - 2)*(MAX_HIGH - 2)], food;  //定義蛇身和食物座標

int lenth, direct, live = 1;                //蛇的長度,方向和存活
int speech = 200;

void initMap(node *n, int len) {              //初始化地圖
  for (int i = 0; i < MAX_HIGH; i++)
    for (int j = 0; j < MAX_WIDE; j++)
      map[i][j] = ‘ ‘;
  for (int i = 0; i < MAX_HIGH; i++)          //設定左右的牆
    map[i][0] = map[i][MAX_WIDE - 1] = ‘|‘;
  for (int j = 0; j < MAX_WIDE; j++)
    map[0][j] = map[MAX_HIGH - 1][j] = ‘-‘;     //設定上下的牆
  for (int j = 1; j < len; j++)
    map[n[j].x][n[j].y] = ‘*‘;             //設定蛇身
  map[n[0].x][n[0].y] = ‘#‘;              //設定蛇頭
}  

 

void showMap() {                     //輸出數組map
  system("cls");                    //清屏
  for (int i = 0; i < MAX_HIGH; i++) {         
    for (int j = 0; j < MAX_WIDE; j++) {
      cout << map[i][j];
    }
    cout << endl;
  }
}

 

void showFood() {                    //產生食物,座標在map中且不為蛇上
  srand((unsigned)time(NULL));            //食物的產生點隨機
  do {
    food.x = rand() % MAX_WIDE;
    food.y = rand() % MAX_HIGH;
  } while (map[food.x][food.y] != ‘ ‘);
  map[food.x][food.y] = ‘o‘;
}

int updataGame() {                   //更新數組
  long start = clock();
  int a, b;                       //暫存 移動後蛇頭的位置
  while (!_kbhit() && (clock() - start <= speech));   //直到有按鍵或者時間過了speech則跳出迴圈
  if (_kbhit()) {
    _getch();                    //上、下、左、右鍵是二個位元組的,取第二個位元組分別為(72,80,75,77)  
    direct = _getch();
  }
  switch (direct)
  {
  case 72:                      //up
    a = snack[0].x - 1;
    b = snack[0].y;
    break;
  case 80:                      //down
    a = snack[0].x + 1;
    b = snack[0].y;
    break;
  case 75:                      //left
    a = snack[0].x;
    b = snack[0].y - 1;
    break;
  case 77:                      //right
    a = snack[0].x;
    b = snack[0].y + 1;
    break;
  default:
    break;
  }
  if (a == 0 || a == MAX_HIGH - 1 || b == 0 || b == MAX_WIDE - 1)    //撞牆
    live = 0;
  if (map[a][b] == ‘*‘)                            //撞自身
    live = 0;
  map[snack[0].x][snack[0].y] = ‘*‘;                     //將蛇頭置為蛇身
  map[snack[lenth - 1].x][snack[lenth - 1].y] = ‘ ‘;              //蛇尾置為空白
  map[a][b] = ‘#‘;                              //更新蛇頭
  for (int i = lenth - 1; i > 0; i--)                       //更新蛇數組
    snack[i] = snack[i - 1];
  snack[0].x = a;
  snack[0].y = b;
  if (a == food.x && b == food.y) {                      //蛇吃了食物
    map[snack[lenth - 1].x][snack[lenth - 1].y] = ‘*‘;            //取消蛇尾置空
    lenth++;
    showFood();                               //產生新食物
    speech -= lenth;                             //加速
  }
  showMap();
  return live;
}


int main() {
  snack[0].x = 3;
  snack[0].y = 6;
  lenth = 4;
  for (int i = 1; i < lenth; i++) {
    snack[i].x = 3;
    snack[i].y = snack[i - 1].y - 1;
  }
  direct = 77;
  initMap(snack, lenth);
  showFood();
  showMap();
  do {
    updataGame();
  } while (live);
  cout << "得分為:" << lenth-4 << endl;
  system("pause");
  return 0;
}

c++ 貪吃蛇小遊戲

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.