|
CS256 - Fundamentals of Structured Programming
| Displaying exercises/e9/solution/c1.c
#include "c.h"
/**
* Complete the function to:
* 1) Clear the screen
* 2) draw a person figure where the upper left corner of the person is located
* at the x,y position. Hint: The person is 4 lines of text, start the cursor
* for each line at the left side, keeping the column (x) fixed, increase the
* line (y) by one for each line of the person.
* o
* /T\
* |
* / \
* 3) Refresh the screen
*/
void person(int x, int y)
{
clear();
mvaddch(y,x+1,'O');
mvaddstr(y+1,x,"/T\\");
mvaddch(y+2,x+1,'|');
mvaddstr(y+3,x,"/ \\");
refresh();
}
/**
* Complete the main function that uses the above function to draw a person
* figure that is moved around using the keyboard arrow keys.
* 1) Start the person roughly in the middle of the screen.
* 2) When a q or a Q is typed, exit the program.
* 3) Make sure that the person figure does not leave the screen area. Keep in
* mind that the x,y position of the person is in the upper left corner of
* the person.
*/
int main(void)
{
init_curses();
int ch, x = COLS/2-1, y=LINES/2-2;
person(x,y);
while((ch = getch()) != EOF) {
if (ch == 'q' || ch == 'Q') break;
if (ch == KEY_LEFT && x > 0) x--;
if (ch == KEY_RIGHT && x < COLS-3) x++;
if (ch == KEY_UP && y > 0) y--;
if (ch == KEY_DOWN && y < LINES-4) y++;
person(x,y);
}
leave_curses();
}
|