我正在用 C 语言制作 Roguelike 游戏,但我无法让我的角色按照我想要的方式移动。我在点 (x, y) 处制作了一个带有字符的二维字符数组,绘制了数组,更改了 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;
}
谁能告诉我我做错了什么?
注意:此 map 设置只是草稿。一旦机制完成,我将以完全不同的方式进行操作,但我将计划以几乎相同的方式沿数组移动角色,所以我宁愿在这个特定设置上获得帮助,而不是如何建议做得更好/不同。但是,显示更好实现的相关答案和源代码仍然有用和值得赞赏,因为我自己可能一开始就完全错了,因为这是我的第一个 Roguelike 游戏。
最佳答案
我觉得你的 floor 声明有点奇怪尝试做这样的事情:
//c arrays are generally array[rows][columns]
int rows = 6, cols = 10;
char floor[rows][cols] = {
{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'}};
此处示例 http://ideone.com/O7UG8g .我在该示例中更改了 219->35,因为我认为打印扩展的 ascii 在该站点上不起作用。
另外我认为绑定(bind)检查应该是这样的:
//move left (go one column left)
//if current position is array[curRow][curCol]
//assuming array[0][0] is top left of map
//check if left is a wall
if((curCol - 1) == charForAWall)
return;
else
curCol--;
右边是 +1 列,向上 -1 行,向下 +1 行。
关于C:Roguelike map 很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15890607/