C++如何實(shí)現(xiàn)推箱子小游戲

這篇文章主要介紹了C++如何實(shí)現(xiàn)推箱子小游戲,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

公司主營業(yè)務(wù):成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出坊子免費(fèi)做網(wǎng)站回饋大家。

具體內(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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1,0,0,0,0,0,1,1,1,1,1,1,1,1,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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
};
int Array3[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,1,1,1,1,1,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,0,0,0,0,1,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;//用來備份坐標(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、游戲開始" << endl;
 cout << " 2、游戲設(shè)置" << endl;
 cout << " 3、退出游戲" << endl << endl;
 cout << " ->按小鍵盤↑↓←→移動(dòng)<- " << endl;
 cout << " ->按F1 F2 F3可切換關(guān)卡<- " << endl;
 system("pause");
 break;
 case 2: system("cls");
 cout << "********游戲界面菜單********" << endl;
 cout << " 1、游戲開始" << endl;
 cout << " -> 2、游戲設(shè)置" << endl;
 cout << " 3、退出游戲" << endl << endl;
 cout << " ->按小鍵盤↑↓←→移動(dòng)<- " << endl;
 cout << " ->按F1 F2 F3可切換關(guān)卡<- " << endl;
 system("pause");
 break;
 case 3: system("cls");
 cout << "********游戲界面菜單********" << endl;
 cout << " 1、游戲開始" << endl;
 cout << " 2、游戲設(shè)置" << endl;
 cout << " -> 3、退出游戲" << endl << endl;
 cout << " ->按小鍵盤↑↓←→移動(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;
}

代碼效果:

C++如何實(shí)現(xiàn)推箱子小游戲

C++如何實(shí)現(xiàn)推箱子小游戲

C++如何實(shí)現(xiàn)推箱子小游戲

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C++如何實(shí)現(xiàn)推箱子小游戲”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

新聞名稱:C++如何實(shí)現(xiàn)推箱子小游戲
本文鏈接:http://muchs.cn/article46/gehoeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)外貿(mào)建站、網(wǎng)站排名、服務(wù)器托管、用戶體驗(yàn)

廣告

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

微信小程序開發(fā)