C program for piano game is program to implement piano game in c.

Piano game
We use our keyboard as a piano and pressing key will generate different types of sound. for this program, we have to include the dos.h header file in the program. This file provides functions for producing sounds.
To develop piano in c we use some different functions from dos.h which are as follows

Sound function in c:
This function is used to produce sound with different frequencies.
Syntax:
sound(Duration);
Example: sound(1200)

Delay function in c:
This function is used to delay sound generated by sound.
Syntax:
delay(duration);
Example :- delay(1000);

No sound function in c:
This function is used to end the sound.
Syntax:
nosound ()

Algorithm for Piano game

  1. Take input that user want to play or stop
  2. Write switch case with different keyboard key
  3. Write a sound function in the case with different number

C program for piano game

#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
char ch;

do {
printf("Welcome to piano game\n");
printf("press keys from a to i for sound and press x to exit\n");
scanf("%c",&ch);
switch(ch)
{
printf("Playing music\n");
case 'a':
sound(2000);
delay(500);
nosound();
break;
case 'b':
printf("Playing music\n");
sound(2200);
delay(500);
nosound();
break;
case 'c':
printf("Playing music\n");
sound(2400);
delay(500);
nosound();
break;
case 'd':
printf("Playing music\n");
sound(2500);
delay(500);
nosound();
break;
case 'e':
printf("Playing music\n");
sound(2600);
delay(500);
nosound();
break;
case 'f':
printf("Playing music\n");
sound(2700);
delay(500);
nosound();
break;
case 'g':
printf("Playing music\n");
sound(2800);
delay(500);
nosound();
break;
case 'h':
printf("Playing music\n");
sound(2900);
delay(500);
nosound();
break;
case 'i':
printf("Playing music\n");
sound(3000);
delay(500);
nosound();
break;
case 'x':
exit(0);
}
}while(ch!=x);
getch();
}

Output :

C program for piano game

Write A Comment