VB icon

move a char across the screen using the arrow keys (good for making games)

Email
Submitted on: 1/2/2015 11:09:00 AM
By: theMayor (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: C++ (general)
Views: 580
 
     This program (well commented) will show you the use of the arrow keys by movng a charachter across the screen. *Note* this program does not have limits so try to stay withing the screen limits. Email me if you''dlike to see boundries put on it!! and dont' forget to vote!!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: move a char across the screen using the arrow keys (good for making games)
// Description:This program (well commented) will show you the use of the arrow keys by movng a charachter across the screen. *Note* this program does not have limits so try to stay withing the screen limits. Email me if you''dlike to see boundries put on it!! and dont' forget to vote!!
// By: theMayor (from psc cd)
//**************************************

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
 int top=40;// keeps up with the top coordinate
 int side=12;// keeps up with the side coordinate
 char input;//will detect arrow keys
 char kc;//will get the players charachter
 cout << "Please Enter a Charachter!\n";
 kc=getche();// gets the user charachter
 cout << flush;
 cout << "\nNow Just press the arrow keys to move the " << kc << " across the Screen!!\n\n";
 cout << "Press any arrowkey to continue...\n";
 getch();
 system ("cls");//clears the screen
 _setcursortype(_NOCURSOR);// removes the cursor from the screen!
 gotoxy(20,24);cout << "Press The Arrow Keys to move the " << kc << " Around!!\n"<<flush;
 gotoxy(37,25);cout << "Esc to Quit\n"; 
 gotoxy(40,12);cout << kc <<flush; 
 while (input!=27)// while the user does NOT hit escape
 {
 input=getch();//get user input
 if (input==0)// if a special key is pressed... in this case if an arrow key is pressed
 {
input=getch();//call getch() again to get the next value
 if (input=='K')// if it's the left arrow key
 {
 gotoxy(top,side);cout << " "<< flush;// all this does is put a space where the charachter WAS
 top--;// subtract from the top coordinate
 gotoxy(top,side);cout << kc << flush; // prints the charachter at the new location
 } 
else if (input=='P')// if the down arrow key is pressed
{
 gotoxy(top,side);cout << " " << flush;
 side++;// adds one to the top coordinate
 gotoxy (top,side);cout << kc << flush;
}
else if (input=='M')// if the right arrow key is pressed 
{
 gotoxy(top,side);cout << " " << flush;
 top++; //adds one to the top coordinate
 gotoxy(top,side);cout<< kc << flush;
}
else if (input=='H')// if the up arrow key is pressed
{
gotoxy(top,side);cout << " " << endl;
side--;
gotoxy(top,side);cout << kc << flush;
}
 }
 }
 return 0;
}


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.