用C语言编写推箱子游戏

 更新时间:2019年10月21日 14:50:45   作者:Pastthewind  
这篇文章主要为大家详细介绍了用C语言编写推箱子游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言推箱子游戏的具体实现代码,供大家参考,具体内容如下

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <conio.h>
//行和列 
#define ROW 10
#define COL 11
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/**
*
*
*/
//地图
char map[ROW][COL] = {
 "##########",//0
 "###  ##",//1
 "###  ##",//2
 "##AX # ##",//3
 "### ## ",//4
 "##### #",//5
 "##  #",//6
 "#  ####",//7
 "###  ",//8
 "##########" //9
 //A:人 , X:箱子 
 } ;
//打印地图 
void showMap();
//接收小人的方向
char enterDirection();
 
//小人向上移动的方法
void moveToUp(); 
//小人向下移动的方法
void moveToDown(); 
//小人向右移动的方法
void moveToRight(); 
//小人向左移动的方法
void moveToLeft(); 
 
//当前小人的坐标
int currentPersonRow = 3;
int currentPersonCol = 2;
//当前箱子的坐标 
int currentBoxRow = 3;
int currentBoxCol = 3;
 
 
 
int main(int argc, char *argv[]) {
 //system("clear");
 printf("点击回车键开始游戏 ^_^\n\n");
 //1代表运行 0停止 
 int flag = 1;
 while(flag==1){
 //显示地图 
 showMap();
 //接收小人的方向
 char dir = enterDirection();
 switch(dir){
  //小人向上移动 
  case 'w':
  case 'W':
   moveToUp();
  break;
  
  //小人向下移动 
  case 's':
  case 'S':
   moveToDown();
  break;
  //小人向右移动 
  case 'd':
  case 'D':
   moveToRight();
  break;
  //小人向左移动 
  case 'a':
  case 'A':
   moveToLeft();
  break;
  //停止运行 
  case 'q':
  case 'Q':
   printf("你的智商真低!T_T\n");
   flag = 0;
  break;
 }
 showMap();
 if(currentBoxRow==8&¤tBoxCol==9){
  printf("你的智商真高^_^!!!");
  flag = 0; 
  }
 
}
 
}
/*
方法的实现 
*/
 
 
//打印地图 
void showMap(){
 int i;
 for(i = 0;i < ROW; i++){
  printf("%s\n",map[i]);
 }
 printf("\n\n\n\n\n"); 
 printf("W:上,S:下, A:左, D:右。Q:退出");
 printf("\n\n\n\n\n");
}
//接收小人的方向
char enterDirection(){
 //清除SCANF中的缓冲区 
 rewind(stdin);
 char dir;
 dir = getch();
 //scanf("%c",&dir);
 return dir;
}
//小人向上移动的方法
void moveToUp(){
 //小人的下一个坐标 
 int nextPersonCol = currentPersonCol;
 int nextPersonRow = currentPersonRow - 1;
 //箱子的下一个坐标
 int nextBoxRow = currentBoxRow - 1;
 int nextBoxCol = currentBoxCol; 
 
 //如果小人的下一个坐标是路 
 if(map[nextPersonRow][nextPersonCol]==' '){
 map[nextPersonRow][nextPersonCol] = 'A';
 map[currentPersonRow][currentPersonCol] = ' ';
 currentPersonRow = nextPersonRow;
 currentPersonCol = nextPersonCol;
 }
 //如果小人的下一个坐标是墙 
 if(map[nextPersonRow][nextPersonCol]=='#'){
  //什么也不做 
 }
 //如果小人的下一个坐标是箱子 
 if(map[nextPersonRow][nextPersonCol]=='X'){
  if(map[nextBoxRow][nextBoxCol] == ' '){
  
  map[nextPersonRow][nextPersonCol] = 'A';
  map[currentPersonRow][currentPersonCol] = ' ';
  
  map[nextBoxRow][nextBoxCol] = 'X';
  map[currentBoxRow][currentBoxCol] = 'A';
 
  
  currentPersonRow = nextPersonRow;
  currentPersonCol = nextPersonCol;
  currentBoxRow = nextBoxRow;
  currentBoxCol = nextBoxCol;
 }
 }
}
//小人向下移动的方法
void moveToDown(){
  //小人的下一个坐标 
 int nextPersonCol = currentPersonCol;
 int nextPersonRow = currentPersonRow + 1;
 //箱子的下一个坐标
 int nextBoxRow = currentBoxRow + 1;
 int nextBoxCol = currentBoxCol; 
 
 //如果小人的下一个坐标是路 
 if(map[nextPersonRow][nextPersonCol]==' '){
 map[nextPersonRow][nextPersonCol] = 'A';
 map[currentPersonRow][currentPersonCol] = ' ';
 currentPersonRow = nextPersonRow;
 currentPersonCol = nextPersonCol;
 }
 //如果小人的下一个坐标是墙 
 if(map[nextPersonRow][nextPersonCol]=='#'){
  //什么也不做 
 }
 //如果小人的下一个坐标是箱子 
 if(map[nextPersonRow][nextPersonCol]=='X'){
  if(map[nextBoxRow][nextBoxCol] == ' '){
  
  map[nextPersonRow][nextPersonCol] = 'A';
  map[currentPersonRow][currentPersonCol] = ' ';
  
  map[nextBoxRow][nextBoxCol] = 'X';
  map[currentBoxRow][currentBoxCol] = 'A';
  
  currentPersonRow = nextPersonRow;
  currentPersonCol = nextPersonCol;
  currentBoxRow = nextBoxRow;
  currentBoxCol = nextBoxCol;
 }
 }
} 
//小人向右移动的方法
void moveToRight(){
 //小人的下一个坐标 
 int nextPersonCol = currentPersonCol + 1;
 int nextPersonRow = currentPersonRow;
 //箱子的下一个坐标
 int nextBoxRow = currentBoxRow;
 int nextBoxCol = currentBoxCol + 1; 
 
 //如果小人的下一个坐标是路 
 if(map[nextPersonRow][nextPersonCol]==' '){
 map[nextPersonRow][nextPersonCol] = 'A';
 map[currentPersonRow][currentPersonCol] = ' ';
 currentPersonRow = nextPersonRow;
 currentPersonCol = nextPersonCol;
 }
 //如果小人的下一个坐标是墙 
 if(map[nextPersonRow][nextPersonCol]=='#'){
  //什么也不做 
 }
 //如果小人的下一个坐标是箱子 
 if(map[nextPersonRow][nextPersonCol]=='X'){
  if(map[nextBoxRow][nextBoxCol]==' '){
  
  map[nextPersonRow][nextPersonCol] = 'A';
  map[currentPersonRow][currentPersonCol] = ' ';
  
  map[nextBoxRow][nextBoxCol] = 'X';
  map[currentBoxRow][currentBoxCol] = 'A';
  
  currentPersonRow = nextPersonRow;
  currentPersonCol = nextPersonCol;
  currentBoxRow = nextBoxRow;
  currentBoxCol = nextBoxCol;
 }
 }
}
//小人向左移动的方法
void moveToLeft(){
 //小人的下一个坐标 
 int nextPersonCol = currentPersonCol - 1;
 int nextPersonRow = currentPersonRow;
 //箱子的下一个坐标
 int nextBoxRow = currentBoxRow;
 int nextBoxCol = currentBoxCol - 1; 
 
 //如果小人的下一个坐标是路 
 if(map[nextPersonRow][nextPersonCol]==' '){
 map[nextPersonRow][nextPersonCol] = 'A';
 map[currentPersonRow][currentPersonCol] = ' ';
 currentPersonRow = nextPersonRow;
 currentPersonCol = nextPersonCol;
 }
 //如果小人的下一个坐标是墙 
 if(map[nextPersonRow][nextPersonCol]=='#'){
  //什么也不做 
 }
 //如果小人的下一个坐标是箱子 
 if(map[nextPersonRow][nextPersonCol]=='X'){
  if(map[nextBoxRow][nextBoxCol]==' '){
  map[nextPersonRow][nextPersonCol] = 'A';
  map[currentPersonRow][currentPersonCol] = ' ';
  
  map[nextBoxRow][nextBoxCol] = 'X';
  map[currentBoxRow][currentBoxCol] = 'A';
  
  currentPersonRow = nextPersonRow;
  currentPersonCol = nextPersonCol;
  currentBoxRow = nextBoxRow;
  currentBoxCol = nextBoxCol;
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 重学c/c++之数据存储详解(整数、浮点数)

    重学c/c++之数据存储详解(整数、浮点数)

    C语言给定了一些基本的数据类型,下面这篇文章主要给大家介绍了关于重学c/c++之数据存储(整数、浮点数)的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • linux下c语言中隐藏进程命令行参数(例如输入密码等高危操作)

    linux下c语言中隐藏进程命令行参数(例如输入密码等高危操作)

    启动程序很多时候用命令行参数可以很方便,做到简化一些配置,但是输入用户名密码等操作,如果通过进程查看工具直接看到密码就太不安全了,这里就为大家分享一下方法
    2021-01-01
  • C++设计模式之访问者模式

    C++设计模式之访问者模式

    这篇文章主要介绍了C++设计模式之访问者模式,本文讲解了什么是访问者模式、访问者模式的UML类图、访问者模式的实现代码等内容,需要的朋友可以参考下
    2014-10-10
  • c++ 入门——浅析构造函数和析构函数

    c++ 入门——浅析构造函数和析构函数

    这篇文章主要介绍了c++ 浅析构造函数和析构函数的相关资料,帮助大家入门c++ 编程,感兴趣的朋友可以了解下
    2020-08-08
  • 总结C/C++面试中可能会碰到的字符串指针题

    总结C/C++面试中可能会碰到的字符串指针题

    C/C++是最能体现程序员能力的语言之一,其功能强大,在IT行业的各个方面都有大量的应用。下面这篇文章主要介绍了总结了在C/C++面试中可能会碰到的字符串指针题,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-01-01
  • 详解C++中的四种类型转换运算符

    详解C++中的四种类型转换运算符

    隐式类型转换是安全的,显式类型转换是有风险的,C语言之所以增加强制类型转换的语法,就是为了强调风险,让程序员意识到自己在做什么,本文将给大家详细介绍一下C++中的四种类型转换运算符,需要的朋友可以参考下
    2023-09-09
  • C语言基础知识点指针的使用

    C语言基础知识点指针的使用

    这篇文章主要介绍了C语言基础知识点指针的使用,下面文章将让我们掌握指针的概念和用法、指针与数组之间的关系、指针指向的指针、如何使用指针变量做函数参数等更多相关内容,需要的小伙伴可以参考一下
    2022-03-03
  • 基于C语言实现扫雷游戏

    基于C语言实现扫雷游戏

    这篇文章主要为大家详细介绍了基于C语言实现扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • c++实现图像像素计算的示例详解

    c++实现图像像素计算的示例详解

    我们知道每张图像都能够用矩阵来表示,矩阵中每个元素的值表示了图像中每个像素值,像素值的大小就对应着图像的亮暗,本文主要来和大家介绍一下C++进行图像像素计算的相关知识,感兴趣的可以了解下
    2023-12-12
  • C++ select模型简单聊天室的实现示例

    C++ select模型简单聊天室的实现示例

    本文主要介绍了C++ select模型简单聊天室的实现示例,使用CMake项目进行开发,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05

最新评论