900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 【C/C++】简单的程序小游戏-推箱子代码

【C/C++】简单的程序小游戏-推箱子代码

时间:2021-08-05 15:50:41

相关推荐

【C/C++】简单的程序小游戏-推箱子代码

简单的程序小游戏,推箱子代码

C语言

/*推葙子练习*/#include <stdio.h>#include <stdlib.h>#include <time.h>int main() {int arr[10][10] = {}; //记录地图信息int direction = 0;/*方向*/int stepx = 0;/*水平移动方式*/int stepy = 0;/*垂直移动方式*/int posx = 0; /*人员水平位置*/intposy = 0; /*人员垂直位置*/int row = 0;int col = 0;srand(time(0));//放置人员和葙子arr[rand() % 8 + 1][rand() % 8 + 1] = 1;//箱子的位置(在1-8之间,不允许靠边界)while (1) {posx = rand() % 10;posy = rand() % 10;if (arr[posy][posx] != 1)//人和箱子不重合跳出循环,继续下面的内容;否则继续循环直到{ //人和箱子分离。break;}}arr[posy][posx] = 2;//人的位置//显示棋盘for (row = 0;row <= 9;row++) {for (col = 0;col <= 9;col++) {if (arr[row][col] == 0) {printf("*");//空位置}else if (arr[row][col] == 1) {printf("X");//箱子}else {printf("O");//人}}printf("\n");}//每次循环获得一个方向,不一定会真正移动while (arr[0][9] != 1) {//获得移动方向printf("请输入移动方向(0代表上,1代表左,2代表下,3代表右)");scanf("%d", &direction);//使用水平,垂直方向各自的移动方式//表示移动方向if (direction == 0) {stepx = 0;stepy = -1;}else if (direction == 1) {stepx = -1;stepy = 0;}else if (direction == 2) {stepx = 0;stepy = 1;}else {stepx = 1;stepy = 0;}//移动人员位置if ((0 <= posx + stepx) && (posx + stepx <= 9) && (0 <= posy + stepy) && (posy + stepy <= 9)) { if (arr[posy + stepy][posx + stepx] == 1) {//移动后的位置是葙子if (0 <= posx + 2 * stepx && posx + 2 * stepx <= 9 && 0 <= posy + 2 * stepy && posy + 2 * stepy <= 9) {//葙子另一边还在地图上arr[posy + 2 * stepy][posx + 2 * stepx] = 1; //移动葙子(箱子的位置相对于人多两步)arr[posy + stepy][posx + stepx] = 2; //移动人arr[posy][posx] = 0; //原位置变成空posx += stepx; //修改人员水平位置posy += stepy; //修改人员垂直位置//显示棋盘for (row = 0;row <= 9;row++) {for (col = 0;col <= 9;col++) {if (arr[row][col] == 0) {printf("*");}else if (arr[row][col] == 1) {printf("X");}else {printf("O");}}printf("\n");}}}else {//移动后的位置是空arr[posy][posx] = 0;//人变成*posx += stepx;posy += stepy;arr[posy][posx] = 2;//*变成人//显示棋盘for (row = 0;row <= 9;row++) {for (col = 0;col <= 9;col++) {if (arr[row][col] == 0) {printf("*");}else if (arr[row][col] == 1) {printf("X");}else {printf("O");}}printf("\n");}}}}printf("你赢了\n");return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。