【c语言】100行代码搞定电子琴

1.头文件

#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>//_getch()
#include<stdio.h>
#include<graphics.h>
#include <windows.h>
#include<mmsystem.h>//包含多媒体设备接口头文件
#pragma comment(lib,"winmm.lib")//加载静态库

2.变量定义

IMAGE bmp[7];//对应7个键的图片
IMAGE bk;//按键后变灰的图片
int music_idx = 0;
int max;//记录曲子简谱的音的个数
#define WIDTH 100//一个键的宽度
#define HEIGHT 300//界面高度
#define NUM 7//键的数目
HWND hWnd;//用于模拟按键获取消息的句柄
int music[] = {
    
     2,1,2,1,2,3,5,3,2,1,2,1,2,3,2,1,5,0,2,1,2,1,2,3,5,3,2,3,2,1,2,2,0,2,1,2,1,2,3,5,3,2,3,2,1,6,6,0,3,2,1,2,1,0,3,2,1,2,1,0,3,2,1,2,1,0,1,2,3,1
,6,5,6,6,0,1,7,6,7,7,0,7,6,7,7,3,3,1,2,1,7,6,5,6,5,6,6,5,6,5,6,5,2,2,5,3,0,1,2,3,1,6,5,6,6,0,1,7,6,7,7,0,7,6,7,7,3,3,1,2,1,7,6,5,6,3,3,3,5,6,3,3,3,5,6,6,0,1,2,3,6,5,5,6,5,5,6,5,6,5,5,2,3,3,6,5,5,6,5,5,5,6,5,5,3,2,1,6,6,1,1,2,1,6,6,1,3,3,4,3,2,3,2,0,1,2,3,6
,5,5,6,5,5,6,5,5,2,3,6,5,5,6,5,5,6,5,5,6,5,5,3,2,1,6,6,3,2,1,6,6,6,1,1,6,3 };//定义一个简谱的数组,可以在网上搜,这个是起风了,虽然有点不太对

3.初始化函数

void initgame()
{
    
    
	
	hWnd=initgraph(NUM * WIDTH, HEIGHT,SHOWCONSOLE);//获取的模拟按键信息放在这个界面
	char buff[256];
	for (int i = 0; i < NUM; i++)//循环加载7个图片
	{
    
    
		memset(buff, 0, 256);
		sprintf(buff, "images\\%d.bmp", i + 1);
		loadimage(&bmp[i], buff);



	}//这个循环可以通用加载图片
	loadimage(&bk, "images\\down.bmp");//加载按下后键变灰的图片






}

在这里插入图片描述
在这里插入图片描述

4.贴图片函数

void show()
{
    
    
	for (int i = 0; i < NUM; i++)
	{
    
    
		putimage(i * 100, 0, &bmp[i]);//前两个坐标为图片左上角贴在界面的坐标
}

}

在这里插入图片描述

5.播放音乐

void playmusic(int n)//将对应的调传过来
{
    
    
	char buff[50] = {
    
    0};
	sprintf(buff, "open ./%d.MP3 alias m",n);//这里类似于加载图片那里
mciSendString(buff, 0, 0, 0);//打开音乐
mciSendString("play m", 0, 0, 0);//播放音乐
putimage((n - 1) * 100, 0, &bk);//哪个按下就贴对应灰图片
Sleep(250);//睡眠250ms
putimage((n - 1) * 100, 0, &bmp[n-1]);//将贴的灰图片又用原来的代替
mciSendString("close m", NULL, NULL, NULL);//关闭音乐
}

6.控制函数

void ctrogame()
{
    
    
	int input;
	
	
	input = _getch();//input为读取按键的ASCLL值
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)playmusic, (LPVOID)(input - 48), NULL, NULL);//创建线程,处理线程,为了处理按键不能同时按下,使用多线程



}

7.定时器函数

void timer()
{
    
    
	max = sizeof(music) / sizeof(int);//个数
	SendMessage(hWnd,WM_KEYDOWN,(WPARAM)(music[music_idx]+48),NULL);//句柄//按键按下//对应音的音节+48对应的ascll值
	music_idx++;处理下一个音节
	if (music_idx >= max)
	{
    
    
		music_idx = 0;//回到头重新播放
	}







}

8.主函数

int main()
{
    
    
	
	initgame();
	SetTimer(hWnd, 9527, 200, (TIMERPROC)timer);//200ms进入定时函数里面//定时器id 9527(随便写)hWnd为句柄
	
	
	while (1)
	{
    
    
		show();
		ctrogame();
	}
   
	return 0;




}

8.完整代码

#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include <windows.h>
#include<mmsystem.h>//包含多媒体设备接口头文件
#pragma comment(lib,"winmm.lib")//加载静态库
IMAGE bmp[7];
IMAGE bk;
int music_idx = 0;
int max;
#define WIDTH 100
#define HEIGHT 300
#define NUM 7
HWND hWnd;
int music[] = {
    
     2,1,2,1,2,3,5,3,2,1,2,1,2,3,2,1,5,0,2,1,2,1,2,3,5,3,2,3,2,1,2,2,0,2,1,2,1,2,3,5,3,2,3,2,1,6,6,0,3,2,1,2,1,0,3,2,1,2,1,0,3,2,1,2,1,0,1,2,3,1
,6,5,6,6,0,1,7,6,7,7,0,7,6,7,7,3,3,1,2,1,7,6,5,6,5,6,6,5,6,5,6,5,2,2,5,3,0,1,2,3,1,6,5,6,6,0,1,7,6,7,7,0,7,6,7,7,3,3,1,2,1,7,6,5,6,3,3,3,5,6,3,3,3,5,6,6,0,1,2,3,6,5,5,6,5,5,6,5,6,5,5,2,3,3,6,5,5,6,5,5,5,6,5,5,3,2,1,6,6,1,1,2,1,6,6,1,3,3,4,3,2,3,2,0,1,2,3,6
,5,5,6,5,5,6,5,5,2,3,6,5,5,6,5,5,6,5,5,6,5,5,3,2,1,6,6,3,2,1,6,6,6,1,1,6,3 };


void timer()
{
    
    
	max = sizeof(music) / sizeof(int);
	SendMessage(hWnd,WM_KEYDOWN,(WPARAM)(music[music_idx]+48),NULL);
	music_idx++;
	if (music_idx >= max)
	{
    
    
		music_idx = 0;
	}







}
void initgame()
{
    
    
	
	hWnd=initgraph(NUM * WIDTH, HEIGHT,SHOWCONSOLE);
	char buff[256];
	for (int i = 0; i < NUM; i++)
	{
    
    
		memset(buff, 0, 256);
		sprintf(buff, "images\\%d.bmp", i + 1);
		loadimage(&bmp[i], buff);



	}
	loadimage(&bk, "images\\down.bmp");






}
void playmusic(int n)
{
    
    
	char buff[50] = {
    
    0};
	sprintf(buff, "open ./%d.MP3 alias m",n);
mciSendString(buff, 0, 0, 0);
mciSendString("play m", 0, 0, 0);
putimage((n - 1) * 100, 0, &bk);
Sleep(250);
putimage((n - 1) * 100, 0, &bmp[n-1]);
mciSendString("close m", NULL, NULL, NULL);
}
void ctrogame()
{
    
    
	int input;
	
	
	input = _getch();
	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)playmusic, (LPVOID)(input - 48), NULL, NULL);//创建线程,处理线程



}
void show()
{
    
    
	for (int i = 0; i < NUM; i++)
	{
    
    
		putimage(i * 100, 0, &bmp[i]);




	}




}

int main()
{
    
    
	
	initgame();
	SetTimer(hWnd, 9527, 200, (TIMERPROC)timer);//100ms//定时器id 9527
	
	
	while (1)
	{
    
    
		show();
		ctrogame();
	}
   
	return 0;




}

9.效果演示

20231108_192642

猜你喜欢

转载自blog.csdn.net/yyqzjw/article/details/134293808
今日推荐