用c++写一个俄罗斯方块
时间: 2023-04-01 17:05:04 浏览: 144
好的,我可以回答这个问题。以下是一个简单的俄罗斯方块的 C 代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 10
#define HEIGHT 20
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
int shapes[7][4][4] = {
{{,,,},
{1,1,1,1},
{,,,},
{,,,}},
{{,,,},
{,2,2,},
{,2,2,},
{,,,}},
{{,,,},
{,3,3,},
{3,3,,},
{,,,}},
{{,,,},
{4,4,,},
{,4,4,},
{,,,}},
{{,,,},
{,5,,},
{5,5,5,},
{,,,}},
{{,,,},
{,6,,},
{,6,6,},
{,6,,}},
{{,,,},
{,7,,},
{,7,7,},
{,,7,}}
};
int colors[8] = {
, // black
1, // blue
2, // green
3, // cyan
4, // red
5, // magenta
6, // yellow
7 // white
};
int board[HEIGHT][WIDTH] = {};
int curX = ;
int curY = ;
int curShape = ;
int curRotation = ;
int score = ;
void drawBlock(int x, int y, int shape, int rotation, int color) {
int i, j;
for (i = ; i < 4; i++) {
for (j = ; j < 4; j++) {
if (shapes[shape][i][j] == 1) {
int px = x + j;
int py = y + i;
if (px >= && px < WIDTH && py >= && py < HEIGHT) {
board[py][px] = color;
}
}
}
}
}
void eraseBlock(int x, int y, int shape, int rotation) {
int i, j;
for (i = ; i < 4; i++) {
for (j = ; j < 4; j++) {
if (shapes[shape][i][j] == 1) {
int px = x + j;
int py = y + i;
if (px >= && px < WIDTH && py >= && py < HEIGHT) {
board[py][px] = ;
}
}
}
}
}
int checkCollision(int x, int y, int shape, int rotation) {
int i, j;
for (i = ; i < 4; i++) {
for (j = ; j < 4; j++) {
if (shapes[shape][i][j] == 1) {
int px = x + j;
int py = y + i;
if (px < || px >= WIDTH || py < || py >= HEIGHT || board[py][px] != ) {
return 1;
}
}
}
}
return ;
}
void clearBoard() {
int i, j;
for (i = ; i < HEIGHT; i++) {
for (j = ; j < WIDTH; j++) {
board[i][j] = ;
}
}
}
void drawBoard() {
int i, j;
for (i = ; i < HEIGHT; i++) {
for (j = ; j < WIDTH; j++) {
int color = board[i][j];
if (color != ) {
printf("\033[%dm \033[m", 30 + colors[color]);
} else {
printf(" ");
}
}
printf("\n");
}
}
void newBlock() {
curX = WIDTH / 2 - 2;
curY = ;
curShape = rand() % 7;
curRotation = rand() % 4;
if (checkCollision(curX, curY, curShape, curRotation)) {
clearBoard();
score = ;
}
}
void rotateBlock() {
eraseBlock(curX, curY, curShape, curRotation);
curRotation = (curRotation + 1) % 4;
if (checkCollision(curX, curY, curShape, curRotation)) {
curRotation = (curRotation - 1 + 4) % 4;
}
drawBlock(curX, curY, curShape, curRotation, curShape + 1);
}
void moveBlock(int dx, int dy) {
eraseBlock(curX, curY, curShape, curRotation);
curX += dx;
curY += dy;
if (checkCollision(curX, curY, curShape, curRotation)) {
curX -= dx;
curY -= dy;
drawBlock(curX, curY, curShape, curRotation, curShape + 1);
if (dy == 1) {
newBlock();
}
} else {
drawBlock(curX, curY, curShape, curRotation, curShape + 1);
}
}
void dropBlock() {
while (!checkCollision(curX, curY, curShape, curRotation)) {
moveBlock(, 1);
}
moveBlock(, -1);
}
void removeLine(int row) {
int i, j;
for (i = row; i > ; i--) {
for (j = ; j < WIDTH; j++) {
board[i][j] = board[i - 1][j];
}
}
for (j = ; j < WIDTH; j++) {
board[][j] = ;
}
score += 100;
}
void checkLines() {
int i, j;
for (i = HEIGHT - 1; i >= ; i--) {
int full = 1;
for (j = ; j < WIDTH; j++) {
if (board[i][j] == ) {
full = ;
break;
}
}
if (full) {
removeLine(i);
i++;
}
}
}
void showScore() {
printf("Score: %d\n", score);
}
int main() {
srand(time(NULL));
newBlock();
while (1) {
drawBoard();
showScore();
if (_kbhit()) {
int key = _getch();
if (key == 27) {
break;
} else if (key == LEFT) {
moveBlock(-1, );
} else if (key == RIGHT) {
moveBlock(1, );
} else if (key == UP) {
rotateBlock();
} else if (key == DOWN) {
dropBlock();
}
}
moveBlock(, 1);
checkLines();
Sleep(100);
system("cls");
}
return ;
}
```
希望这个代码能够帮到你!
阅读全文