我正在用 C 语言制作 Roguelike 游戏,但我无法让我的角色按照我想要的方式移动。我在点 (x, y) 处创建了一个带有字符的 2D 字符数组,绘制了数组,并更改了 x 和 y 值,并在输入要前进的方向时重新绘制了数组(有点像 Zork,但有图形)。但这不符合我的计划。该代码将解释更多:
/* game.h (header file for globals) */
#define GAME_H
char character = '@';
//char monster = '&';
int x = 2;
int y = 2;
/* my beginning floor
(will not be implemented, just for testing movement) */
char floor[10][6] = { /* 219 = filled block, 32 = space */
219, 219, 219, 219, 219, 219, 219, 219, 219, '\n',
219, 32, 32, 32, 32, 32, 32, 32, 219, '\n',
219, 32, 32, 32, 32, 32, 32, 32, 219, '\n',
219, 32, 32, 32, 32, 32, 32, 32, 219, '\n',
219, 219, 219, 219, 219, 219, 219, 219, 219, '\n'};
/* game.c (main file) */
#include <stdio.h>
#include "game.h"
int main(void){
system("cls");
floor[x][y] = character;
printf("%s", floor);
char move;
redo:
printf("\nTravel which way?\n");
printf("a = left\ns = down\nd = up\nf = right\n\n>");
scanf("%s", &move);
/*
array oftentimes gets desroyed because the newlines are
being overwritten by the assignments.
the if statements should have prevented this.
why didn't they?
*/
if (move == 'a'){ /* LEFT */
if (x < 1){
x = 1;}
x--;
floor[x][y] = character;
floor[x+1][y] = ' ';
system("cls");
printf("%s", floor);
goto redo;
} else if (move == 's'){ /* DOWN (works, but goes right. Sometimes clones itself) */
if (y > 3){
y = 3;} /*bounds may be wrong*/
y++;
floor[x][y] = character;
floor[x][y-1] = ' ';
system("cls");
printf("%s", floor);
goto redo;
} else if (move == 'd'){ /* UP */
if (y < 1){
y = 1;}
y--;
floor[x][y] = character;
floor[x][y+1] = ' ';
system("cls");
printf("%s", floor);
goto redo;
} else if (move == 'f'){ /* RIGHT */
if (x > 7){
x = 7;}
x++;
floor[x][y] = character;
floor[x-1][y] = ' ';
system("cls");
printf("%s", floor);
goto redo;}
else {
goto done;
}
done:
return 0;
}
谁能告诉我我做错了什么?
注意:此地图设置只是一个草稿。一旦机制完成,我将完全不同地做这件事,但我将计划以几乎相同的方式沿阵列移动角色,所以我宁愿在这个特定设置方面获得帮助,而不是关于如何获得建议做得更好/不同。但是,显示更好实现的相关答案和源代码仍然有用和赞赏,因为我自己可能一开始就完全错误,因为这是我的第一个 Roguelike 游戏。