我在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