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


    1.头文件

    #define _CRT_SECURE_NO_WARNINGS
    #include//_getch()
    #include
    #include
    #include 
    #include//包含多媒体设备接口头文件
    #pragma comment(lib,"winmm.lib")//加载静态库
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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 };//定义一个简谱的数组,可以在网上搜,这个是起风了,虽然有点不太对
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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");//加载按下后键变灰的图片
    
    
    
    
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

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

    4.贴图片函数

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

    在这里插入图片描述

    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);//关闭音乐
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    6.控制函数

    void ctrogame()
    {
    	int input;
    	
    	
    	input = _getch();//input为读取按键的ASCLL值
    	CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)playmusic, (LPVOID)(input - 48), NULL, NULL);//创建线程,处理线程,为了处理按键不能同时按下,使用多线程
    
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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;//回到头重新播放
    	}
    
    
    
    
    
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    8.主函数

    int main()
    {
    	
    	initgame();
    	SetTimer(hWnd, 9527, 200, (TIMERPROC)timer);//200ms进入定时函数里面//定时器id 9527(随便写)hWnd为句柄
    	
    	
    	while (1)
    	{
    		show();
    		ctrogame();
    	}
       
    	return 0;
    
    
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    8.完整代码

    #define _CRT_SECURE_NO_WARNINGS
    #include
    #include
    #include
    #include 
    #include//包含多媒体设备接口头文件
    #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;
    
    
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    9.效果演示

    20231108_192642

  • 相关阅读:
    Qt翻译(本地化)坑总结
    geteway我的一些记录
    单例模式(常用)
    Linux或Centos查看CPU和内存占用情况_top只能查看对应的命令_如何查看具体进程---linux工作笔记062
    【2023/10/16 下午10:32:39】
    程序无法启动,提示“找不到msvcp140.dll”或“msvcp140.dll缺失报错”解决方法
    uniapp—day02
    软件UI自动化测试应该怎么做?对软件产品起到什么作用?
    Vscode LinuxC++环境配置
    string类接口介绍及应用
  • 原文地址:https://blog.csdn.net/yyqzjw/article/details/134293808