求大神帮忙看看,怎么才能正确输出人物移动的地图?
c4droid吧
全部回复
仅看楼主
level 6
#include<stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <unistd.h>
#define H 20
#define W 20
typedef struct{
int x;
int y;
}person_t;
//定义表示方向的枚举变量
typedef enum {DIR_NULL, UP, RIGHT, DOWN, LEFT}dir_t;
typedef enum {START, PLAYING, OVER}game_state_t;
//记录那个方向键被按下了。
dir_t kdir;
//记录游戏状态
game_state_t gs;
//定义二维数组一个变量表示地图
char map[H][W];
person_t p; // 一个人
//移动人
//设置人的位置的
void set_person(person_t* p, int x, int y)
{
p->x = x;
p->y = y;
}
void input()
{
//默认任何键都没有按下,防止多次执行
kdir = DIR_NULL;
if(!_kbhit()) return;
switch(getch())
{
case '2':
kdir = UP;
break;
case '6':
kdir = RIGHT;
break;
case '8':
kdir = DOWN;
break;
case '4':
kdir = LEFT;
break;
case '0':
gs = OVER;
break;
default:
break;
}
}
void move_person(person_t* p, dir_t dir)
{
int x=0,y=0; //保存 在x y方向上的移动距离
switch(dir)
{
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
default:
break;
}
}
void update()
{ int x, y;
//我们用9 表示四周的围墙,0表示空地
for(y=0; y<H; y++)
{
for(x=0; x<W; x++)
{
//如果是数组的边缘,就设置为OBJ_WALL,也就是墙。
if(x==0 || x==W-1 || y==0 || y==H-1)
map[y][x]=9;
else
map[y][x]=0;
}
}
//根据用户按键来更新人的位置
move_person(&p, kdir);
//设置人
map[p.y][p.x] = 2;
}
void display()
{
int x, y;
system("cls");
//然后我们输出这个数组看看
for(y=0; y<H; y++)
{
for(x=0; x<W; x++)
{ if (9== map[y][x])
printf("🔴");
else if (2== map[y][x])
printf("♀");
else
printf(" ");
}
printf("\n");
}
}
//主函数
int main()
{
set_person(&p,10, 4);
// mep();
while(gs!=OVER)
{
input();
update();
display();
//buf_display();
sleep (3);
}
return 0;
}
2017年06月22日 04点06分 1
level 6
这段代码原来是一个别人写的推箱子的代码,我自己裁剪出了人物移动的代码。想好好分析下,但不知怎么才能正确写出人物移动的代码
2017年06月22日 04点06分 2
level 6
现在的情况是运行后不断打印出新的地图。人物却无法移动。。请大神们帮忙改写下正确输出地图的代码。。
2017年06月22日 04点06分 3
level 6
2017年06月22日 04点06分 4
level 6
各位大牛们帮帮忙啊。。[泪][泪][泪]
2017年06月22日 10点06分 5
level 3
2017年06月22日 13点06分 6
第24行,输出两个空格
2017年06月22日 13点06分
@奇兵小匠 可以把代码复上来吗?[啊]
2017年06月22日 13点06分
@浮华的终成空ლ 阿度吞代码[滑稽]
2017年06月22日 13点06分
@奇兵小匠 哈哈,谢谢你。不过怎么反应那么慢呢,按一下隔一段时间才能移动。😂
2017年06月22日 15点06分
level 9
2017年06月22日 15点06分 9
人物移动的确搞了我好久
2017年06月22日 15点06分
level 6
人物移动代码,上面的大神写的,我复上来,给大家参考。
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define H 20
#define L 20
int main()
{ int x,y,i,j;i=j=8;
do
{
clrscr();
for (x=0;x<H;x++)
{
for (y=0;y<L;y++)
{
if ((x==0||x==H-1)||(y==0||y==L-1))
printf("🔴");
else if ((x==i)&&(y==j))
printf("🐸");
else
printf(" ");
}
printf("\n");
}
usleep(1000);
getch();
if (getch()=='2')
i-=1;
else if (getch()=='8')
i+=1;
elseif (getch()=='4')
j-=1;
else if (getch()=='6')
j+=1;
else
{ i=i;j=j;}
}while (1);
}
2017年06月22日 15点06分 11
运行好卡。。。。
2017年06月22日 15点06分
level 9
2017年06月22日 16点06分 13
修改后可以加速运行
2017年06月22日 16点06分
@星辰阴影- 感谢星空兄的优化[真棒]
2017年06月23日 11点06分
level 9
另,地图没有设置边界,会走出地图,可以修改一下
2017年06月22日 16点06分 14
嗯嗯,谢谢大神。[真棒]
2017年06月22日 23点06分
level 11
小白可以看么
2017年06月23日 08点06分 16
看吧,我就是小白。[啊]
2017年06月23日 08点06分
level 9
2017年06月23日 13点06分 19
膜拜下大神。。[真棒][真棒][真棒]
2017年06月23日 13点06分
不过好像并没有用到二维数组吧
2017年06月23日 14点06分
@浮华的终成空ლ 这里的确没用到,但要是把它写成一个小游戏的话肯定是要用的
2017年06月23日 14点06分
level 6
哈哈,推箱子基础版,小白们可以一起学习下,人可以动。箱子推不动。
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define H 20
#define L 22
//结构体
typedef struct {
int x;
int y;
}person_;
typedef struct {
int x;
int y;
}box_;
typedef struct {
int x;
int y;
}obste_;
typedef struct {
int x;
int y;
}obste1_;
typedef struct {
int x;
int y;
}target_;
//被调用的函数
void people (person_* p,int x,int y)
{p->x=x;
p->y=y;}
void xiangzi (box_* bo,int x,int y)
{bo->x=x;
bo->y=y;}
void zhanai (obste_* obs,int x,int y)
{obs->x=x;
obs->y=y;}
void zhanai1 (obste1_* obs1,int x,int y)
{obs1->x=x;
obs1->y=y;}
void mubiao(target_* tar,int x,int y)
{tar->x=x;
tar->y=y;}
//定义地图
char map[H][L];
int i,j; //定义全局变量
//获取方向,移动人物
int getcht()
{
int move ;
getch();
move=getch();
switch(move)
{ case '2':
if (i>2) //阻止越界
i-=1;
break;
case '8':
if (i<H-2)
i+=1;
break;
case '4':
if (j>1)
j-=1;
break;
case '6':
if (j<L-2)
j+=1;
break;
}
}
//输出
void drawmap()
{ int x,y;
for (x=0;x<H;x++)
{ for (y=0;y<L;y++)
{ if ((x==i)&&(y==j))
printf("🐸");
else if (map[x][y]==9)
{ printf("🔴");
}
else if (map[x][y]==6)
{ printf("❓");
}
else if (map[x][y]==3)
{ printf("🐷");
}
else if (map[x][y]==4)
{ printf("📦");
}
else if (map[x][y]==5)
{printf("❌");
}
else if (map[x][y]==7)
{printf("❌");
}
else
{ printf(" ");
}
}
printf("\n");
}
}
//主函数
int main()
{
int x,y;
i=j=10;
//定义结构体的具体变量(名称)
person_ p;
box_ bo[4];
obste_ obs[4];
obste1_ obs1[15];
target_ tar[4];
//设置墙
for (x=0;x<H;x++)
{ for (y=0;y<L;y++)
{ if (x==0||x==H-1||y==0||y==L-1)
{ map[x][y]=9;
}else
map[x][y]=0;
}
}
//函数调用,申请为各变量地址赋坐标
people (&p,12,18);
for (x=0;x<4;x++)
{ xiangzi( &bo[x],3,x+12);
zhanai(&obs[x],x+9,x+6);
mubiao(&tar[x],x+13,x+7);
}
for (x=0;x<15;x++)
{zhanai1(&obs1[x],6,x
+3
);
}
//为具体变量赋值,用于输出时判断
map[p.x][p.y]=3;
for (x=0;x<4;x++)
{ map[bo[x].x][bo[x].y]=4;
map[obs[x].x][obs[x].y]=5;
map[tar[x].x][tar[x].y]=6;
}
for (x=0;x<15;x++)
{map[obs1[x].x][obs1[x].y]=7;
}
while (1)
{
clrscr();
drawmap();
// usleep(10);
getcht();
}
}
2017年06月23日 16点06分 22
level 9
这个代码的箱子可以推动
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define H 20
#define L 22
// 结构体
typedef struct {
int x;
int y;
} person_;
typedef struct {
int x;
int y;
} box_;
typedef struct {
int x;
int y;
} obste_;
typedef struct {
int x;
int y;
} obste1_;
typedef struct {
int x;
int y;
} target_;
// 被调用的函数
void people(person_ * p, int x, int y) {
p->x = x;
p->y = y;
}
void xiangzi(box_ * bo, int x, int y) {
bo->x = x;
bo->y = y;
}
void zhanai(obste_ * obs, int x, int y) {
obs->x = x;
obs->y = y;
}
void zhanai1(obste1_ * obs1, int x, int y) {
obs1->x = x;
obs1->y = y;
}
void mubiao(target_ * tar, int x, int y) {
tar->x = x;
tar->y = y;
}
// 定义地图
char map[H][L];
int i, j; // 定义全局变量
// 获取方向,移动人物
int getcht() {
int move;
move = getch();
switch (move) {
case '2':
if (i > 1)// 阻止越界
{
if(i>2&&map[i-1][j]==4)
{
map[i-2][j]=4;
map[i-1][j]=0;
}
i -= 1;
}
break;
case '8':
if(i<H-3&&map[i+1][j]==4)
{
map[i+2][j]=4;
map[i+1][j]=0;
}
if (i < H - 2)
i += 1;
break;
case '4':
if(j>2&&map[i][j-1]==4)
{
map[i][j-2]=4;
map[i][j-1]=0;
}
if (j > 1)
j -= 1;
break;
case '6':
if(j<L-3&&map[i][j+1]==4)
{
map[i][j+2]=4;
map[i][j+1]=0;
}
if (j < L - 2)
j += 1;
break;
}
}
// 输出
void drawmap() {
int x, y;
for (x = 0; x < H; x++) {
for (y = 0; y < L; y++) {
if ((x == i) && (y == j))
printf("🐸");
else if (map[x][y] == 9) {
printf("🔴");
} else if (map[x][y] == 6) {
printf("❓");
} else if (map[x][y] == 3) {
printf("🐷");
} else if (map[x][y] == 4) {
printf("📦");
} else if (map[x][y] == 5) {
printf("❌");
} else if (map[x][y] == 7) {
printf("❌");
} else {
printf("⬛");
}
}
printf("\n");
}
}
// 主函数在楼中楼
2017年06月24日 15点06分 23
主函数在楼下
2017年06月24日 15点06分
level 9
int main() {
int x, y;
i = j = 10;
// 定义结构体的具体变量(名称)
person_ p;
box_ bo[4];
obste_ obs[4];
obste1_ obs1[15];
target_ tar[4];
// 设置墙
for (x = 0; x < H; x++) {
for (y = 0; y < L; y++) {
if (x==0||x==H-1||y==0||y==L-1)
map[x][y] = 9;
else
map[x][y] = 0;
}
}
// 函数调用,申请为各变量地址赋坐标
people(&p, 12, 18);
for (x = 0; x < 4; x++) {
xiangzi(&bo[x], 3, x + 12);
zhanai(&obs[x], x + 9, x + 6);
mubiao(&tar[x], x + 13, x + 7);
}
for (x = 0; x < 15; x++) {
zhanai1(&obs1[x], 6, x + 3);
}
// 为具体变量赋值,用于输出时判断
map[p.x][p.y] = 3;
for (x = 0; x < 4; x++) {
map[bo[x].x][bo[x].y] = 4;
map[obs[x].x][obs[x].y] = 5;
map[tar[x].x][tar[x].y] = 6;
}
for (x = 0; x < 15; x++) {
map[obs1[x].x][obs1[x].y] = 7;
}
while (1) {
clrscr();
drawmap();
// usleep(10);
getcht();
}
}
2017年06月24日 15点06分 24
[真棒][真棒][真棒]
2017年06月24日 23点06分
1 2 尾页