Sign in to follow this  
saml

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this