我在C制作Roguelike游戏,我无法让我的角色以我想要的方式移动。我在点(x,y)上创建了一个2D字符数组,并绘制了该数组,并更改了x和y值,并根据输入的方向重新排列了数组(有点像Zork,但是与图形)。但是这并不能解决我的计划。该代码会解释比我更可以:C:Roguelike地图很扭曲
/* 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游戏。
它有什么问题?假设我们大多数人不会在可能的情况下编写代码。 – Pubby 2013-04-08 23:14:57
问题是,如果我告诉它向左移动,角色不会移动,比如离开。如果我放置命令来移动字符,它应该向左移动1,但是在重复调用该命令时不会这样做。它以Z字形而不是直线形式移动,有时候字符会出现在数组中的多个位置。 – 2013-04-08 23:19:29
好吧,我会建议保持球员和地图分开。使用curses库来绘制所有内容而不是printf。无论如何,简单看看你的代码让我觉得对's''的边界检查是不正确的。 – Pubby 2013-04-08 23:24:28