您好,登錄后才能下訂單哦!
這篇文章主要介紹“C++實現推箱子小游戲代碼分享”,在日常操作中,相信很多人在C++實現推箱子小游戲代碼分享問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++實現推箱子小游戲代碼分享”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
代碼如下:
// ConsoleApplication2.cpp : 定義控制臺應用程序的入口點。//#include "stdafx.h"#include<iostream>#include<windows.h>#define KEY_DOWN(vk_code) GetAsyncKeyState(vk_code) & 0x8000 ? 1 : 0using namespace std;void Map();void PlayerMove();void Menu();void BoxMove();//extern int Array[15][20];//定義菜單狀況int nMenuState = 1; //定義選關設置int nSetState = 1;//定義關卡狀態int PassState = 1;//定義選關情況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,1 }, { 1,1,1,1,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{ 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); //初始化玩家坐標Player splayer = {1,1};struct Box { int box_x; int box_y; int box_xx;//用來備份坐標 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; }};//第一關箱子坐標初始化Box sbox[3] = { { 5,5 }, { 6,6 }, { 7,7 }};//坑結構體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 }};//選關設置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 << "********游戲選關設置********" << endl; cout << " -> 1、關卡一" << endl; cout << " 2、關卡二" << endl; cout << " 3、關卡三" << endl; system("pause"); break; case 2: cout << "********游戲選關設置********" << endl; cout << " 1、關卡一" << endl; cout << " -> 2、關卡二" << endl; cout << " 3、關卡三" << endl; system("pause"); break; case 3: cout << "********游戲選關設置********" << endl; cout << " 1、關卡一" << endl; cout << " 2、關卡二" << endl; cout << " -> 3、關卡三" << 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; } }}//判斷玩家移動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(); }}//判斷箱子移動void BoxMove() { for (int i = 0; i < 3; i++) { sbox[i].BackUp(); } int skew_x = 0; //臨時偏移量 int skew_y = 0; for (int s = 0; s < 3; s++) { //利用坐標偏移量求箱子新位置 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; //判斷箱子推到正確位置的數量 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; //更新箱子和坑狀態 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、游戲開始" << endl; cout << " 2、游戲設置" << endl; cout << " 3、退出游戲" << endl << endl; cout << " ->按小鍵盤↑↓←→移動<- " << endl; cout << " ->按F1 F2 F3可切換關卡<- " << endl; system("pause"); break; case 2: system("cls"); cout << "********游戲界面菜單********" << endl; cout << " 1、游戲開始" << endl; cout << " -> 2、游戲設置" << endl; cout << " 3、退出游戲" << endl << endl; cout << " ->按小鍵盤↑↓←→移動<- " << endl; cout << " ->按F1 F2 F3可切換關卡<- " << endl; system("pause"); break; case 3: system("cls"); cout << "********游戲界面菜單********" << endl; cout << " 1、游戲開始" << endl; cout << " 2、游戲設置" << endl; cout << " -> 3、退出游戲" << endl << endl; cout << " ->按小鍵盤↑↓←→移動<- " << endl; cout << " ->按F1 F2 F3可切換關卡<- " << endl; system("pause"); break; }}void Map() { while (1) { system("cls"); PlayerMove(); BoxMove(); JudgeWin(); if (4 == PassState) { cout << "恭喜通關" << endl; PassState = 1; memcpy(Array, Array1, sizeof(Array)); //更新玩家位置 splayer.P_row = 1; splayer.P_col = 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; } system("pause"); break; } if (KEY_DOWN(VK_F1)) { memcpy(Array, Array1, sizeof(Array)); PassState = 1; //更新玩家位置 splayer.P_row = 1; splayer.P_col = 1; //更新箱子和坑狀態 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; //更新箱子和坑狀態 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; //更新箱子和坑狀態 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放在前面,不然出現全是箱子都不知道怎么錯的…… 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)不能進行與或運算 ?? // return 0; //} //else if (KEY_DOWN(VK_RETURN) && 1 == nMenuState) { // Map(); //} else { Menu(); } } //Map(); system("pause"); return 0;}
到此,關于“C++實現推箱子小游戲代碼分享”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。