Here's some code for making a guy (a smiley face) walk around.
#include // these includes are stdio.h and conio.h. Only conio.h is really needed, stdio.h is put there just out of habit.#include
#define UP 72 // Characters the arrow keys return from keyboard.#define DOWN 80#define LEFT 75#define RIGHT 77
int main () {char c;int x,y, sizex=20, sizey=10;
textcolor(15);textbackground(0);clrscr();// Change background color to 0 (black), and clear screen.
textbackground(1);// Change background color to 1 (blue)
// This for statement draws an "arena" sizex X sizey in the upper left corner of screen.for (y=0; y{ for (x=0; x cprintf("");}
// main loop function
x=sizex/2; // Set X and Y to be at center of arenay=sizey/2;while (c!=27){gotoxy(x,y); // Moves cursor to specified coordinantes.cprintf("%c", 0x02); //Prints out a smiley face (ASCII char #2).gotoxy(x,y); // Moves cursor under the guy (looks nicer and used for erasing).
c=getch(); // Reads characters from keyboard.if (c==0) c=getch(); // Detects arrow scancodes, they have a 0 character and then another character, we use only the second character.cprintf(" "); //Erases the guy (so he doesn't leave a trail).
if ((c==UP) && (y>1)) y--; //If keyboard returns UP and if the Guy (the smiley) CAN go up (as in he must stay in the arena).if ((c==DOWN) && (yif ((c==LEFT) && (x>1)) x--;if ((c==RIGHT) && (x}return 0;}
-Xotor-
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
Here's some code for making a guy (a smiley face) walk around.
#include // these includes are stdio.h and conio.h. Only conio.h is really needed, stdio.h is put there just out of habit.
#include
#define UP 72 // Characters the arrow keys return from keyboard.
#define DOWN 80
#define LEFT 75
#define RIGHT 77
int main () {
char c;
int x,y, sizex=20, sizey=10;
textcolor(15);
textbackground(0);
clrscr();
// Change background color to 0 (black), and clear screen.
textbackground(1);
// Change background color to 1 (blue)
// This for statement draws an "arena" sizex X sizey in the upper left corner of screen.
for (y=0; y
{
for (x=0; x
cprintf("
");
}
// main loop function
x=sizex/2; // Set X and Y to be at center of arena
y=sizey/2;
while (c!=27)
{
gotoxy(x,y); // Moves cursor to specified coordinantes.
cprintf("%c", 0x02); //Prints out a smiley face (ASCII char #2).
gotoxy(x,y); // Moves cursor under the guy (looks nicer and used for erasing).
c=getch(); // Reads characters from keyboard.
if (c==0) c=getch(); // Detects arrow scancodes, they have a 0 character and then another character, we use only the second character.
cprintf(" "); //Erases the guy (so he doesn't leave a trail).
if ((c==UP) && (y>1)) y--; //If keyboard returns UP and if the Guy (the smiley) CAN go up (as in he must stay in the arena).
if ((c==DOWN) && (y
if ((c==LEFT) && (x>1)) x--;
if ((c==RIGHT) && (x
}
return 0;
}
-Xotor-