DEVC++實(shí)現(xiàn)推箱子小游戲的方法

小編給大家分享一下DEVC++實(shí)現(xiàn)推箱子小游戲的方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

為安遠(yuǎn)等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及安遠(yuǎn)網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、安遠(yuǎn)網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

具體內(nèi)容如下

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include <windows.h>

using namespace std;
void Game_Menu(HANDLE hout);
void Game_description(HANDLE hout);
void gotoxy(HANDLE hout, int x, int y);
int DrawMap(HANDLE hout);
void Move(HANDLE hout);
int finish();
void setmap(int n);
void color(int m);
bool flag = true;
int pass = 1;
#define R 10
#define C 10
#define framex 20
#define framey 14 
int map[R][C] = {0};
//關(guān)卡1 
int map1[R][C] = {
{ 0,0,1,1,1,0,0,0 },
{ 0,0,1,3,1,0,0,0 },
{ 0,0,1,0,1,1,1,1 },
{ 1,1,1,0,0,4,3,1 },
{ 1,3,4,4,0,1,1,1 },
{ 1,1,1,5,4,1,0,0 },
{ 0,0,0,1,3,1,0,0 },
{ 0,0,0,1,1,1,0,0 }
};
//地圖 2(關(guān)卡2)
int map2[R][C]={
{1,1,1,1,1,0,0,0,0,0},
{1,5,0,0,1,0,0,0,0,0},
{1,0,4,4,1,0,1,1,1,0},
{1,0,4,0,1,0,1,3,1,0},
{1,1,1,0,1,1,1,3,1,0},
{0,1,1,0,0,0,0,3,1,0},
{0,1,0,0,0,1,0,0,1,0},
{0,1,0,0,0,1,1,1,1,0},
{0,1,1,1,1,1,0,0,0,0}
};
//地圖 3(關(guān)卡3)
int map3[R][C]={
{ 0,0,0,1,1,1,1,1,1,1 },
{ 0,0,1,1,0,0,1,0,5,1 },
{ 0,0,1,0,0,0,1,0,0,1 },
{ 0,0,1,4,0,4,0,4,0,1 },
{ 0,0,1,0,4,1,1,0,0,1 },
{ 1,1,1,0,4,0,1,0,1,1 },
{ 1,3,3,3,3,3,0,0,1,0 },
{ 1,1,1,1,1,1,1,1,1,0 },
};
void Game_Menu(HANDLE hout){//游戲開始菜單 
  system("cls");
  gotoxy(hout, framex, 1);
  cout << "*******************";
  gotoxy(hout, framex, 3);
  cout << "   推箱子    ";
  gotoxy(hout, framex, 5);
  cout << "  按s或S開始   ";
  gotoxy(hout, framex, 7);
  cout << "  按q或Q退出   ";
  gotoxy(hout, framex, 9);
  cout << "游戲前關(guān)閉中文輸入 ";
  gotoxy(hout, framex, 11);
  cout << "*******************";
  _getch();
};
void Game_description(HANDLE hout){//操作提示 
  system("cls");
  gotoxy(hout, framex, 1);
  cout << "*****************************";
  gotoxy(hout, framex, 3);
  cout << "   按方向鍵上下左右移動  ";
  gotoxy(hout, framex, 5);
  cout << " 所有箱子到達(dá)目的地游戲勝利 ";
  gotoxy(hout, framex, 7);
  cout << "     按q或Q退出     ";
  gotoxy(hout, framex, 9);
  cout << "*****************************";
};
void gotoxy(HANDLE hout, int x, int y){
  COORD pos;
  pos.X = x;
  pos.Y = y;
  SetConsoleCursorPosition(hout, pos);
};
void print(int i){//根據(jù)map值輸出相應(yīng)的圖形 
  switch(i){
        case 0:
          color(0x7);
          cout << " "; //空地■★□?◇◆??¤
          break;
          case 1:
          color(8);
          cout << "■";//墻體
          break;
          case 3:
          color(0xE);
          cout << "◇";//目的地
          break;
          case 4:
          color(4);
          cout << "□";//箱子 
          break;
          case 5:
          color(3);
          cout << "♀"; //人
          break;
          case 7:   //4+3  箱子到達(dá)目的地
          color(6);  
          cout << "◆";
          break;
          case 8:   //5+3 人與目的地重合
          color(3);
          cout << "♀";
          break;
          default:
          break;
        }
}
int DrawMap(HANDLE hout){//新的關(guān)卡開始時載入地圖 
  //HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
  gotoxy(hout, framex + C, framey - 3);
  color(0xF);
  cout << "第" << pass << "關(guān)";
  //printf("第%d關(guān)", pass); 
  for(int i = 0; i < R; i++){
    gotoxy(hout, framex, framey + i);
    for(int j = 0; j < C; j++){
      
      print(map[i][j]);
    }
  }
  return 0;
};
void Move(HANDLE hout){//移動小人 
  int x = 0, y = 0;
  for(int i = 0; i < R; i++){
    for(int j = 0; j < C; j++){
      if(map[i][j] == 5 || map [i] [j] == 8){
        x = i;
        y = j;
        break;
      }
    }
  }
  gotoxy(hout, framex, framey + R);
  color(0xF);
  printf("當(dāng)前位置:(%d, %d)", x, y);
  int ch = _getch();
  
  switch(ch){
    
    case 'w':
    case 'W':
    case 72:
      if(map[x - 1][y] == 0 || map[x - 1][y] == 3){
        map[x][y] -= 5;
        gotoxy(hout, framex + 2 * y, framey + x);
        print(map[x][y]);
        map[x - 1][y] += 5;
        gotoxy(hout, framex + 2 * y, framey + x - 1);
        print(map[x - 1][y]);
      }
      else if(map[x - 1][y] == 4 || map[x - 1][y] == 7){
        if(map[x - 2][y] == 0 || map[x - 2][y] == 3){
          map[x][y] -= 5;
          gotoxy(hout, framex + 2 * y, framey + x);
          print(map[x][y]);
          map[x - 1][y] += 1;
          gotoxy(hout, framex + 2 * y, framey + x - 1);
          print(map[x - 1][y]);
          map[x - 2][y] += 4;
          gotoxy(hout, framex + 2 * y, framey + x - 2);
          print(map[x - 2][y]);
        }
      }
      break;
    
    case 's':
    case 'S':
    case 80:
      if(map[x + 1][y] == 0 || map[x + 1][y] == 3){
        map[x][y] -= 5;
        gotoxy(hout, framex + 2 * y, framey + x);
        print(map[x][y]);
        map[x + 1][y] += 5;
        gotoxy(hout, framex + 2 * y, framey + x + 1);
        print(map[x + 1][y]);
      }
      else if(map[x + 1][y] == 4 || map[x + 1][y] == 7){
        if(map[x + 2][y] == 0 || map[x + 2][y] == 3){
          map[x][y] -= 5;
          gotoxy(hout, framex + 2 * y, framey + x);
          print(map[x][y]);
          map[x + 1][y] += 1;
          gotoxy(hout, framex + 2 * y, framey + x + 1);
          print(map[x + 1][y]);
          map[x + 2][y] += 4;
          gotoxy(hout, framex + 2 * y, framey + x + 2);
          print(map[x + 2][y]);
        }
      }
      break;
    
    case 'a':
    case 'A':
    case 75:
      if(map[x][y - 1] == 0 || map[x][y - 1] == 3){
        map[x][y] -= 5;
        gotoxy(hout, framex + 2 * y, framey + x);
        print(map[x][y]);
        map[x][y - 1] += 5;
        gotoxy(hout, framex + 2 * y - 2, framey + x);
        print(map[x][y - 1]);
      }
      else if(map[x][y - 1] == 4 || map[x][y - 1] == 7){
        if(map[x][y - 2] == 0 || map[x][y - 2] == 3){
          map[x][y] -= 5;
          gotoxy(hout, framex + 2 * y, framey + x);
          print(map[x][y]);
          map[x][y - 1] += 1;
          gotoxy(hout, framex + 2 * y - 2, framey + x);
          print(map[x][y - 1]);
          map[x][y - 2] += 4;
          gotoxy(hout, framex + 2 * y - 4, framey + x);
          print(map[x][y - 2]);
        }
      }
      break;
    
    case 'd':
    case 'D':
    case 77:
      if(map[x][y + 1] == 0 || map[x][y + 1] == 3){
        map[x][y] -= 5;
        gotoxy(hout, framex + 2 * y, framey + x);
        print(map[x][y]);
        map[x][y + 1] += 5;
        gotoxy(hout, framex + 2 * y + 2, framey + x);
        print(map[x][y + 1]);
      }
      else if(map[x][y + 1] == 4 || map[x][y + 1] == 7){
        if(map[x][y + 2] == 0 || map[x][y + 2] == 3){
          map[x][y] -= 5;
          gotoxy(hout, framex + 2 * y, framey + x);
          print(map[x][y]);
          map[x][y + 1] += 1;
          gotoxy(hout, framex + 2 * y + 2, framey + x);
          print(map[x][y + 1]);
          map[x][y + 2] += 4;
          gotoxy(hout, framex + 2 * y + 4, framey + x);
          print(map[x][y + 2]);
        }
      }
      break;
    case 'q':
    case 'Q':
      flag = false;
    default:
      break;  
  }
};
int finish(){//判斷游戲結(jié)束 
  for(int i = 0; i < R; i++){
    for(int j = 0; j < C; j++){
      if(map[i][j] == 4)return 0;
    }
  }
  return 1;
};
void setmap(int n){
  switch(n){
    case 1:
      memcpy(map, map1, sizeof(map1));
      break;
    case 2:
      memcpy(map, map2, sizeof(map2));
      break;
    case 3:
      memcpy(map, map3, sizeof(map3));
      break;
  }
};
void color(int m){//改變輸出符號的顏色 
  HANDLE consolehend;
  consolehend = GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleTextAttribute(consolehend, m); 
};
int main(){
  HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
  
  Game_Menu(hout);
  char ch = getch();
  setmap(pass);
  Game_description(hout);
  DrawMap(hout);
  if(ch == 'q' || ch == 'Q'){
    return 0;
  }
  while(flag){
    Move(hout);
    if(finish()){  
      DrawMap(hout);
      gotoxy(hout, framex, framey + R);
      cout << " 恭喜,成功過關(guān)!";
      gotoxy(hout, framex, framey + R + 2);
      cout << "重玩(R)";
      ch = getch();
      system("cls");
      pass++;
      if(ch == 'r' || ch == 'R')pass--;
      if(pass > 3) { 
        gotoxy(hout, framex, framey);
        cout << " 您已通過全部關(guān)卡!";
        getch();
        flag = false;
      } 
      else{
        setmap(pass);
        Game_description(hout);
        DrawMap(hout);
      }
    }
  }
  return 0; 
}

DEVC++實(shí)現(xiàn)推箱子小游戲的方法

看完了這篇文章,相信你對“DEVC++實(shí)現(xiàn)推箱子小游戲的方法”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

分享題目:DEVC++實(shí)現(xiàn)推箱子小游戲的方法
標(biāo)題路徑:http://bm7419.com/article12/jjegdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、App開發(fā)、外貿(mào)網(wǎng)站建設(shè)做網(wǎng)站、品牌網(wǎng)站建設(shè)Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司