98 lines
2.8 KiB
C
98 lines
2.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h> // for `usleep()`
|
|
#include <string.h> // for `memset()`
|
|
|
|
#define WIDTH 40 // Terminal width (adjust based on your terminal)
|
|
#define HEIGHT 20 // Terminal height
|
|
#define NUM_CHARS 5 // Number of characters (now 5)
|
|
|
|
// ANSI color codes for the characters
|
|
#define COLOR_GREEN "\033[32m"
|
|
#define COLOR_PURPLE "\033[35m"
|
|
#define COLOR_YELLOW "\033[33m"
|
|
#define COLOR_RED "\033[31m"
|
|
#define COLOR_CYAN "\033[36m"
|
|
#define COLOR_RESET "\033[0m"
|
|
|
|
typedef struct {
|
|
char symbol; // Character symbol (e.g., 'G', 'P', etc.)
|
|
const char* color; // ANSI color code
|
|
int x, y; // Position
|
|
} Character;
|
|
|
|
// Function to clear the terminal screen
|
|
void clearScreen() {
|
|
printf("\033[H\033[J"); // ANSI escape code
|
|
}
|
|
|
|
// Function to draw the Tokyo skyline (using tabs and symbols)
|
|
void drawSkyline() {
|
|
printf("\n"); // Move to the bottom
|
|
printf("\t\t _/\\_ __\n");
|
|
printf("\t\t / \\ _/ \\_\n");
|
|
printf("\t\t |TOKYO |___/ \\\n");
|
|
printf("\t\t \\______/ \\__\n");
|
|
}
|
|
|
|
// Function to render a single frame
|
|
void drawFrame(Character chars[NUM_CHARS]) {
|
|
char grid[HEIGHT][WIDTH];
|
|
|
|
// Initialize grid with spaces
|
|
memset(grid, ' ', sizeof(grid));
|
|
|
|
// Place characters in the grid
|
|
for (int k = 0; k < NUM_CHARS; k++) {
|
|
if (chars[k].y >= 0 && chars[k].y < HEIGHT && chars[k].x >= 0 && chars[k].x < WIDTH) {
|
|
grid[chars[k].y][chars[k].x] = chars[k].symbol;
|
|
}
|
|
}
|
|
|
|
// Draw the grid with colors
|
|
for (int i = 0; i < HEIGHT; i++) {
|
|
for (int j = 0; j < WIDTH; j++) {
|
|
// Check if this cell contains a character
|
|
int charFound = 0;
|
|
for (int k = 0; k < NUM_CHARS; k++) {
|
|
if (chars[k].y == i && chars[k].x == j) {
|
|
printf("%s%c%s", chars[k].color, chars[k].symbol, COLOR_RESET);
|
|
charFound = 1;
|
|
break;
|
|
}
|
|
}
|
|
if (!charFound) printf(" "); // Empty space
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
// Draw the Tokyo skyline
|
|
drawSkyline();
|
|
}
|
|
|
|
int main() {
|
|
// Initialize the five characters with colors and positions
|
|
Character chars[NUM_CHARS] = {
|
|
{'G', COLOR_GREEN, 10, 0}, // Green
|
|
{'P', COLOR_PURPLE, 15, 0}, // Purple
|
|
{'Y', COLOR_YELLOW, 20, 0}, // Yellow
|
|
{'R', COLOR_RED, 25, 0}, // Red
|
|
{'C', COLOR_CYAN, 30, 0} // Cyan
|
|
};
|
|
|
|
// Animation loop (falling motion)
|
|
for (int frame = 0; frame < HEIGHT; frame++) {
|
|
clearScreen();
|
|
drawFrame(chars);
|
|
|
|
// Move each character down
|
|
for (int k = 0; k < NUM_CHARS; k++) {
|
|
chars[k].y++;
|
|
}
|
|
|
|
usleep(200000); // 200ms delay (adjust for speed)
|
|
}
|
|
|
|
return 0;
|
|
}
|