在广场上画# [英] Drawing #'s in the square

查看:58
本文介绍了在广场上画#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有这里是代码:





 #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--;
mvaddch(yPos,xPos,' #');
}
break ;
case DOWN:
if (map [yPos + 1] [xPos] == ' '){
yPos ++;
mvaddch(yPos,xPos,' #');
}
break ;
case LEFT:
if (map [yPos] [xPos-1] == ' '){
xPos--;
mvaddch(yPos,xPos,' #');
}
break ;
case RIGHT:
if (map [yPos] [xPos + 1] == ' '){
xPos ++;
mvaddch(yPos,xPos,' #');
}
}
}
endwin();
}







问题是当我移动我的密钥而不打印哈希在形状



我尝试过:



我试过重新定位线

mvaddch(yPos,xPos,'#');在is语句中但是没有帮助

解决方案

使用调试器检查你从键盘获得的内容,你可能对你得到的代码感到惊讶!

尝试将箭头代码更改为简单字母,仅用于测试目的。



您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

Hey all here is the code:


#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--;
        mvaddch(yPos,xPos,'#');
      }
      break;
    case DOWN:
      if (map[yPos+1][xPos]== ' '){
        yPos++;
        mvaddch(yPos,xPos,'#');
      }
      break;
    case LEFT:
      if (map[yPos][xPos-1]== ' '){
        xPos--;
        mvaddch(yPos,xPos,'#');
      }
      break;
    case RIGHT:
      if (map[yPos][xPos+1]== ' '){
        xPos++;
        mvaddch(yPos,xPos,'#');
      }
    }
  }
  endwin();
}




The problem is though when i move my key its not printing a hash in the shape

What I have tried:

I have tried to reposition the line
mvaddch(yPos,xPos,'#'); in the is statement but thats not helping

解决方案

Use the debugger to check what you get from keyboard, you may have a surprise with the codes you get !
try to change the arrows codes to simple letters, just for testing purpose.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.


这篇关于在广场上画#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆