C++實(shí)現(xiàn)推箱子小游戲源碼
本文實(shí)例為大家分享了C++實(shí)現(xiàn)推箱子小游戲的具體代碼,供大家參考,具體內(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,1 },
{ 1,1,1,1,1,1,1,1,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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;//用來(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、游戲開始" << 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;
}
代碼效果:



關(guān)于C++小游戲的更多精彩內(nèi)容請(qǐng)點(diǎn)擊專題: 《C++經(jīng)典小游戲》 學(xué)習(xí)了解
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Matlab實(shí)現(xiàn)鯨魚優(yōu)化算法的示例代碼
鯨魚優(yōu)化算法(WOA)是澳大利亞學(xué)者M(jìn)irjaili等于2016年提出的群體智能優(yōu)化算法,根據(jù)座頭鯨的捕獵行為實(shí)現(xiàn)優(yōu)化搜索的目的。本文將利用Matlab實(shí)現(xiàn)這一算法,需要的可以參考一下2022-04-04
C++使用標(biāo)準(zhǔn)庫(kù)實(shí)現(xiàn)事件和委托以及信號(hào)和槽機(jī)制
這篇文章主要為大家詳細(xì)介紹了C++如何使用標(biāo)準(zhǔn)庫(kù)實(shí)現(xiàn)事件和委托以及信號(hào)和槽機(jī)制,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-11-11
c語(yǔ)言函數(shù)棧幀的創(chuàng)建和銷毀過(guò)程詳解
我們知道c語(yǔ)言中函數(shù)都是被調(diào)用的,main函數(shù)里面能調(diào)用其他函數(shù),其實(shí)main函數(shù)也是被別的函數(shù)調(diào)用的,下面通過(guò)本文給大家分享c語(yǔ)言函數(shù)棧幀的創(chuàng)建和銷毀過(guò)程,一起看看吧2021-08-08
Qt實(shí)現(xiàn)兩個(gè)獨(dú)立窗口的信號(hào)通信
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)兩個(gè)獨(dú)立窗口的信號(hào)通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C++ Boost Chrono實(shí)現(xiàn)計(jì)時(shí)碼表流程詳解
Boost是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱。Boost庫(kù)是一個(gè)可移植、提供源代碼的C++庫(kù),作為標(biāo)準(zhǔn)庫(kù)的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱2022-11-11
解析C++中臨時(shí)對(duì)象的產(chǎn)生情況
臨時(shí)對(duì)象的產(chǎn)生和銷毀都是有成本的,都會(huì)影響程序的執(zhí)行性能和效率,所以如果能了解臨時(shí)對(duì)象產(chǎn)生的原因,就可以提升程序的性能和效率,下面小編就來(lái)和大家聊聊臨時(shí)對(duì)象產(chǎn)生的幾種情況吧2023-06-06
C++實(shí)現(xiàn)字符格式相互轉(zhuǎn)換的示例代碼
這篇文章主要為大家詳細(xì)介紹了C++中實(shí)現(xiàn)字符格式相互轉(zhuǎn)換的方法,主要有UTF8與string互轉(zhuǎn)、wstring與string互轉(zhuǎn),感興趣的小伙伴可以了解一下2022-11-11

