您好,登錄后才能下訂單哦!
這篇文章主要介紹了C++如何實(shí)現(xiàn)推箱子小游戲,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體內(nèi)容如下
功能尚為完善。
// ConsoleApplication2.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 // #include "stdafx.h" #include<iostream> #include<windows.h> #define KEY_DOWN(vk_code) GetAsyncKeyState(vk_code) & 0x8000 ? 1 : 0 using namespace std; void Map(); void PlayerMove(); void Menu(); void BoxMove(); //extern int Array[15][20]; //定義菜單狀況 int nMenuState = 1; //定義選關(guān)設(shè)置 int nSetState = 1; //定義關(guān)卡狀態(tài) int PassState = 1; //定義選關(guān)情況 bool bslect = false; int Array1[15][20] = { { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1 }, { 1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1 }, { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, }; int Array[15][20] = {}; struct Player { int P_row; int P_col; int P_rowBk; int P_colBk; Player() { P_row = 0; P_col = 0; P_rowBk = 0; P_colBk = 0; } Player(int x = 10, int y = 10) { P_row = x; P_col = y; P_rowBk = x; P_colBk = y; } void BackUp() { P_rowBk = P_row; P_colBk = P_col; } void restore() { P_row = P_rowBk; P_col = P_colBk; } }; //Player splayer(1, 1); //初始化玩家坐標(biāo) Player splayer = {1,1}; struct Box { int box_x; int box_y; int box_xx;//用來(lái)備份坐標(biāo) int box_yy; Box() { box_x = 0; box_y = 0; box_xx = 0; box_yy = 0; } Box(int x = 10, int y = 10) { box_x = x; box_y = y; box_xx = box_x; box_yy = box_y; } void BackUp() { box_xx = box_x; box_yy = box_y; } void restore() { box_x = box_xx; box_y = box_yy; } }; //第一關(guān)箱子坐標(biāo)初始化 Box sbox[3] = { { 5,5 }, { 6,6 }, { 7,7 } }; //坑結(jié)構(gòu)體 struct Destinate { int Des_x; int Des_y; Destinate(int a, int b) { Des_x = a; Des_y = b; } }; Destinate sdestinate[3] = { { 8,8 }, { 9,9 }, { 10,10 } }; //選關(guān)設(shè)置 void SetChoise() { while (1) { system("cls"); if (KEY_DOWN(VK_UP)) { //cout << "向上被按下" << endl; --nSetState; if (nSetState < 1) { nSetState = 3; } } if (KEY_DOWN(VK_DOWN)) { //cout << "向下被按下" << endl; ++nSetState; if (nSetState > 3) { nSetState = 1; } } switch (nSetState) { case 1: cout << "********游戲選關(guān)設(shè)置********" << endl; cout << " -> 1、關(guān)卡一" << endl; cout << " 2、關(guān)卡二" << endl; cout << " 3、關(guān)卡三" << endl; system("pause"); break; case 2: cout << "********游戲選關(guān)設(shè)置********" << endl; cout << " 1、關(guān)卡一" << endl; cout << " -> 2、關(guān)卡二" << endl; cout << " 3、關(guān)卡三" << endl; system("pause"); break; case 3: cout << "********游戲選關(guān)設(shè)置********" << endl; cout << " 1、關(guān)卡一" << endl; cout << " 2、關(guān)卡二" << endl; cout << " -> 3、關(guān)卡三" << endl; system("pause"); break; } if (KEY_DOWN(VK_RETURN)) { if (1 == nSetState) { memcpy(Array, Array1, sizeof(Array)); splayer.P_row = 1; splayer.P_col = 1; PassState = 1; for (int i = 0; i < 3; i++) { sbox[i].box_x = 5 + i; sbox[i].box_y = 5 + i; sdestinate[i].Des_x = 8 + i; sdestinate[i].Des_y = 8 + i; } } else if (2 == nSetState) { memcpy(Array, Array2, sizeof(Array)); splayer.P_row = 1; splayer.P_col = 1; PassState = 2; for (int i = 0; i < 3; i++) { sbox[i].box_x = 5 + i; sbox[i].box_y = 5 + i; sdestinate[i].Des_x = 3 + i; sdestinate[i].Des_y = 3; } } else if (3 == nSetState) { memcpy(Array, Array3, sizeof(Array)); splayer.P_row = 1; splayer.P_col = 1; PassState = 3; memcpy(Array, Array3, sizeof(Array)); for (int i = 0; i < 3; i++) { sbox[i].box_x = 10; sbox[i].box_y = 7 + i; sdestinate[i].Des_x = 2 + i; sdestinate[i].Des_y = 2; } } bslect = true; break; } } } //判斷玩家移動(dòng) void PlayerMove() { splayer.BackUp(); if (KEY_DOWN(VK_UP)) { --splayer.P_row; } if (KEY_DOWN(VK_DOWN)) { ++splayer.P_row; } if (KEY_DOWN(VK_LEFT)) { --splayer.P_col; } if (KEY_DOWN(VK_RIGHT)) { ++splayer.P_col; } if (1 == Array[splayer.P_row][splayer.P_col]) {//判斷玩家是否越墻 splayer.restore(); } } //判斷箱子移動(dòng) void BoxMove() { for (int i = 0; i < 3; i++) { sbox[i].BackUp(); } int skew_x = 0; //臨時(shí)偏移量 int skew_y = 0; for (int s = 0; s < 3; s++) { //利用坐標(biāo)偏移量求箱子新位置 if (splayer.P_row == sbox[s].box_x && splayer.P_col == sbox[s].box_y) { skew_x = sbox[s].box_x - splayer.P_rowBk; skew_y = sbox[s].box_y - splayer.P_colBk; sbox[s].box_x = sbox[s].box_x + skew_x; sbox[s].box_y = sbox[s].box_y + skew_y; /*sbox[s].restore();*/ } } for (int a = 0; a < 3; a++) { for (int b = 0; b < 3; b++) { if (a != b && (sbox[a].box_x == sbox[b].box_x && sbox[a].box_y == sbox[b].box_y)) { splayer.restore(); sbox[a].restore(); } } } for (int s = 0; s < 3; s++) { //判斷箱子是否越界 if (1 == Array[sbox[s].box_x][sbox[s].box_y]) { splayer.restore(); sbox[s].restore(); } } } //判斷勝利 void JudgeWin() { int nCorrectNum = 0; //判斷箱子推到正確位置的數(shù)量 for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (sdestinate[i].Des_x == sbox[j].box_x && sdestinate[i].Des_y == sbox[j].box_y) { ++nCorrectNum; break; } } if (3 <= nCorrectNum) { //system("cls"); cout << "You Win!" << endl; ++PassState; if (2 == PassState) { memcpy(Array, Array2, sizeof(Array)); //更新玩家位置 splayer.P_row = 1; splayer.P_col = 1; //更新箱子和坑狀態(tài) for (int i = 0; i < 3; i++) { sbox[i].box_x = 5 + i; sbox[i].box_y = 5 + i; sdestinate[i].Des_x = 3 + i; sdestinate[i].Des_y = 3; } } if (3 == PassState) { splayer.P_row = 1; splayer.P_col = 1; memcpy(Array, Array3, sizeof(Array)); for (int i = 0; i < 3; i++) { sbox[i].box_x = 10; sbox[i].box_y = 7 + i; sdestinate[i].Des_x = 2 + i; sdestinate[i].Des_y = 2; } } //system("pause"); break; } /*system("pause");*/ } } //打印游戲菜單 void Menu() { if (KEY_DOWN(VK_UP)) { //cout << "向上被按下" << endl; --nMenuState; if (nMenuState < 1) { nMenuState = 3; } } if (KEY_DOWN(VK_DOWN)) { //cout << "向下被按下" << endl; ++nMenuState; if (nMenuState > 3) { nMenuState = 1; } } switch (nMenuState) { case 1: system("cls"); cout << "********游戲界面菜單********" << endl; cout << " -> 1、游戲開(kāi)始" << endl; cout << " 2、游戲設(shè)置" << endl; cout << " 3、退出游戲" << endl << endl; cout << " ->按小鍵盤(pán)↑↓←→移動(dòng)<- " << endl; cout << " ->按F1 F2 F3可切換關(guān)卡<- " << endl; system("pause"); break; case 2: system("cls"); cout << "********游戲界面菜單********" << endl; cout << " 1、游戲開(kāi)始" << endl; cout << " -> 2、游戲設(shè)置" << endl; cout << " 3、退出游戲" << endl << endl; cout << " ->按小鍵盤(pán)↑↓←→移動(dòng)<- " << endl; cout << " ->按F1 F2 F3可切換關(guān)卡<- " << endl; system("pause"); break; case 3: system("cls"); cout << "********游戲界面菜單********" << endl; cout << " 1、游戲開(kāi)始" << endl; cout << " 2、游戲設(shè)置" << endl; cout << " -> 3、退出游戲" << endl << endl; cout << " ->按小鍵盤(pán)↑↓←→移動(dòng)<- " << endl; cout << " ->按F1 F2 F3可切換關(guān)卡<- " << endl; system("pause"); break; } } void Map() { while (1) { system("cls"); PlayerMove(); BoxMove(); JudgeWin(); if (4 == PassState) { cout << "恭喜通關(guān)" << endl; PassState = 1; memcpy(Array, Array1, sizeof(Array)); //更新玩家位置 splayer.P_row = 1; splayer.P_col = 1; //更新箱子和坑狀態(tài) for (int i = 0; i < 3; i++) { sbox[i].box_x = 5 + i; sbox[i].box_y = 5 + i; sdestinate[i].Des_x = 8 + i; sdestinate[i].Des_y = 8 + i; } system("pause"); break; } if (KEY_DOWN(VK_F1)) { memcpy(Array, Array1, sizeof(Array)); PassState = 1; //更新玩家位置 splayer.P_row = 1; splayer.P_col = 1; //更新箱子和坑狀態(tài) for (int i = 0; i < 3; i++) { sbox[i].box_x = 5 + i; sbox[i].box_y = 5 + i; } } if (KEY_DOWN(VK_F2)) { memcpy(Array, Array2, sizeof(Array)); PassState = 2; //更新玩家位置 splayer.P_row = 1; splayer.P_col = 1; //更新箱子和坑狀態(tài) for (int i = 0; i < 3; i++) { sbox[i].box_x = 3 + i; sbox[i].box_y = 5; sdestinate[i].Des_x = 3 + i; sdestinate[i].Des_y = 3; } } if (KEY_DOWN(VK_F3)) { memcpy(Array, Array3, sizeof(Array)); PassState = 3; //更新玩家位置 splayer.P_row = 1; splayer.P_col = 1; //更新箱子和坑狀態(tài) for (int i = 0; i < 3; i++) { sbox[i].box_x = 10; sbox[i].box_y = 7 + i; sdestinate[i].Des_x = 2 + i; sdestinate[i].Des_y = 2; } } //打印地圖 for (int i = 0; i < 15; i++) { for (int j = 0; j < 20; j++) { bool bDrawBox = false; //判斷箱子 for (int m = 0; m < 3; m++) { if (i == sbox[m].box_x && j == sbox[m].box_y) { bDrawBox = true; break; } } bool bDrawDes = false; //判斷坑 for (int m = 0; m < 3; m++) { if (i == sdestinate[m].Des_x && j == sdestinate[m].Des_y) { bDrawDes = true; break; } } if (1 == Array[i][j]) { cout << "■"; } else if (i == splayer.P_row && j == splayer.P_col) { cout << "♀"; } else if (true == bDrawBox) { //將true放在前面,不然出現(xiàn)全是箱子都不知道怎么錯(cuò)的…… cout << "箱"; } else if (true == bDrawDes) { cout << "坑"; } else { cout << " "; } } cout << endl; } system("pause"); } } int _tmain(int argc, _TCHAR* argv[]) { memcpy(Array, Array1, sizeof(Array)); while (1) { //if (KEY_DOWN(VK_RETURN)){ // if (1 == nMenuState) { // Map(); // } // else if (2 == nMenuState) { // SetChoise(); // /* cout << "wwww"; // system("pause");*/ // } // else if (3 == nMenuState) // return 0; //} if (3 == nMenuState && KEY_DOWN(VK_RETURN)) { //可以 return 0; } else if (2 == nMenuState && KEY_DOWN(VK_RETURN)) { SetChoise(); if (bslect) { Map(); } } else if (1 == nMenuState && KEY_DOWN(VK_RETURN)) { Map(); } //if (KEY_DOWN(VK_RETURN) && 3 == nMenuState) { //KEY_DOWN(VK_RETURN)不能進(jìn)行與或運(yùn)算 ?? // return 0; //} //else if (KEY_DOWN(VK_RETURN) && 1 == nMenuState) { // Map(); //} else { Menu(); } } //Map(); system("pause"); return 0; }
代碼效果:
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C++如何實(shí)現(xiàn)推箱子小游戲”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。