Here's a modification of the program in post#4 that let's you move your name around the screen like a snake.
Code:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#define CONSOLE_WIDTH 80
#define CONSOLE_HEIGHT 24
#define MAX_NAME 21
typedef struct {
int x, y;
} Point;
void clrscr();
void gotoxy(int x, int y);
int main(void) {
int ch = 'a'; /* Input char; init to anything but q */
int dx = -1, dy = 0; /* Displacement for x and y */
char name[MAX_NAME];
Point point[MAX_NAME]; /* Position of each character in name. */
size_t i, len;
clrscr();
/* Get user's name, remove newline, concatenate space. */
printf("Enter your name: ");
fgets(name, sizeof name - 1, stdin); /* Leave room for a space char. */
len = strlen(name);
if (name[len-1] == '\n')
name[--len] = '\0';
name[len++] = ' ';
name[len] = '\0'; /* Ensure zero termination. */
/* Initialize point array. */
point[0].x = CONSOLE_WIDTH / 2;
point[0].y = CONSOLE_HEIGHT / 2;
for (i = 1; i < len; i++) {
point[i].x = point[i-1].x + 1;
point[i].y = point[i-1].y;
}
clrscr();
while (ch != 'q') { /* q to quit */
/* Display each char in name at it's position in point. */
for (i = 0; i < len; i++) {
gotoxy(point[i].x, point[i].y);
_putch(name[i]);
}
if (_kbhit()) {
ch = _getch();
if (ch == 224) {
switch (_getch()) {
case 72: /* Up */ dx = 0; dy = -1; break;
case 75: /* Left */ dx = -1; dy = 0; break;
case 77: /* Right */ dx = 1; dy = 0; break;
case 80: /* Down */ dx = 0; dy = 1; break;
}
}
}
/* Move each char into the next char's position. */
for (i = len - 1; i > 0; i--)
point[i] = point[i-1];
/* Update head char's position. */
if ((dx == 1 && point[0].x < CONSOLE_WIDTH - 1) ||
(dx == -1 && point[0].x > 0))
point[0].x += dx;
if ((dy == 1 && point[0].y < CONSOLE_HEIGHT) ||
(dy == -1 && point[0].y > 0))
point[0].y += dy;
Sleep(100);
}
return 0;
}
void clrscr() {
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '),
dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
return;
}
void gotoxy(int x, int y) {
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}