# C++ Question

## Recommended Posts

I am trying to write a program which allows the user to move an '@' symbol around a field of dashes with the arrow keys. It works, but instead of refreshing the field when the user presses a key, it creates an entirely new field with the change. Is it feasable to simply "refresh" the data when the user makes a change? Or should I just be happy with what I have (quite new at programming)? Thanks for any and all help.
#include <iostream>
#include <conio.h>
using namespace std;

const char CPPKEYUP = 72;
const char CPPKEYLEFT = 75;
const char CPPKEYRIGHT = 77;
const char CPPKEYDOWN = 80;

int player_x = 4, player_y = 4;

////////////////////////////////
void draw_frame()
////////////////////////////////
{
int x, y;
char space = '-', player = '@';

for (y = 0; y < 10; y++) {
for (x = 0; x < 10; x++) {
if (player_y == y && player_x == x) {
cout << player;
}
else {
cout << space;
}
}
cout << "\n";
}
cout << "\n";
}

////////////////////////////////
int main()
////////////////////////////////
{
char user_input = 0;
user_input = kbhit();
while(user_input !=27)
{
draw_frame();
user_input = getch();
switch(user_input)

{
case CPPKEYUP:
player_y--;
if (player_y == -1) {
player_y = 9;
}
break;
case CPPKEYDOWN:
player_y++;
if (player_y == 10) {
player_y = 0;
}
break;
case CPPKEYLEFT:
player_x--;
if (player_x == -1) {
player_x = 9;
}
break;
case CPPKEYRIGHT:
player_x++;
if (player_x == 10) {
player_x = 0;
}
break;
}
}
}



##### Share on other sites
Without using a API I don't think you are going to get the what you want. I could be wrong since im fairly new as well. BTW, too me that code looks pretty good.

##### Share on other sites
You are going to have to go to something platform specific for the console work
OR
try the NCurses API
OR
you can use something like 'system("cls");' which will get you close to what you want, but is NOT recomended as it is bad coding.

##### Share on other sites
Thanks for the help guys - I'll take a look at NCurses.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627719
• Total Posts
2978790

• 9
• 21
• 14
• 12
• 42