C++俄罗斯方块代码(带mp3背景音乐)在vs环境下
时间: 2023-12-24 21:05:23 浏览: 179
这里提供一个简单的C++控制台版俄罗斯方块代码,同时带有mp3背景音乐。该代码在VS 2019环境下编译通过。
注意:需要在项目属性中添加winmm.lib库,才能正常运行播放音乐。
```
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <Windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
using namespace std;
const int HEIGHT = 20;
const int WIDTH = 10;
int board[HEIGHT][WIDTH] = { 0 }; // 游戏区域
int nowBlock[4][4] = { 0 }; // 当前方块
int nextBlock[4][4] = { 0 }; // 下一个方块
int score = 0; // 分数
int speed = 500; // 下落速度
int x = 0, y = 0; // 方块坐标
// 方块类型,每个方块由4个小方块组成,0表示无方块,1表示有方块
int block[7][4][4] = {
// I型方块
{
{ 0,0,0,0 },
{ 1,1,1,1 },
{ 0,0,0,0 },
{ 0,0,0,0 }
},
// J型方块
{
{ 1,0,0,0 },
{ 1,1,1,0 },
{ 0,0,0,0 },
{ 0,0,0,0 }
},
// L型方块
{
{ 0,0,1,0 },
{ 1,1,1,0 },
{ 0,0,0,0 },
{ 0,0,0,0 }
},
// O型方块
{
{ 1,1,0,0 },
{ 1,1,0,0 },
{ 0,0,0,0 },
{ 0,0,0,0 }
},
// S型方块
{
{ 0,1,1,0 },
{ 1,1,0,0 },
{ 0,0,0,0 },
{ 0,0,0,0 }
},
// T型方块
{
{ 0,1,0,0 },
{ 1,1,1,0 },
{ 0,0,0,0 },
{ 0,0,0,0 }
},
// Z型方块
{
{ 1,1,0,0 },
{ 0,1,1,0 },
{ 0,0,0,0 },
{ 0,0,0,0 }
}
};
// 播放背景音乐
void playMusic() {
mciSendString("open music.mp3 alias bgm", NULL, 0, NULL);
mciSendString("play bgm repeat", NULL, 0, NULL);
}
// 停止背景音乐
void stopMusic() {
mciSendString("stop bgm", NULL, 0, NULL);
mciSendString("close bgm", NULL, 0, NULL);
}
// 随机生成一个方块
void generateBlock() {
int type = rand() % 7;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
nowBlock[i][j] = block[type][i][j];
}
}
x = WIDTH / 2 - 2;
y = 0;
}
// 判断方块是否可以下落
bool canMove(int dx, int dy) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (nowBlock[i][j] != 0) {
int nx = x + j + dx;
int ny = y + i + dy;
if (nx < 0 || nx >= WIDTH || ny < 0 || ny >= HEIGHT || board[ny][nx] != 0) {
return false;
}
}
}
}
return true;
}
// 方块下落
void moveDown() {
if (canMove(0, 1)) {
y++;
}
else {
// 将方块加入到游戏区域
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (nowBlock[i][j] != 0) {
board[y + i][x + j] = nowBlock[i][j];
}
}
}
// 判断是否有满行,有则消除并加分
for (int i = HEIGHT - 1; i >= 0; i--) {
bool full = true;
for (int j = 0; j < WIDTH; j++) {
if (board[i][j] == 0) {
full = false;
break;
}
}
if (full) {
for (int k = i - 1; k >= 0; k--) {
for (int j = 0; j < WIDTH; j++) {
board[k + 1][j] = board[k][j];
}
}
score += 10;
i++;
}
}
generateBlock();
}
}
// 方块左移
void moveLeft() {
if (canMove(-1, 0)) {
x--;
}
}
// 方块右移
void moveRight() {
if (canMove(1, 0)) {
x++;
}
}
// 方块旋转
void rotate() {
int temp[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
temp[i][j] = nowBlock[i][j];
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
nowBlock[i][j] = temp[3 - j][i];
}
}
if (!canMove(0, 0)) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
nowBlock[i][j] = temp[i][j];
}
}
}
}
// 显示游戏界面
void show() {
system("cls");
cout << "Score: " << score << endl;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (board[i][j] == 0) {
cout << " ";
}
else {
cout << "#";
}
}
cout << endl;
}
}
int main() {
srand(time(NULL));
generateBlock();
playMusic();
while (true) {
show();
moveDown();
if (_kbhit()) {
char c = _getch();
if (c == 'a' || c == 'A') {
moveLeft();
}
else if (c == 'd' || c == 'D') {
moveRight();
}
else if (c == 'w' || c == 'W') {
rotate();
}
else if (c == 's' || c == 'S') {
speed = 50;
}
else if (c == 'q' || c == 'Q') {
break;
}
}
Sleep(speed);
if (score >= 100) {
speed = 300;
}
else if (score >= 200) {
speed = 100;
}
}
stopMusic();
return 0;
}
```
阅读全文