Introduction to Programming

Stars


Filename: stars.c
Compile: gcc stars.c -o stars
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void clear() 
{ 
    printf("\033[H\033[J"); 
} 

void move(int x, int y) 
{ 
    printf("\033[%d;%dH", y, x); 
} 

void doColor(int c) 
{ 
    if (c >= 0 && c <= 7) 
        printf("\033[3%dm", c); 
} 

void undoColor() 
{ 
    printf("\033[m"); 
} 

int main(int argc, char *argv[]) 
{ 
    int num; 
    int i; 

    srand(time(NULL)); 

    num = rand() % 100; 

    clear(); 

    for (i=0; i<num; i++) 
    { 
        move(rand() % 60, rand() % 20); 
        doColor(rand() % 8); 
        printf("*"); 
    } 

    undoColor(); 
    move(0, 21); 
    return 0; 
}

Next: Bounce

Previous: Project Ideas


Introduction to Programming

Home