一个低配版本的钢琴大师,没有其它花里胡哨的操作,也没有游戏结束的判断,无聊的玩玩吧,一个人玩还是挺有意思的,哈哈 ~
游戏中只有一个背景素材、一首音乐,其它的元素都是通过 EasyX图形库中的 api 画出来的,希望能够帮助爱学习的你!
游戏效果如下所示:
钢琴大师
代码如下所示:
#include <iostream>
#include <graphics.h>
#include <mmsystem.h>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#pragma comment (lib,"winmm")
class Piano
{
private:
int speed;
const static int NUM = 6;
const int WIDTH;
const int HEIGHT;
struct Coord
{
int x;
int y;
};
Coord arr[NUM];
char ch; //接收按下的字符
public:
Piano(int n);
void InitGame(); // 初始化
void DiamondsDown(); //方块下落
void Play(); //玩游戏
void DrawLine() //画线
{
line(0, HEIGHT - 99, WIDTH, HEIGHT - 99);
line(0, HEIGHT - 100, WIDTH, HEIGHT - 100);
line(0, HEIGHT - 101, WIDTH, HEIGHT - 101);
}
};
int main()
{
srand((unsigned)time(nullptr));
Piano piano(0);
piano.InitGame();
DWORD t1 = GetTickCount(), tt1;
while (1)
{
BeginBatchDraw();
tt1 = GetTickCount();
if (tt1 - t1 > 10)
{
piano.DiamondsDown();
t1 = tt1;
}
piano.DrawLine();
if (kbhit())
piano.Play();
EndBatchDraw();
}
return 0;
}
Piano::Piano(int n) :speed(n), WIDTH(640), HEIGHT(480)
{
for (int i = 0; i < NUM; i++)
{
arr[i].x = i * 106 + 5;
arr[i].y = rand() % HEIGHT;
}
}
void Piano::InitGame()
{
initgraph(WIDTH, HEIGHT);
mciSendString("open bj.mp3", 0, 0, 0);
mciSendString("play bj.mp3 repeat", 0, 0, 0);
loadimage(nullptr, "bj.jpg", WIDTH, HEIGHT);
std::cin.get();
setbkcolor(RGB(192, 192, 192));
cleardevice();
settextcolor(RED);
settextstyle(50, 30, "楷体");
outtextxy(80, 50, "钢 琴 大 师");
outtextxy(450, 120, "浪");
outtextxy(450, 200, "子");
outtextxy(450, 280, "花");
outtextxy(450, 360, "梦");
settextstyle(30, 18, "楷体");
outtextxy(50, 500, "人生百味");
outtextxy(200, 550, "愿岁月无恙");
outtextxy(80, 150, "游戏模式:");
outtextxy(200, 220, "简单");
outtextxy(200, 300, "一般");
outtextxy(200, 380, "困难");
MOUSEMSG msg;
while (1)
{
msg = GetMouseMsg();
settextcolor(RGB(0, 255, 255));
if (msg.x - 200 < 73 && msg.x - 200 > 0 && msg.y - 215 < 38 && msg.y - 215 > 0)
{
outtextxy(200, 220, "简单");
if (msg.mkLButton)
{
speed = 1;
break;
}
}
else if (msg.x - 200 < 73 && msg.x - 200 > 0 && msg.y - 298 < 35 && msg.y - 298 > 0)
{
outtextxy(200, 300, "一般");
if (msg.mkLButton)
{
speed = 3;
break;
}
}
else if (msg.x - 200 < 73 && msg.x - 200 > 0 && msg.y - 380 < 30 && msg.y - 380 > 0)
{
outtextxy(200, 380, "困难");
if (msg.mkLButton)
{
speed = 5;
break;
}
}
else
{
settextcolor(RED);
outtextxy(200, 220, "简单");
outtextxy(200, 300, "一般");
outtextxy(200, 380, "困难");
}
}
cleardevice();
setfillcolor(BLACK);
setlinecolor(RED);
for (int i = 0; i < NUM; i++)
line(i * 106, 0, i * 106, HEIGHT);
}
void Piano::DiamondsDown()
{
for (int i = 0; i < NUM; i++)
clearrectangle(arr[i].x, arr[i].y, arr[i].x + 95, arr[i].y + 30);
for (int i = 0; i < NUM; i++)
{
arr[i].y += speed;
solidrectangle(arr[i].x, arr[i].y, arr[i].x + 95, arr[i].y + 30);
if (arr[i].y > HEIGHT)
{
int x = rand() % 6;
arr[i] = { x * 106 + 5,-50 };
}
}
}
void Piano::Play()
{
ch = getch();
switch (ch)
{
case 'A':
for (int i = 0; i < NUM; i++)
if (arr[i].y > (HEIGHT - 100) && arr[i].x == (0 * 106 + 5))
{
clearrectangle(arr[i].x, arr[i].y, arr[i].x + 95, arr[i].y + 30);
int x = rand() % 6;
arr[i] = { x * 106 + 5,-50 };
break;
}
break;
case 'S':
for (int i = 0; i < NUM; i++)
if (arr[i].y > (HEIGHT - 100) && arr[i].x == (1 * 106 + 5))
{
clearrectangle(arr[i].x, arr[i].y, arr[i].x + 95, arr[i].y + 30);
int x = rand() % 6;
arr[i] = { x * 106 + 5,-50 };
break;
}
break;
case 'D':
for (int i = 0; i < NUM; i++)
if (arr[i].y > (HEIGHT - 100) && arr[i].x == (2 * 106 + 5))
{
clearrectangle(arr[i].x, arr[i].y, arr[i].x + 95, arr[i].y + 30);
int x = rand() % 6;
arr[i] = { x * 106 + 5,-50 };
break;
}
break;
case 'J':
for (int i = 0; i < NUM; i++)
if (arr[i].y > (HEIGHT - 100) && arr[i].x == (3 * 106 + 5))
{
clearrectangle(arr[i].x, arr[i].y, arr[i].x + 95, arr[i].y + 30);
int x = rand() % 6;
arr[i] = { x * 106 + 5,-50 };
break;
}
break;
case 'K':
for (int i = 0; i < NUM; i++)
if (arr[i].y > (HEIGHT - 100) && arr[i].x == (4 * 106 + 5))
{
clearrectangle(arr[i].x, arr[i].y, arr[i].x + 95, arr[i].y + 30);
int x = rand() % 6;
arr[i] = { x * 106 + 5,-50 };
break;
}
break;
case 'L':
for (int i = 0; i < NUM; i++)
if (arr[i].y > (HEIGHT - 100) && arr[i].x == (5 * 106 + 5))
{
clearrectangle(arr[i].x, arr[i].y, arr[i].x + 95, arr[i].y + 30);
int x = rand() % 6;
arr[i] = { x * 106 + 5,-50 };
break;
}
break;
}
}