-
Drawing Program in C
Hey guys, I'm having trouble with a code for an assignment.
I've got the base for the assignment working as you can see;
Basically, there is a boundary drawn, as you move with the keyboard arrows (UP, DOWN, LEFT and RIGHT) the program will draw a "#" and the curser is a "@".
The program does this successfully but when you press "p", the program should enter a mode where you can move the curser but not draw anything, then when you press "p" again, it starts to draw again.
The problem I'm facing is that, when I press "p" the program only allows me to enter one space without drawing before returning itself to normal drawing mode.
It also exits when you press "q"
Sorry about all the commented out lines, I wrote the two loops (One for drawing, one for moving) separately.
Here is my code:
Code:
#include <curses.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define UP 3
#define DOWN 2
#define RIGHT 5
#define LEFT 4
#define P 6
#define LEN 18
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]={" ---------------- ",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
" ---------------- "
};
keypad(initscr(),TRUE);
curs_set(0);
int xPos=1;
int yPos=1;
int p;
char c='\0';
mvprintw(20,2,"Drawing Field of Play");
mvprintw(21,2,"Press P to Toggle Pen");
mvprintw(22,2,"Press Q to Quit Drawing");
mvprintw(23,2,"Pen Down");
while(c!='q')
{
mvprintw(23,2,"Pen Down");
dispMap(xPos,yPos,map);
c=getch();
while (c=='p')
{
mvprintw(23,2,"Pen Up ");
c=getch();
enum{off,on}p;
p==on;
if (p)
switch(c)
{
case UP:
if (map[yPos-1][xPos]== ' ')
{
yPos--;
c=p;
}
else if (map[yPos-1][xPos]== '#')
{
yPos--;
c=p;
}
break;
case DOWN:
if (map[yPos+1][xPos]== ' ')
{
yPos++;
c=p;
}
else if (map[yPos+1][xPos]== '#')
{
yPos++;
c=p;
}
break;
case LEFT:
if (map[yPos][xPos-1]== ' ')
{
xPos--;
c=p;
}
else if (map[yPos][xPos-1]== '#')
{
xPos--;
c=p;
}
break;
case RIGHT:
if (map[yPos][xPos+1]== ' ')
{
xPos++;
c=p;
}
else if (map[yPos][xPos+1]== '#')
{
xPos++;
c=p;
}
}
// 'p'==off;
// if ('p')
// {
// getchar();
// }
}
switch(c)
{
case UP:
if (map[yPos-1][xPos]== ' ')
{
map[yPos][xPos]='#';
yPos--;
}
else if (map[yPos-1][xPos]== '#')
{
map[yPos][xPos]='#';
yPos--;
}
break;
case DOWN:
if (map[yPos+1][xPos]== ' ')
{
map[yPos][xPos]='#';
yPos++;
}
else if (map[yPos+1][xPos]== '#')
{
map[yPos][xPos]='#';
yPos++;
}
break;
case LEFT:
if (map[yPos][xPos-1]== ' ')
{
map[yPos][xPos]='#';
xPos--;
}
else if (map[yPos][xPos-1]== '#')
{
map[yPos][xPos]='#';
xPos--;
}
break;
case RIGHT:
if (map[yPos][xPos+1]== ' ')
{
map[yPos][xPos]='#';
xPos++;
}
else if (map[yPos][xPos+1]== '#')
{
map[yPos][xPos]='#';
xPos++;
}
}
}
// while (c=='p')
// {
//
// dispMap(xPos,yPos,map);
// c=getch();
// mvprintw(23,2,"Pen Up ");
//
// enum button{on,off}p;
// {
// p=on;
// if (p)
// {
// mvprintw(23,2,"Pen Up ");
// dispMap(xPos,yPos,map);
// 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++;
// }
// }
// p==off;
// if(!p)
// {
// mvprintw(23,2,"Pen Down");
// }
// getchar();
//
// }
endwin();
}
Cheers!
-
It would be a lot easier if you used a boolean (or int) variable of pendown (1=down, 0=not down), instead of testing for c=='p', because c gets new chars, all the time, as the program is in use.
so
Code:
int pendown = 0; //start with pen not down
if(c=='p')
pendown=!pendown; //switch state, if pen is down already, switch to up, if up, switch to down state
while(pendown) {
//do a drawing keystroke
c=getchar();
switch (c) {
//handle c==p pendown=!pendown break
//etc.
}
}
-
thanks for the reply man, before what was happening is, I'd press p, it would move 1 character and reset to Pen Down, with the change the code, it selects Pen Up, but then I can't move the curser at all;
Here's what I've done:
Code:
while(c!='q')
{
mvprintw(23,2,"Pen Down");
dispMap(xPos,yPos,map);
c=getch();
if (c=='p')
{
pendown!=pendown;
while (pendown=1)
{
mvprintw(23,2,"Pen Up ");
c=getch();
switch(c)
{
case UP:
if (map[yPos-1][xPos]== ' ')
{
yPos--;
//c=p;
}
else if (map[yPos-1][xPos]== '#')
{
yPos--;
//c=p;
}
break;
case DOWN:
if (map[yPos+1][xPos]== ' ')
{
yPos++;
//c=p;
}
else if (map[yPos+1][xPos]== '#')
{
yPos++;
//c=p;
}
break;
case LEFT:
if (map[yPos][xPos-1]== ' ')
{
xPos--;
//c=p;
}
else if (map[yPos][xPos-1]== '#')
{
xPos--;
//c=p;
}
break;
case RIGHT:
if (map[yPos][xPos+1]== ' ')
{
xPos++;
//c=p;
}
else if (map[yPos][xPos+1]== '#')
{
xPos++;
//c=p;
}
}
}
}
is that what you meant to do?
-
-
Oh yeah, of course.
Code:
if (c=='p')
{
pendown!=pendown;
while (pendown==1)
{
mvprintw(23,2,"Pen Up ");
c=getch();
and so on,
now it's not even saying Pen Up, it doesn't seem to register the "p" button.
I'm a bit confused with the pendown!=pendown.
If c is equal to p
pendown does not equal pendown?
I'm pretty amatuer when it comes to this kinda stuff
-
pendown=!pendown
Is a toggle switch (light switch), that you are changing. If it's true (anything else but zero), you are switching it to 0. (false). If it's false, you are switching it to true.
Wax on, wax off, :cool:
The logic is a little fussy. I used this:
Code:
int c, moveOK,pendown=0;
while(c!='q') {
moveOK=0;
c=_getch();
if(c=='p')
pendown=!pendown;
if (c == 0 || c == 0xE0) {
c = _getch(); //key code has two values-get the second one
switch(c) {
case UP:
if(y>TOP+1) {
_gotoxy(x,--y);
moveOK=1;
}
break;
case LEFT:
if(x>LBORDER+1) {
_gotoxy(--x,y);
moveOK=1;
}
break;
case RIGHT:
if(x<RBORDER-1) {
_gotoxy(++x,y);
moveOK=1;
}
break;
case DOWN:
if(y<BOTTOM-1) {
_gotoxy(x,++y);
moveOK=1;
}
break;
}
if(pendown && moveOK)
putchar('*');
}
}
The above uses conio.h functions, (I don't have curses), but you should be able to translate it, no problem.
All the capitalized words are #define values. TOP is the top row of the screen for the area, RBORDER is the right border of the area, BOTTOM is the last row of the screen for the area, and LBORDER is the left side of the screen area border.