#include #include using namespace std; void finish(); void initCurses(); int getCheckVelocity(int xy, int v, int max); main() { /* initialize your non-curses data structures here */ initCurses(); bool flag = false; int maxy = 25, maxx = 80; int x = maxx/2, y = maxy/2; int vx = 0, vy = 0; mvaddch(y, x, 'O'); int c; while(true){ c = getch(); /* REFRESH, accept single keystroke of input */ mvaddch(y, x, ' '); // Erase old ball switch(c){ case 'a': case 'A': vx = -1; vy = 0; break; case 'd': case 'D': vx = 1; vy = 0; break; case 'w': case 'W': vy = -1; vx = 0; break; case 's': case 'S': vy = 1; vx = 0; break; case 'q': case 'Q': flag = true; break; } if(flag) break; vy = getCheckVelocity(y, vy, maxy); vx = getCheckVelocity(x, vx, maxx); y = y + vy; x = x + vx; mvaddch(y, x, 'O'); // display ball at new location } finish(); /* we're done */ return 0; } int getCheckVelocity(int xy, int v, int max){ int vel = v; if(xy + v <= 0 || xy + v >= max){ vel = -v; } return vel; } void finish() { endwin(); /* do your non-curses wrapup here */ system("clear"); cout << "All Done!" << endl; system("pause"); } void initCurses(){ (void) initscr(); /* initialize the curses library */ keypad(stdscr, TRUE); /* enable keyboard mapping */ (void) nonl(); /* tell curses not to do NL->CR/NL on output */ (void) cbreak(); /* take input chars one at a time, no wait for \n */ (void) noecho(); /* don't echo input */ halfdelay(1); /* modifies getch to wait 1/10 of a second for input and if user hasn't typed anything getch returns ERR else returns the character typed by user */ }