C++ 俄罗斯方块游戏代码示例
由于代码量较大,无法在此处完整展示。以下是一个简单的俄罗斯方块游戏的示例代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;
const int height = 20; //游戏区域高度
const int width = 10; //游戏区域宽度
int board[height][width]; //存储游戏方格的状态:0表示空,1表示有方块
int score = 0; //分数
//定义方块(4x4大小)
int blocks[7][4][4] = {
{
{0,1,0,0},
{0,1,0,0},
{0,1,0,0},
{0,1,0,0}
},
{
{0,0,0,0},
{1,1,0,0},
{0,1,1,0},
{0,0,0,0}
},
{
{0,0,0,0},
{0,1,1,0},
{0,1,1,0},
{0,0,0,0}
},
{
{0,0,0,0},
{0,1,1,0},
{1,1,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,1,0,0},
{0,1,0,0},
{0,1,0,0}
},
{
{0,0,0,0},
{0,1,0,0},
{1,1,1,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,0,0,0},
{1,1,1,0},
{0,0,0,0}
}
};
//显示游戏界面
void display()
{
system("cls");
cout << "Tetris" << endl;
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;
}
}
//检查方块是否能够放置到当前位置
bool check(int blockType, int blockState, int row, int col)
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (blocks[blockType][blockState][i*4+j] == 1) {
int r = row+i;
int c = col+j;
if (r < 0 || r >= height || c < 0 || c >= width || board[r][c] == 1) {
return false;
}
}
}
}
return true;
}
//将方块放置到当前位置
void place(int blockType, int blockState, int row, int col)
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (blocks[blockType][blockState][i*4+j] == 1) {
board[row+i][col+j] = 1;
}
}
}
}
//移除方块
void remove(int blockType, int blockState, int row, int col)
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (blocks[blockType][blockState][i*4+j] == 1) {
board[row+i][col+j] = 0;
}
}
}
}
//检查是否有满行,如果有则消除该行并增加分数
void checkRows()
{
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 j = 0; j < width; j++) {
board[i][j] = 0;
}
score += 10;
for (int k = i-1; k >= 0; k--) {
for (int j = 0; j < width; j++) {
board[k+1][j] = board[k][j];
}
}
i++;
}
}
}
int main()
{
srand((unsigned int)time(NULL)); //初始化随机数生成器
int blockType = rand() % 7; //生成一个随机的方块类型
int blockState = 0; //方块的状态
int row = 0; //方块的行
int col = width/2-2; //方块的列
int delay = 500; //下落速度(单位:毫秒)
bool gameOver = false; //游戏是否结束
//游戏循环
while (!gameOver) {
display();
//处理用户输入
if (_kbhit()) {
char c = _getch();
switch (c) {
case 'a': //左移
if (check(blockType, blockState, row, col-1)) {
remove(blockType, blockState, row, col);
col--;
place(blockType, blockState, row, col);
}
break;
case 'd': //右移
if (check(blockType, blockState, row, col+1)) {
remove(blockType, blockState, row, col);
col++;
place(blockType, blockState, row, col);
}
break;
case 's': //加速下落
delay = 50;
break;
case 'w': //旋转
if (check(blockType, (blockState+1)%4, row, col)) {
remove(blockType, blockState, row, col);
blockState = (blockState+1) % 4;
place(blockType, blockState, row, col);
}
break;
case ' ': //直接落地
while (check(blockType, blockState, row+1, col)) {
remove(blockType, blockState, row, col);
row++;
place(blockType, blockState, row, col);
}
break;
}
}
//下落
if (check(blockType, blockState, row+1, col)) {
remove(blockType, blockState, row, col);
row++;
place(blockType, blockState, row, col);
} else {
place(blockType, blockState, row, col);
checkRows();
//生成下一个方块
blockType = rand() % 7;
blockState = 0;
row = 0;
col = width/2-2;
delay = 500;
//检查游戏是否结束
if (!check(blockType, blockState, row, col)) {
gameOver = true;
}
}
//延时
Sleep(delay);
}
display();
cout << "Game Over!" << endl;
cout << "Score: " << score << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/n5K1 著作权归作者所有。请勿转载和采集!