用C++的easyx俄罗斯方块代码(有音乐)在vs环境下
时间: 2024-05-01 19:22:19 浏览: 104
以下是一个使用EasyX图形库的俄罗斯方块游戏的代码示例,同时也包含了背景音乐播放功能:
```cpp
#include <graphics.h>
#include <conio.h>
#include <Windows.h>
#include <mmsystem.h>
#pragma comment(lib, "Winmm.lib")
#define COL 10 // 列数
#define ROW 20 // 行数
#define SIZE 30 // 方块大小
int block[7][4][4] = { // 七种俄罗斯方块的形状
{{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0}},
{{0, 0, 0, 0},
{0, 2, 2, 0},
{0, 2, 2, 0},
{0, 0, 0, 0}},
{{0, 0, 0, 0},
{0, 3, 0, 0},
{3, 3, 3, 0},
{0, 0, 0, 0}},
{{0, 0, 0, 0},
{4, 4, 0, 0},
{0, 4, 4, 0},
{0, 0, 0, 0}},
{{0, 0, 0, 0},
{0, 5, 5, 0},
{5, 5, 0, 0},
{0, 0, 0, 0}},
{{0, 0, 0, 0},
{6, 6, 6, 0},
{0, 0, 6, 0},
{0, 0, 0, 0}},
{{0, 0, 0, 0},
{7, 7, 7, 0},
{0, 7, 0, 0},
{0, 0, 0, 0}},
};
int map[ROW][COL] = { 0 }; // 地图数组
int curBlock[4][4]; // 当前方块
int curX = 0; // 当前方块的x坐标
int curY = 0; // 当前方块的y坐标
void drawBlock(int x, int y, int color) { // 绘制方块
setfillcolor(color);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (curBlock[i][j] != 0) {
solidrectangle(x + j * SIZE, y + i * SIZE, x + (j + 1) * SIZE, y + (i + 1) * SIZE);
}
}
}
}
void clearBlock(int x, int y) { // 清除方块
setfillcolor(BLACK);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (curBlock[i][j] != 0) {
solidrectangle(x + j * SIZE, y + i * SIZE, x + (j + 1) * SIZE, y + (i + 1) * SIZE);
}
}
}
}
bool check(int x, int y, int blockType) { // 检查方块是否能够放置到指定位置
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (block[blockType][i][j] != 0) {
if (x + j < 0 || x + j >= COL || y + i >= ROW || (y + i >= 0 && map[y + i][x + j] != 0)) {
return false;
}
}
}
}
return true;
}
void mergeBlock() { // 合并方块到地图数组中
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (curBlock[i][j] != 0) {
map[curY + i][curX + j] = curBlock[i][j];
}
}
}
}
void removeLine() { // 移除满行
for (int i = ROW - 1; i >= 0; i--) {
bool isFull = true;
for (int j = 0; j < COL; j++) {
if (map[i][j] == 0) {
isFull = false;
break;
}
}
if (isFull) {
for (int k = i; k > 0; k--) {
for (int j = 0; j < COL; j++) {
map[k][j] = map[k - 1][j];
}
}
i++;
}
}
}
void playMusic() { // 播放背景音乐
PlaySound(TEXT("music.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
}
int main() {
initgraph(640, 480); // 初始化图形界面
setbkcolor(WHITE);
cleardevice();
IMAGE bg;
loadimage(&bg, _T("bg.jpg"));
putimage(0, 0, &bg);
int score = 0;
int blockType = 0;
int nextBlockType = rand() % 7;
curX = COL / 2 - 2;
curY = 0;
memcpy(curBlock, block[blockType], sizeof(curBlock));
while (true) {
if (check(curX, curY + 1, blockType)) { // 方块下落
clearBlock(curX * SIZE, curY * SIZE);
curY++;
drawBlock(curX * SIZE, curY * SIZE, 0x0000FF);
}
else { // 方块到达底部或者堆积在其他方块上
mergeBlock();
removeLine();
score += 10;
blockType = nextBlockType;
nextBlockType = rand() % 7;
curX = COL / 2 - 2;
curY = 0;
memcpy(curBlock, block[blockType], sizeof(curBlock));
if (!check(curX, curY, blockType)) { // 游戏结束
break;
}
}
Sleep(500); // 降低游戏速度
if (_kbhit()) { // 处理键盘输入
int key = _getch();
if (key == 'a' && check(curX - 1, curY, blockType)) { // 左移
clearBlock(curX * SIZE, curY * SIZE);
curX--;
drawBlock(curX * SIZE, curY * SIZE, 0x0000FF);
}
else if (key == 'd' && check(curX + 1, curY, blockType)) { // 右移
clearBlock(curX * SIZE, curY * SIZE);
curX++;
drawBlock(curX * SIZE, curY * SIZE, 0x0000FF);
}
else if (key == 's' && check(curX, curY + 1, blockType)) { // 加速下落
clearBlock(curX * SIZE, curY * SIZE);
curY++;
drawBlock(curX * SIZE, curY * SIZE, 0x0000FF);
}
else if (key == 'w') { // 旋转
int tmpBlock[4][4];
memcpy(tmpBlock, curBlock, sizeof(tmpBlock));
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
curBlock[i][j] = tmpBlock[3 - j][i];
}
}
if (!check(curX, curY, blockType)) {
memcpy(curBlock, tmpBlock, sizeof(curBlock));
}
else {
clearBlock(curX * SIZE, curY * SIZE);
drawBlock(curX * SIZE, curY * SIZE, 0x0000FF);
}
}
}
}
settextstyle(40, 0, _T("宋体"));
setbkmode(TRANSPARENT);
settextcolor(RED);
outtextxy(200, 200, _T("游戏结束"));
_getch(); // 等待用户按下任意键
closegraph(); // 关闭图形界面
return 0;
}
```
在以上代码中,我们使用了`PlaySound`函数来播放背景音乐,需要在代码中加入以下语句:
```cpp
#pragma comment(lib, "Winmm.lib")
```
并且需要将一个名为"music.wav"的音乐文件放在可执行文件的同一目录下。
同时,我们使用了`Sleep`函数来降低游戏速度,以便玩家能够更好地控制方块。`Sleep`函数的参数表示休眠的毫秒数,可以根据需要进行调整。
最后,我们在游戏结束时显示了得分,并等待用户按下任意键后关闭图形界面。
阅读全文