标签:most sdn lock logo 思路 cli 黄色 fscanf 全局变量
本项目已开源:https://github.com/wmpscc/AvoidBlank
简单示例
#include <graphics.h>
int main()
{
//初始化为640*480大小
initgraph(640, 480);
//等待用户按键
getch();
//关闭图形界面
closegraph();
return 0;
}
开始之前先介绍一下相关点。
本篇文章假定你已经准备好游戏每个按键对应的音频文件和游戏关卡的歌词按键谱。
setinitmode(RENDER_MANUAL); //设置窗口模式
功能:
这个函数用于设置初始化图形的选项和模式。声明:
void setinitmode(int mode, int x = CW_USEDEFAULT, int y = CW_USEDEFAULT);参数:
mode
初始化模式,是二进制组合的值。如果为INIT_DEFAULT 表示使用默认值。
可以使用的值的组合:
INIT_NOBORDER 为无边框窗口
INIT_CHILD 为子窗口(需要使用attachHWND指定要依附的父窗口,此函数不另说明)
INIT_TOPMOST 使窗口总在最前
INIT_RENDERMANUAL 手动更新标志,即调用delay_fps/delay_ms等会等待操作的函数时会更新窗口,否则保持窗口内容
INIT_WITHLOGO 使initgraph的时候显示开场动画logo
INIT_NOFORCEEXIT 使关闭窗口的时候不强制退出程序,但窗口会消失,需要配合is_run函数
INIT_DEFAULT 默认参数,不调用本函数时即使用此参数
INIT_ANIMATION 是INIT_DEFAULT, INIT_RENDERMANUAL, INIT_NOFORCEEXIT的组合,用于动画编写x, y
初始化时窗口左上角在屏幕的坐标,默认为系统分配。返回值:
(无)说明:
本函数只能在initgraph前调用。
initgraph(WINDOW_WIDTH, WINDOW_HIGHT, INIT_RENDERMANUAL); //初始化窗口
功能:
这个函数用于初始化绘图环境。声明:
void initgraph(
int Width,
int Height,
int Flag = INIT_DEFAULT
);
void initgraph(
int* gdriver,
int* gmode,
char* path
); // 兼容 Borland C++ 3.1 的重载,不建议使用。
- 参数:
Width
绘图环境的宽度。如果为-1,则使用屏幕的宽度Height
绘图环境的高度。如果为-1,则使用屏幕的高度Style
请留空,为保留参数- 返回值:
(无)
功能:
这个函数用于设置当前背景色。并且会把当前图片上是原背景色的像素,转变为新的背景色。声明:
void setbkcolor(
color_t color,
PIMAGE pimg = NULL
);
参数:
color
指定要设置的背景颜色。注意,该设置会同时影响文字背景色。返回值:
(无)
setfillstyle(SOLID_FILL, Blank_COLOR); //设置方块颜色
功能:
这个函数用于设置当前填充类型。该函数的自定义填充部分尚不支持。声明:
void setfillstyle(
int pattern,
color_t color,
PIMAGE pimg = NULL
);
- 参数:
- pattern
填充类型,可以是以下宏或值:NULL_FILL 不填充
SOLID_FILL 固实填充
pupattern
color
填充颜色。
- 返回值:
(无)
功能:
这个函数用于画填充的多边形。边线颜色由setcolor函数决定,填充颜色由setfillstyle函数决定声明:
void fillpoly(
int numpoints,
const int *polypoints,
PIMAGE pimg = NULL
);
- 参数:
numpoints
多边形点的个数。polypoints
每个点的坐标,数组元素个数为 numpoints * 2。
该函数会自动连接多边形首尾。
- 返回值:
(无)
功能:
这个函数用于设置当前字体样式。声明:
void setfont(
int nHeight,
int nWidth,
LPCSTR lpszFace,
PIMAGE pimg = NULL
);
void setfont(
int nHeight,
int nWidth,
LPCSTR lpszFace,
int nEscapement,
int nOrientation,
int nWeight,
bool bItalic,
bool bUnderline,
bool bStrikeOut,
PIMAGE pimg = NULL
);
void setfont(
int nHeight,
int nWidth,
LPCSTR lpszFace,
int nEscapement,
int nOrientation,
int nWeight,
bool bItalic,
bool bUnderline,
bool bStrikeOut,
BYTE fbCharSet,
BYTE fbOutPrecision,
BYTE fbClipPrecision,
BYTE fbQuality,
BYTE fbPitchAndFamily,
PIMAGE pimg = NULL
);
void setfont(
const LOGFONT *font,
PIMAGE pimg = NULL
);
outtextxy(WINDOW_WIDTH / 2, 0, mCharsScore); //在窗口输出字体
功能:
这个函数用于在指定位置输出字符串。声明:
void outtextxy(
int x,
int y,
LPCSTR textstring,
PIMAGE pimg = NULL
);
void outtextxy(
int x,
int y,
CHAR c,
PIMAGE pimg = NULL
);
void outtextxy(
int x,
int y,
LPCWSTR textstring,
PIMAGE pimg = NULL
);
void outtextxy(
int x,
int y,
WCHAR c,
PIMAGE pimg = NULL
);
参数:
x
字符串输出时头字母的 x 轴的坐标值
y
字符串输出时头字母的 y 轴的坐标值。
textstring
要输出的字符串的指针。
c
要输出的字符。返回值:
(无)
random(max); //产生[0,max)的随机数,注意不包括最大值
filename 为要播放的文件的路径名
mciSendString(filename, NULL, 0, NULL); //低效播放方式
//高效率无阻塞播放
ThreadParameter *tp = (ThreadParameter *)pParam;
MCI_OPEN_PARMS open;//定义打开结构体变量
open.lpstrElementName = filename;//填充参数
mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT, DWORD_PTR(&open));//打开
MCI_PLAY_PARMS play;//定义播放结构题变量
mciSendCommand(open.wDeviceID, MCI_PLAY, 0, DWORD_PTR(&play));//非阻塞式播放
举个例子
typedef struct ThreadParameter { //音频播放线程参数
char filename[200];
char key[30];
};
ThreadParameter tpp; //音频播放线程传参结构体<全局变量>
DWORD WINAPI music_play(LPVOID pParam) //播放音乐
{
//高效率无阻塞播放
ThreadParameter *tp = (ThreadParameter *)pParam;
MCI_OPEN_PARMS open;//定义打开结构体变量
open.lpstrElementName = tp->filename;//填充参数
mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT, DWORD_PTR(&open));//打开
MCI_PLAY_PARMS play;//定义播放结构题变量
mciSendCommand(open.wDeviceID, MCI_PLAY, 0, DWORD_PTR(&play));//非阻塞式播放
return 0;
}
启动线程
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tpp, 0, NULL); //启动音乐播放线程
解释如下:
结构体是用来传参数的,线程函数返回值类型必须为DWORD WINAPI,函数参数必须为LPVOID 类型。形式如下:
启动函数第四个参数传入传参结构体指针,此处为&tpp。形式如下:
FILE *fp = NULL;
fp = fopen(filename, "r"); //filename为要打开的文件路径
我已经准备好每个按键的音频文件和每个关卡对应音乐的键谱了,相关定义如下
#define Path_Music_resPiano "..//resplus//piano" //载入键音
#define Path_Music_Fail "..//resplus//fail.mp3"
#define Path_Music_Launch "..//resplus//chuanshao.mp3"
#define Path_pre "..//resplus//piano//" //音频路径前缀
#define Path_end ".mp3" //音频路径后缀
//每个关卡对应键谱
char Path_A_Breeze_From_Alabama[] = "..//resplus//A Breeze From Alabama.txt";
char Path_Happy_New_Year[] = "..//resplus//Happy New Year.txt";
char Path_Jasmine[] = "..//resplus//Jasmine.txt";
char Path_Little_Star[] = "..//resplus//Little Star.txt";
char Path_lyricwaltz[] = "..//resplus//lyricwaltz.txt";
char Path_Merry_Christmas[] = "..//resplus//Merry Christmas.txt";
int main() //主函数
{
//--------播放启动音乐----------begin
ThreadParameter tp;
strcpy(tp.filename, Path_Music_Launch);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL);
//--------播放启动音乐----------end
//------------初始化------------begin
setinitmode(RENDER_MANUAL);
initgraph(WINDOW_WIDTH, WINDOW_HIGHT, INIT_RENDERMANUAL); //初始化窗口
strcpy(Path_Music, Path_Jasmine); //默认关卡
//------------初始化------------end
//----------选择关卡------------begin
setfont(20, 0, "宋体");
outtextxy(10, 10, "按下对应字母进入关卡");
outtextxy(10, 35, "A: A Breeze From Alabama");
outtextxy(10, 55, "B: 茉莉花");
outtextxy(10, 75, "C: Happy New Year");
outtextxy(10, 95, "D: Little Star");
outtextxy(10, 115, "E: lyricwaltz");
outtextxy(10, 135, "F: Merry Christmas");
switch (getch())
{
case 'A':
strcpy(Path_Music, Path_A_Breeze_From_Alabama);
break;
case 'B':
strcpy(Path_Music, Path_Jasmine);
break;
case 'C':
strcpy(Path_Music, Path_Happy_New_Year);
break;
case 'D':
strcpy(Path_Music, Path_Little_Star);
break;
case 'E':
strcpy(Path_Music, Path_lyricwaltz);
break;
case 'F':
strcpy(Path_Music, Path_Merry_Christmas);
break;
default:
break;
}
//----------选择关卡------------end
initFile(); //读入相关文件
creatView(); //创建地图
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GameControl, NULL, 0, NULL); //启动键盘控制线程
GameView(); //绘图
getch(); //防止游戏结束后退出
return 0;
}
键谱文件中的内容(片段供参考)
d2 d2 #f2 e2 a1 #c2 e2 d2 d (#f.a) (#f.a) d (#f.a) (#f.a) d (#f.a) (#f.a) A-1 (e.g) (e.g)
对应的音频文件名为黄色部分
==d2== ==d== (==#f==.==a==) ==A-1==
char track[1024][12]; //乐谱
int mLength = 0; //键总数
void initFile() //初始化文件资源
{
FILE *fp = NULL;
fp = fopen(Path_Music, "r");
if (fp != NULL)
{
while (fscanf(fp, "%s", track[mLength]) != EOF)
{ //读入所有键
mLength++;
}
}
fclose(fp);
}
从文件中读入每个按键的标识码存到数组track中,并记录有多少个键存到变量mLength中。
根据总按键数创建一个尺寸为窗口宽度(按键数黑块高度)的PIMAGE对象。生成1到4的随机数,对应的数为所在的轨道。按照所在轨道在总画布上错位绘制黑块
PIMAGE totalimg; //总画布存储对象 <全局变量>
int randtrack[3000] = { -1000 }; //存轨道的随机数
void creatView()
{
int point[8]; //四边形点集
int x = 0, y = 0; //基定点坐标
randomize(); //初始化随机数
totalimg = newimage(WINDOW_WIDTH, Blank_HIGHT*mLength); //初始化总画布
setbkcolor(COLOR_WHITE, totalimg); //设置总画布背景
cleardevice(totalimg); //清空总画布
for (int i = 0; i < mLength; i++)
{
randtrack[i] = random(4) + 1; //参数0-3的随机数,加一后为轨道位置
y = inter_y;
x = (randtrack[i] - 1) * Blank_WIDTH;
point[0] = x;
point[1] = y;
point[2] = x + Blank_WIDTH;
point[3] = y;
point[4] = x + Blank_WIDTH;
point[5] = y + Blank_HIGHT;
point[6] = x;
point[7] = y + Blank_HIGHT;
setfillstyle(SOLID_FILL, Blank_COLOR); //设置方块颜色
fillpoly(4, point, totalimg); //绘制方块
inter_y += Blank_HIGHT; //设置滑块公共高度
}
}
根据按键以及之前的随机数数组即答案来计分
由于监听按键和绘制动画是异步操作的,所有我们给按键控制单独开一个线程来监听。线程的使用在上面已经介绍过了,下面来看看代码。
DWORD WINAPI GameControl() //控制
{
int curr = 0;
int k = 0, index = 0;
ThreadParameter tp;
strcpy(tp.filename, Path_Music_Fail); //给线程传参结构体赋值失败音乐路径
for (; k != key_esc;)
{
if (kbhit())
{
k = getch();
if (k == 'S')
{
if (randtrack[mLength - curr - 1] == 1)
{
music_play_control(track[index++]); //将键音传入音乐处理函数
mScore++; //得分加一
drawCountText(); //刷新计分标签
}
else
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL); //播放失败音乐线程
failplay = true; //设定失败标志为真
}
}
else if (k == 'D')
{
if (randtrack[mLength - curr - 1] == 2)
{
music_play_control(track[index++]);
mScore++;
drawCountText();
}
else
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL);
failplay = true;
}
}
else if (k == '4')
{
if (randtrack[mLength - curr - 1] == 3)
{
music_play_control(track[index++]);
mScore++;
drawCountText();
}
else
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL);
failplay = true;
}
}
else if (k == '5')
{
if (randtrack[mLength - curr - 1] == 4)
{
music_play_control(track[index++]);
mScore++;
drawCountText();
}
else
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL);
failplay = true;
}
}
curr++;
accelerate = mScore / 25; //设定滑块每25分增加速度
}
if (failplay)
{
api_sleep(500); //等待播放音乐线程启动
return 0;
}
}
return 0;
}
代码中用大写字母S、D和数字键4、5分别控制四条轨道,可随意修改。
failpaly为游戏失败标志,若为真则播放失败音乐,延迟500毫秒后结束线程。
void drawCountText() //绘制计数器
{
sprintf(mCharsScore, "%d", mScore); //int转char*
setfont(40, 0, "宋体"); //设定字体样式
setfontbkcolor(EGERGB(0xff, 0x00, 0x00)); //设置字体背景色
outtextxy(WINDOW_WIDTH / 2, 0, mCharsScore); //在窗口输出字体
}
预处理(写法比较暴力哈)
void music_play_control(char key[]) //控制音乐播放
{
int flage = 0;
char copy[30];
char buff[10][30]; //同时按下的键值
char temp[200];
memset(temp, '\0', sizeof(temp));
//按照特定格式读入每个键符
if (key[0] != '(')
{
strcat(temp, Path_pre);
strcat(temp, key);
strcat(temp, Path_end);
strcpy(tpp.filename, temp);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tpp, 0, NULL);
}
else
{
memset(buff, '\0', sizeof(buff));
strcpy(copy, key);
for (int i = 0; i < strlen(copy); i++)
{
if (copy[i] == '.')
{
flage++;
copy[i] = ' ';
}
else if (copy[i] == '(' || copy[i] == ')')
{
copy[i] = ' ';
}
}
if (flage == 0)
{
sscanf(copy, "%s", buff[0]);
}
else if (flage == 1)
{
sscanf(copy, "%s %s", buff[0], buff[1]);
}
else if (flage == 2)
{
sscanf(copy, "%s %s %s", buff[0], buff[1], buff[2]);
}
else if (flage == 3)
{
sscanf(copy, "%s %s %s %s", buff[0], buff[1], buff[2], buff[3]);
}
for (int i = 0; i < flage; i++)
{
strcat(temp, Path_pre);
strcat(temp, buff[i]);
strcat(temp, Path_end);
strcpy(tpp.filename, temp);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tpp, 0, NULL); //启动音乐播放线程
api_sleep(200);
}
}
}
实际播放音乐的线程函数
DWORD WINAPI music_play(LPVOID pParam) //播放音乐
{
//mciSendString(tp->filename, NULL, 0, NULL); //低效播放方式
//高效率无阻塞播放
ThreadParameter *tp = (ThreadParameter *)pParam;
MCI_OPEN_PARMS open;//定义打开结构体变量
open.lpstrElementName = tp->filename;//填充参数
mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT, DWORD_PTR(&open));//打开
MCI_PLAY_PARMS play;//定义播放结构题变量
mciSendCommand(open.wDeviceID, MCI_PLAY, 0, DWORD_PTR(&play));//非阻塞式播放
return 0;
}
void GameView() // 绘图
{
randomize(); //初始化随机数
cleardevice(); //清屏
setcaption("别踩白块 - 1707004544"); //窗口标题
setbkcolor(EGERGB(0xff, 0xff, 0xff)); //背景色
ThreadDrawTrack();
}
游戏启动后,使总画布左下角定点与窗口(0,0)顶点对齐
使总画布在可见区域由底向上绘制,并记录下移坐标。
根据下移的坐标除以黑块的高度可确定是哪个方块
DWORD WINAPI ThreadDrawTrack()
{
setrendermode(RENDER_MANUAL);
int y = inter_y;
int addh = 0;
ThreadParameter tp;
strcpy(tp.filename, Path_Music_Fail);
for (; is_run(); delay_fps(FPS))
{
if (failplay)
{
cleardevice();
sprintf(mCharsScore, "%d", mScore);
setfont(100, 0, "宋体");
setfontbkcolor(EGERGB(0xff, 0x00, 0x00));
outtextxy(WINDOW_WIDTH / 2 + 20, WINDOW_HIGHT / 2, mCharsScore);
outtextxy(0, WINDOW_HIGHT / 2, "得分:");
break;
}
if (-1 * y >= 0)
{
break;
}
putimage(0, -1 * y, totalimg);
y -= 2 + accelerate;
below = addh - mScore*Blank_HIGHT - 160;
if (below >= WINDOW_HIGHT)
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL);
failplay = true;
}
addh += 2;
drawCountText(); //更新显示得分
}
return 1;
}
个人感觉EGE绘制效率确实有点低,这是我写的五总方法中画面最流畅,且最简单的方法。缺点也很明显,不能单独控制每个按键。
欢迎关注我的github。
我也是初学者,代码上有很多不足欢迎提出指正,感谢阅览。
完整实现代码如下
#include <graphics.h>
#include <math.h>
#include<string.h>
#include<stdio.h>
#include <windows.h>
#pragma comment(lib, "Winmm.lib") //For MCI(Media Control Interface,媒体控制接口)
#define Path_Music_resPiano "..//resplus//piano" //载入键音
#define Path_Music_Fail "..//resplus//fail.mp3"
#define Path_Music_Launch "..//resplus//chuanshao.mp3"
#define Path_pre "..//resplus//piano//" //音频路径前缀
#define Path_end ".mp3" //音频路径后缀
#define WINDOW_HIGHT 800 //窗口高度
#define WINDOW_WIDTH 480 //窗口宽度
#define Blank_HIGHT 160 //方块高度
#define Blank_WIDTH 120 //方块宽度
#define Blank_COLOR EGERGB(00,00,00) //方块颜色
#define COLOR_WHITE EGERGB(0xFF, 0xFF, 0xFF) //白色
#define FPS 100
#define SLEEP 10
//-------函数声明--------
void drawCountText(); //绘制计数器
void initFile(); //初始化文件资源
DWORD WINAPI music_play(LPVOID pParam); //播放音乐
void music_play_control(char key[]); //控制音乐播放
DWORD WINAPI GameControl(); //控制
void GameView(); // 绘图
void creatView(); //创建地图
//-------全局变量--------
int mScore = 0; //分数
char mCharsScore[50]; //分数字符串
char track[1024][12]; //乐谱
int mLength = 0; //键总数
int below = 0; //最下面一个方块定点的y轴
PIMAGE totalimg;
int randtrack[3000] = { -1000 }; //存轨道的随机数
int inter_y = 0; //生成图片时用到的y
bool failplay = false;
int accelerate = 0;
char Path_A_Breeze_From_Alabama[] = "..//resplus//A Breeze From Alabama.txt";
char Path_Happy_New_Year[] = "..//resplus//Happy New Year.txt";
char Path_Jasmine[] = "..//resplus//Jasmine.txt";
char Path_Little_Star[] = "..//resplus//Little Star.txt";
char Path_lyricwaltz[] = "..//resplus//lyricwaltz.txt";
char Path_Merry_Christmas[] = "..//resplus//Merry Christmas.txt";
char Path_Music[50];
typedef struct ThreadParameter { //音频播放线程参数
char filename[200];
char key[30];
};
ThreadParameter tpp; //音频播放线程传参结构体
int main() //主函数
{
//--------播放启动音乐----------begin
ThreadParameter tp;
strcpy(tp.filename, Path_Music_Launch);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL);
//--------播放启动音乐----------end
//------------初始化------------begin
setinitmode(RENDER_MANUAL);
initgraph(WINDOW_WIDTH, WINDOW_HIGHT, INIT_RENDERMANUAL); //初始化窗口
strcpy(Path_Music, Path_Jasmine); //默认关卡
//------------初始化------------end
//----------选择关卡------------begin
setfont(20, 0, "宋体");
outtextxy(10, 10, "按下对应字母进入关卡");
outtextxy(10, 35, "A: A Breeze From Alabama");
outtextxy(10, 55, "B: 茉莉花");
outtextxy(10, 75, "C: Happy New Year");
outtextxy(10, 95, "D: Little Star");
outtextxy(10, 115, "E: lyricwaltz");
outtextxy(10, 135, "F: Merry Christmas");
switch (getch())
{
case 'A':
strcpy(Path_Music, Path_A_Breeze_From_Alabama);
break;
case 'B':
strcpy(Path_Music, Path_Jasmine);
break;
case 'C':
strcpy(Path_Music, Path_Happy_New_Year);
break;
case 'D':
strcpy(Path_Music, Path_Little_Star);
break;
case 'E':
strcpy(Path_Music, Path_lyricwaltz);
break;
case 'F':
strcpy(Path_Music, Path_Merry_Christmas);
break;
default:
break;
}
//----------选择关卡------------end
initFile(); //读入相关文件
creatView(); //创建地图
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GameControl, NULL, 0, NULL); //启动键盘控制线程
GameView(); //绘图
getch(); //防止游戏结束后退出
return 0;
}
void initFile() //初始化文件资源
{
FILE *fp = NULL;
fp = fopen(Path_Music, "r");
if (fp != NULL)
{
while (fscanf(fp, "%s", track[mLength]) != EOF)
{ //读入所有键
mLength++;
}
}
fclose(fp);
}
DWORD WINAPI ThreadDrawTrack()
{
setrendermode(RENDER_MANUAL);
int y = inter_y;
int addh = 0;
ThreadParameter tp;
strcpy(tp.filename, Path_Music_Fail);
for (; is_run(); delay_fps(FPS))
{
if (failplay)
{
cleardevice();
sprintf(mCharsScore, "%d", mScore);
setfont(100, 0, "宋体");
setfontbkcolor(EGERGB(0xff, 0x00, 0x00));
outtextxy(WINDOW_WIDTH / 2 + 20, WINDOW_HIGHT / 2, mCharsScore);
outtextxy(0, WINDOW_HIGHT / 2, "得分:");
break;
}
if (-1 * y >= 0)
{
break;
}
putimage(0, -1 * y, totalimg);
y -= 2 + accelerate;
below = addh - mScore*Blank_HIGHT - 160;
if (below >= WINDOW_HIGHT)
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL);
failplay = true;
}
addh += 2;
drawCountText(); //更新显示得分
}
return 1;
}
void GameView() // 绘图
{
randomize(); //初始化随机数
cleardevice(); //清屏
setcaption("别踩白块 - 1707004544 李浩"); //窗口标题
setbkcolor(EGERGB(0xff, 0xff, 0xff)); //背景色
ThreadDrawTrack();
}
DWORD WINAPI GameControl() //控制
{
int curr = 0;
int k = 0, index = 0;
ThreadParameter tp;
strcpy(tp.filename, Path_Music_Fail); //给线程传参结构体赋值失败音乐路径
for (; k != key_esc;)
{
if (kbhit())
{
k = getch();
if (k == 'S')
{
if (randtrack[mLength - curr - 1] == 1)
{
music_play_control(track[index++]); //将键音传入音乐处理函数
mScore++; //得分加一
drawCountText(); //刷新计分标签
}
else
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL); //播放失败音乐线程
failplay = true; //设定失败标志为真
}
}
else if (k == 'D')
{
if (randtrack[mLength - curr - 1] == 2)
{
music_play_control(track[index++]);
mScore++;
drawCountText();
}
else
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL);
failplay = true;
}
}
else if (k == '4')
{
if (randtrack[mLength - curr - 1] == 3)
{
music_play_control(track[index++]);
mScore++;
drawCountText();
}
else
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL);
failplay = true;
}
}
else if (k == '5')
{
if (randtrack[mLength - curr - 1] == 4)
{
music_play_control(track[index++]);
mScore++;
drawCountText();
}
else
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tp, 0, NULL);
failplay = true;
}
}
curr++;
accelerate = mScore / 25; //设定滑块每25分增加速度
}
if (failplay)
{
api_sleep(500); //等待播放音乐线程启动
return 0;
}
}
return 0;
}
void drawCountText() //绘制计数器
{
sprintf(mCharsScore, "%d", mScore); //int转char*
setfont(40, 0, "宋体"); //设定字体样式
setfontbkcolor(EGERGB(0xff, 0x00, 0x00)); //设置字体背景色
outtextxy(WINDOW_WIDTH / 2, 0, mCharsScore); //在窗口输出字体
}
void creatView()
{
int point[8]; //四边形点集
int x = 0, y = 0; //基定点坐标
randomize(); //初始化随机数
totalimg = newimage(WINDOW_WIDTH, Blank_HIGHT*mLength); //初始化总画布
setbkcolor(COLOR_WHITE, totalimg); //设置总画布背景
cleardevice(totalimg); //清空总画布
for (int i = 0; i < mLength; i++)
{
randtrack[i] = random(4) + 1; //参数0-3的随机数,加一后为轨道位置
y = inter_y;
x = (randtrack[i] - 1) * Blank_WIDTH;
point[0] = x;
point[1] = y;
point[2] = x + Blank_WIDTH;
point[3] = y;
point[4] = x + Blank_WIDTH;
point[5] = y + Blank_HIGHT;
point[6] = x;
point[7] = y + Blank_HIGHT;
setfillstyle(SOLID_FILL, Blank_COLOR); //设置方块颜色
fillpoly(4, point, totalimg); //绘制方块
inter_y += Blank_HIGHT; //设置滑块公共高度
}
}
void music_play_control(char key[]) //控制音乐播放
{
int flage = 0;
char copy[30];
char buff[10][30]; //同时按下的键值
char temp[200];
memset(temp, '\0', sizeof(temp));
//按照特定格式读入每个键符
if (key[0] != '(')
{
strcat(temp, Path_pre);
strcat(temp, key);
strcat(temp, Path_end);
strcpy(tpp.filename, temp);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tpp, 0, NULL);
}
else
{
memset(buff, '\0', sizeof(buff));
strcpy(copy, key);
for (int i = 0; i < strlen(copy); i++)
{
if (copy[i] == '.')
{
flage++;
copy[i] = ' ';
}
else if (copy[i] == '(' || copy[i] == ')')
{
copy[i] = ' ';
}
}
if (flage == 0)
{
sscanf(copy, "%s", buff[0]);
}
else if (flage == 1)
{
sscanf(copy, "%s %s", buff[0], buff[1]);
}
else if (flage == 2)
{
sscanf(copy, "%s %s %s", buff[0], buff[1], buff[2]);
}
else if (flage == 3)
{
sscanf(copy, "%s %s %s %s", buff[0], buff[1], buff[2], buff[3]);
}
for (int i = 0; i < flage; i++)
{
strcat(temp, Path_pre);
strcat(temp, buff[i]);
strcat(temp, Path_end);
strcpy(tpp.filename, temp);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)music_play, &tpp, 0, NULL); //启动音乐播放线程
api_sleep(200);
}
}
}
DWORD WINAPI music_play(LPVOID pParam) //播放音乐
{
//mciSendString(tp->filename, NULL, 0, NULL); //低效播放方式
//高效率无阻塞播放
ThreadParameter *tp = (ThreadParameter *)pParam;
MCI_OPEN_PARMS open;//定义打开结构体变量
open.lpstrElementName = tp->filename;//填充参数
mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT, DWORD_PTR(&open));//打开
MCI_PLAY_PARMS play;//定义播放结构题变量
mciSendCommand(open.wDeviceID, MCI_PLAY, 0, DWORD_PTR(&play));//非阻塞式播放
return 0;
}
标签:most sdn lock logo 思路 cli 黄色 fscanf 全局变量
原文地址:https://www.cnblogs.com/ishero/p/11136284.html