C/C++實現推箱子小遊戲

本文實例為大傢分享瞭C/C++實現推箱子小遊戲的具體代碼,供大傢參考,具體內容如下

效果演示

實現功能

如上圖所示。按鍵控制小豬的運動,推箱子到達目的地。

如何實現

1.首先思考要保存箱子,小豬等信息,添加多個map可以用到三維數組。
2.定義小豬,箱子,墻,空地等信息在三維數組裡的數值。

 空地             0     
 墻               1
 目的地           2   
 箱子             3
 豬               4  2+4=6 豬到達目的地也顯示豬
 箱子到達目的地   2+3=5

3.按鍵實現推箱子運動。

考慮多種情況:

①.小豬的運動(空地,目的在)
②.小豬推箱子運動(空地,目的地)

用三維數組保存map信息:這裡我隻做瞭兩種圖,可自行添加

int backgrand[MAP][ROW][COL] =//map圖
{
 {
  1, 1, 1, 1, 1, 1, 1, 1,
  1, 0, 0, 0, 2, 0, 0, 1,
  1, 1, 0, 0, 3, 1, 1, 1,
  1, 2, 3, 0, 4, 0, 0, 1,
  1, 0, 1, 0, 3, 3, 2, 1,
  1, 1, 0, 3, 2, 0, 0, 1,
  1, 0, 0, 2, 0, 0, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1
 },
 {
  0, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 0, 2, 0, 0, 0, 1,
  1, 1, 0, 0, 3, 0, 1, 1,
  1, 2, 3, 0, 4, 0, 0, 1,
  1, 0, 1, 0, 0, 0, 1, 1,
  1, 1, 0, 3, 2, 3, 0, 1,
  1, 0, 0, 0, 0, 2, 0, 1,
  1, 1, 1, 1, 1, 1, 1, 1
 },
};

繪圖用到瞭easyx庫,加載圖片和繪制窗口隻用到瞭三條語句:很簡單

initgraph();//繪制窗口大小
loadimage();//加載圖片
putinmage();//放出圖片

具體可看代碼!

代碼塊

#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<graphics.h>
#define ROW 8
#define COL 8
#define MAP 2//map個可以自己添加數


int count = 0;//哪個map圖
/*
空地             0
墻               1
目的地           2
箱子             3
豬               4  2+4=6 豬到達目的地也顯示豬
箱子到達目的地   2+3=5


*/


IMAGE image1, image2, image3, image4, image5, image6;

int backgrand[MAP][ROW][COL] =//map圖
{
 {
  1, 1, 1, 1, 1, 1, 1, 1,
  1, 0, 0, 0, 2, 0, 0, 1,
  1, 1, 0, 0, 3, 1, 1, 1,
  1, 2, 3, 0, 4, 0, 0, 1,
  1, 0, 1, 0, 3, 3, 2, 1,
  1, 1, 0, 3, 2, 0, 0, 1,
  1, 0, 0, 2, 0, 0, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1
 },
 {
  0, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 0, 2, 0, 0, 0, 1,
  1, 1, 0, 0, 3, 0, 1, 1,
  1, 2, 3, 0, 4, 0, 0, 1,
  1, 0, 1, 0, 0, 0, 1, 1,
  1, 1, 0, 3, 2, 3, 0, 1,
  1, 0, 0, 0, 0, 2, 0, 1,
  1, 1, 1, 1, 1, 1, 1, 1
 },
};
int Judge(){//p判斷是否結束一局
 int i = 0;
 for (i = 0; i < ROW; i++){
  int j = 0;
  for (j = 0; j < COL; j++){
   if (backgrand[count][i][j] == 3 || backgrand[count][i][j] == 2){//判斷是否還有箱子或者目的地
    return 0;
   }
  }
 }
 return 1;
}

void Backgrand(){//繪制地圖
 int i = 0;
 for (; i < ROW; i++){
  int j = 0;
  for (; j < COL; j++){
   int x = 80 * j;//橫軸是y(列),數軸是x(行)。
   int y = 80 * i;
   switch (backgrand[count][i][j]){
   case 0:
    putimage(x, y, &image3);
    break;
   case 1:
    putimage(x, y, &image1);
    break;
   case 2:
    putimage(x, y, &image6);
    break;
   case 3:
    putimage(x, y, &image4);
    break;
   case 4:
   case 6:
    putimage(x, y, &image2);
    break;
   case 5:
    putimage(x, y, &image5);
    break;
   default:
    break;
   }
  }
 }
}

void Keymove(){//按鍵控制豬的運動
 int i = 0;
 int j = 0;//j定義出來瞭,裡面for循環要初始化
 for (i = 0; i < ROW; i++){ //找到豬的坐標
  for (j = 0; j < COL; j++){
   if (backgrand[count][i][j] == 4 || backgrand[count][i][j] == 6){
    break;
   }
  }
  if (backgrand[count][i][j] == 4 || backgrand[count][i][j] == 6){
   break;
  }
 }
 char key = _getch();//獲得按鍵
 switch (key){
 case 'w':
 case 'W':
 case 72:
  if (backgrand[count][i - 1][j] == 0 ||\
   backgrand[count][i - 1][j] == 2)
  {  
   backgrand[count][i][j] = backgrand[count][i][j] - 4;//豬運動,豬旁邊是空地或者是目的地
   backgrand[count][i - 1][j] = backgrand[count][i - 1][j] + 4;
  }
  if (backgrand[count][i - 1][j] == 3 && (backgrand[count][i - 2][j] == 0 || \
   backgrand[count][i - 2][j] == 2))   //豬推箱子運動條件,豬旁邊是箱子與上箱子旁邊是空地。
  {                                       //後面或的條件要括號括起來
   backgrand[count][i][j] = backgrand[count][i][j] - 4;                                      
   backgrand[count][i - 1][j] = backgrand[count][i - 1][j] + 4;
   backgrand[count][i - 1][j] -= 3;
   backgrand[count][i - 2][j] += 3;
  }
  if (backgrand[count][i - 1][j] == 5 &&\
   backgrand[count][i - 2][j] == 0)  //箱子到達目的地,後面還有空的情況
  {
   backgrand[count][i][j] -= 4;
   backgrand[count][i - 1][j] += 4;
   backgrand[count][i - 1][j] -= 3;
   backgrand[count][i - 2][j] += 3;
  }
  break;
 case 's':
 case 'S':
 case 80:
  if (backgrand[count][i + 1][j] == 0 || \
   backgrand[count][i + 1][j] == 2)
  {  //豬運動
   backgrand[count][i][j] = backgrand[count][i][j] - 4;
   backgrand[count][i + 1][j] = backgrand[count][i + 1][j] + 4;
  }
  if (backgrand[count][i + 1][j] == 3 && (backgrand[count][i + 2][j] == 0 ||\
   backgrand[count][i + 2][j] == 2))
  {
   backgrand[count][i][j] = backgrand[count][i][j] - 4;
   backgrand[count][i + 1][j] = backgrand[count][i + 1][j] + 4;
   backgrand[count][i + 1][j] -= 3;
   backgrand[count][i + 2][j] += 3;
  }
  if (backgrand[count][i + 1][j] == 5 && \
   backgrand[count][i + 2][j] == 0)
  {
   backgrand[count][i][j] -= 4;
   backgrand[count][i + 1][j] += 4;
   backgrand[count][i + 1][j] -= 3;
   backgrand[count][i + 2][j] += 3;
  }
  break;
 case 'a':
 case 'A':
 case 75:
  if (backgrand[count][i][j - 1] == 0 || \
   backgrand[count][i][j - 1] == 2)
  {  //豬運動
   backgrand[count][i][j] = backgrand[count][i][j] - 4;
   backgrand[count][i][j - 1] = backgrand[count][i][j - 1] + 4;
  }
  if (backgrand[count][i][j - 1] == 3 &&\
   (backgrand[count][i][j - 2] == 0 ||\
   backgrand[count][i][j - 2] == 2))
  {
   backgrand[count][i][j] = backgrand[count][i][j] - 4;
   backgrand[count][i][j - 1] = backgrand[count][i][j - 1] + 4;
   backgrand[count][i][j - 1] -= 3;
   backgrand[count][i][j - 2] += 3;
  }
  if (backgrand[count][i][j - 1] == 5 && \
   backgrand[count][i][j - 2] == 0)
  {
   backgrand[count][i][j] -= 4;
   backgrand[count][i][j - 1] += 4;
   backgrand[count][i][j - 1] -= 3;
   backgrand[count][i][j - 2] += 3;
  }
  break;
 case 'd':
 case 'D':
 case 77:
  if (backgrand[count][i][j + 1] == 0 || \
   backgrand[count][i][j + 1] == 2)
  {  //豬運動
   backgrand[count][i][j] = backgrand[count][i][j] - 4;
   backgrand[count][i][j + 1] = backgrand[count][i][j + 1] + 4;
  }
  if (backgrand[count][i][j + 1] == 3 && (backgrand[count][i][j + 2] == 0 ||\
   backgrand[count][i][j + 2] == 2))
  {
   backgrand[count][i][j] -= 4;
   backgrand[count][i][j + 1] += 4;
   backgrand[count][i][j + 1] -= 3;
   backgrand[count][i][j + 2] += 3;
  }
  if (backgrand[count][i][j + 1] == 5 && backgrand[count][i][j + 2] == 0){
   backgrand[count][i][j] -= 4;
   backgrand[count][i][j + 1] += 4;
   backgrand[count][i][j + 1] -= 3;
   backgrand[count][i][j + 2] += 3;
  }
  break;
 default:
  break;
 }


}


void Initgraph(){
 initgraph(80 * ROW, 80 * COL);
 loadimage(&image1, "1.jpg");//墻
 loadimage(&image2, "2.jpg");//豬
 loadimage(&image3, "3.jpg");//空地
 loadimage(&image4, "4.jpg");//箱子
 loadimage(&image6, "6.jpg");//目的地
 loadimage(&image5, "5.jpg");//箱子到目的地
}



int main(){
 Initgraph();
 while (1){
  Backgrand();
  Keymove();
  if (Judge()){
   Backgrand();
   Sleep(500);
   count++;
  }
  if (count == MAP){
   break;
  }
 }


 getchar();
 closegraph();
 system("pause");
 return 0;
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: