-
Moving ASCII Character
I am trying to make an ASCII character move across a map.
Example:
..........
...!......
..........
..........
..........
..........
I want the '!' to move Up, Down, Left and Right depending on the key you enter. Not sure at all how to do this, help or an example would be appriciated.
-Thanks, Ranedhel
-
What have you tried so far?
How about a simple 2-d array, like
char a[10][10];
to hold the map.
-
Re:
yes, I thought of that - but I'm not totally sure how to put everything together. If it's not to much trouble, could you
make me an example code?
-
This creates a 10x10 array, and populates all elements with a dot.
Code:
#include <stdio.h>
int main(void)
{
char a[10][10];
int i, j;
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
a[i][j] = '.';
return(0);
}
Now see if you can make it work for you :)
-
Re:
:o Heh, I'm sorta new to arrays, I'm not exactly sure how to display the elements. :o
-
You need to read up on it then ;)
printf ("%c", a[i][j]);
-
Re:
-
Or (and I should have said this in the first place):
>>putchar(a[i][j]);
-
um.. what about just saving the position of this special character and when you want to move you redraw the map (10x10) and put the special character on its position like this:
Code:
for(int y=0;y<MAX_Y;y++) //MAX_X is the width of you charmap
{ for(int x=0;x<MAX_X;x++) //guess what
putch('.');
putch('\n');
}
gotoxy(posx,posy);
putch('!');
greets,
corpse
-
Code:
#define EMPTY 0
#define PLAYER 1
#define TRUE 1
#define FALSE 0
void DrawMap(void);
void MovePlayer(int xmove,int ymove);
void Init(void);
void GetInput(void);
void ClearScreen(void);
void Init(void)
{
}
void GetInput(void)
{
}
void ClearScreen(void)
{
}
void DrawMap(void)
{
for (int i=0;i<10;i++)
{
for (int j=0;j<10;j+)
{
switch (map[i][j])
{
case EMPTY: printf(".");break;
case PLAYER: printf("!");break;
}
}
printf("\n");
}
}
void MovePlayer(int xmove,int ymove)
{
if (xmove!=0)
{
int tempx=playerx+xmove;
if (tempx>0 && tempx<10) playerx=tempx;
}
if (ymove!=0)
{
int tempy=playery+ymove;
if (tempy>0 && tempy<10) playery=tempy;
}
}
int main(void)
{
Init();
while (Playing)
{
ClearScreen();
DrawMap();
GetInput();
}
return(0);
}
Just one example of how to draw the map and a small example of your main game loop - GetInput() does not return until the user does something so this is not a good real-time example. Also in real time you would not want to ClearScreen() every frame as this would cause a lot of flicker. To explain more I would have to go into details that might be beyond the scope of this thread.
MovePlayer() does not check for map boundaries but it needs to. I'll leave this up to you. If you do not check the boundaries, this program will crash hard if you move off of the map. The emtpy functions are for you to figure out plus I'm too lazy to code them for you.
:D
-