广告位联系
返回顶部
分享到

C++实现推箱子小游戏源码

C#教程 来源:互联网搜集 作者:秩名 发布时间:2019-07-12 21:02:38 人浏览
摘要

本文介绍C++实现推箱子小游戏源码 功能尚为完善。 // ConsoleApplication2.cpp : 定义控制台应用程序的入口点。// #include stdafx.h#includeiostream#includewindows.h#define KEY_DOWN(vk_code) GetAsyncKeyState(vk_code) 0x8000 ? 1 : 0using namespace

本文介绍C++实现推箱子小游戏源码

功能尚为完善。


// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
 
#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; 
//定义选关设置
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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
 { 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); //初始化玩家坐标
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;
}

效果如下:








版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/weixin_43689161/article/details/94758226
相关文章
  • WPF实现窗体亚克力效果的代码

    WPF实现窗体亚克力效果的代码
    WPF 窗体设置亚克力效果 框架使用大于等于.NET40。 Visual Studio 2022。 项目使用MIT开源许可协议。 WindowAcrylicBlur设置亚克力颜色。 Opacity设置透
  • C#非托管泄漏中HEAP_ENTRY的Size对不上解析

    C#非托管泄漏中HEAP_ENTRY的Size对不上解析
    一:背景 1. 讲故事 前段时间有位朋友在分析他的非托管泄漏时,发现NT堆的_HEAP_ENTRY的 Size 和!heap命令中的 Size 对不上,来咨询是怎么回事?
  • C#中ArrayList 类的使用介绍
    一:ArrayList 类简单说明 动态数组ArrayList类在System.Collecions的命名空间下,所以使用时要加入System.Collecions命名空间,而且ArrayList提供添加,
  • C#使用BinaryFormatter类、ISerializable接口、XmlSeriali

    C#使用BinaryFormatter类、ISerializable接口、XmlSeriali
    序列化是将对象转换成字节流的过程,反序列化是把字节流转换成对象的过程。对象一旦被序列化,就可以把对象状态保存到硬盘的某个位
  • C#序列化与反序列化集合对象并进行版本控制
    当涉及到跨进程甚至是跨域传输数据的时候,我们需要把对象序列化和反序列化。 首先可以使用Serializable特性。 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  • C#事件中关于sender的用法解读

    C#事件中关于sender的用法解读
    C#事件sender的小用法 开WPF新坑了,看了WPF的炫酷界面,再看看winForm实在是有些惨不忍睹(逃)。后面会开始写一些短的学习笔记。 一、什么
  • 在C#程序中注入恶意DLL的方法

    在C#程序中注入恶意DLL的方法
    一、背景 前段时间在训练营上课的时候就有朋友提到一个问题,为什么 Windbg 附加到 C# 程序后,程序就处于中断状态了?它到底是如何实现
  • 基于C#实现一个简单的FTP操作工具
    实现功能 实现使用FTP上传、下载、重命名、刷新、删除功能 开发环境 开发工具: Visual Studio 2013 .NET Framework版本:4.5 实现代码 1 2 3 4 5 6 7
  • C#仿QQ实现简单的截图功能

    C#仿QQ实现简单的截图功能
    接上一篇写的截取电脑屏幕,我们在原来的基础上加一个选择区域的功能,实现自定义选择截图。 个人比较懒,上一篇的代码就不重新设计
  • C#实现线性查找算法的介绍
    线性查找,肯定是以线性的方式,在集合或数组中查找某个元素。 通过代码来理解线性查找 什么叫线性?还是在代码中体会吧。 首先需要一
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计