Browse » Home
»
how to chande color of a character run time in c
» Program to change colors of a moving character runtime...
Program to change colors of a moving character runtime...
Program to change colors of a moving character runtime...
#include <stdio.h>
#include <conio.h>
#include <dos.h>
void main()
{
clrscr();
char choice;
printf("b for blue\ng for green\nr for red\nm for magenta\n");
printf("c for cyan\nw for white\ny for yellow");
printf("\nz for light blue\nl for light green\nn for brown\n\n");
while(!kbhit()) //kbhit() is a function which checks to see is any key of keyboard is pressed
{
delay(100);
printf("A");
}
if(kbhit())
choice=getch(); //the key that is pressed is assigned in choice variable with the help of getche()
do
{
if(choice=='r')
{
while(!kbhit())
{
textcolor(4); //textcolor() sets a new character color,the color value is passed as argument
delay(100);
cprintf("A");
}
}
if(kbhit())
choice=getch();
if(choice=='b')
{
while(!kbhit())
{
textcolor(1);
delay(100);
cprintf("A");
}
}
if(kbhit())
choice=getch();
if(choice=='g')
{
while(!kbhit())
{
textcolor(2);
delay(100);
cprintf("A");
}
}
if(kbhit())
choice=getch();
if(choice=='y')
{
while(!kbhit())
{
textcolor(14);
delay(100);
cprintf("A");
}
}
if(kbhit())
choice=getch();
if(choice=='w')
{
while(!kbhit())
{
textcolor(15);
delay(100);
cprintf("A");
}
}
if(kbhit())
choice=getch();
if(choice=='n')
{
while(!kbhit())
{
textcolor(6);
delay(100);
cprintf("A");
}
}
if(kbhit())
choice=getch();
if(choice=='c')
{
while(!kbhit())
{
textcolor(3);
delay(100);
cprintf("A");
}
}
if(kbhit())
choice=getch();
if(choice=='m')
{
while(!kbhit())
{
textcolor(5);
delay(100);
cprintf("A");
}
}
if(kbhit())
choice=getch();
if(choice=='l')
{
while(!kbhit())
{
textcolor(10);
delay(100);
cprintf("A");
}
}
if(kbhit())
choice=getch();
if(choice=='z')
{
while(!kbhit())
{
textcolor(9);
delay(100);
cprintf("A");
}
}
if(kbhit())
choice=getch();
}
while(choice!=27);
}
Output :
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment