使用ncurses创建地图 [英] Map creating using ncurses
本文介绍了使用ncurses创建地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,该程序的目标是使用您的键在形状周围移动#。当它移动时,它会在它后面留下一串#。目前我无法获得打印的痕迹,因为我无法让地图更新。
#include< ; curses.h>
#define LEN 21
#defined TRUE 1
#define FALSE 0
#define UP 3
#define DOWN 2
#define RIGHT 5
#define LEFT 4
// 一个简单的程序,允许光标在屏幕。
void dispMap( int x,
int y,
char map [] [LEN]){
int row,col;
// 绘制基本地图
for (row = 0 ; row< LEN; row ++){
for (col = 0 ; col< LEN; col ++){
mvaddch(row + 1,col + 1,map [row] [col]);
}
}
// 光标符号
mvaddch(y + 1,x + 1,' #');
}
int main( void ){
char map [] [LEN] = { - --------------, // 填充空白区域数组长度为LEN
| |,
| |,
| |,
| |,
| |,
| | ,
| | ,
| | ,
| | ,
| | ,
| | ,
| | ,
| | ,
--------------- ,
绘制游戏场地,
< span class =code-string> 按p切换笔,
按q退出,
Pen pen, // 可能定义一个字符串以允许它更改
,
,
};
// 激活键盘以从此终端读取
键盘(initscr(),TRUE);
// 不显示光标
curs_set( 0 跨度>);
// 初始化光标的x和y位置
int xPos = 1 ;
int yPos = 1 ;
int p = 1 ;
char c = ' \ 0' 跨度>; // 输入字符初始化为null
while (c!= ' q'){
dispMap (XPOS,yPos,地图);
// 读取单个字符
c = getch();
mvaddch(yPos,xPos,' #'); // 删除旧位置
// 根据光标移动
switch (c){
case UP:
if (map [yPos-1] [xPos] == ' '){
yPos--;
}
break ;
case DOWN:
if (map [yPos + 1] [xPos] == ' '){
yPos ++;
}
break ;
case LEFT:
if (map [yPos] [xPos-1] == ' '){
xPos--;
}
break ;
case RIGHT:
if (map [yPos] [xPos + 1] == ' '){
xPos ++;
}
}
}
endwin();
}
我的尝试:
我已经尝试了一切,你的帮助将不胜感激
解决方案
对于输出你可以使用着名的printf
< pre lang =c ++> printf( Hello world!\ n);
但你的变量是
printf(map);
So the goal of this program is to use your keys to move the # around the shape. And when it moves it leaves a trail of #'s behind it. At the moment i can't get the trail to print as i can't get the map to update.
#include <curses.h>
#define LEN 21
#define TRUE 1
#define FALSE 0
#define UP 3
#define DOWN 2
#define RIGHT 5
#define LEFT 4
// A simple program that allows a cursor to be moved around the screen.
void dispMap(int x,
int y,
char map[][LEN]){
int row,col;
// draw basic map
for(row=0;row<LEN;row++){
for(col=0;col<LEN;col++){
mvaddch(row+1,col+1,map[row][col]);
}
}
// cursor symbol
mvaddch(y+1,x+1,'#');
}
int main(void){
char map[][LEN]={" --------------- ", // empty space to fill array to length of LEN
"| | ",
"| | ",
"| | ",
"| | ",
"| | ",
"| | ",
"| | ",
"| | ",
"| | ",
"| | ",
"| | ",
"| | ",
"| | ",
" --------------- ",
"Drawing feild of play",
"Press p to toggle pen",
"Press q to quit ",
"Pen down ", //Maybe define a string to allow it to change
" ",
" ",
};
// activate the keypad to read from this terminal
keypad(initscr(),TRUE);
//don't show cursor
curs_set(0);
// initialise the x and y positions of the cursor
int xPos=1;
int yPos=1;
int p = 1;
char c='\0'; // input character initialise to null
while( c!='q'){
dispMap(xPos,yPos,map);
// read a single character
c=getch();
mvaddch(yPos,xPos,'#'); // erase old position
// move according to cursor
switch(c){
case UP:
if (map[yPos-1][xPos]== ' '){
yPos--;
}
break;
case DOWN:
if (map[yPos+1][xPos]== ' '){
yPos++;
}
break;
case LEFT:
if (map[yPos][xPos-1]== ' '){
xPos--;
}
break;
case RIGHT:
if (map[yPos][xPos+1]== ' '){
xPos++;
}
}
}
endwin();
}
What I have tried:
Ive tried everything and your help will be much appreciated
解决方案
For output you can use the famous printf
printf("Hello world!\n");
but with your variable
printf(map);
这篇关于使用ncurses创建地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文