我想要一个俄罗斯方块的C语言代码
时间: 2023-12-28 21:05:42 浏览: 75
下面是一个简单的俄罗斯方块的C语言代码,代码中用到的图形库为graphics.h,需要安装相应的库才能运行。
```
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 20
#define COLS 10
#define BLOCK_SIZE 20
int score = 0;
// 方块的种类和形状
int shapes[7][4][4] = {
// I
{
{0,0,0,0},
{1,1,1,1},
{0,0,0,0},
{0,0,0,0}
},
// O
{
{0,0,0,0},
{0,2,2,0},
{0,2,2,0},
{0,0,0,0}
},
// T
{
{0,0,0,0},
{0,3,0,0},
{3,3,3,0},
{0,0,0,0}
},
// J
{
{0,0,0,0},
{4,0,0,0},
{4,4,4,0},
{0,0,0,0}
},
// L
{
{0,0,0,0},
{0,0,5,0},
{5,5,5,0},
{0,0,0,0}
},
// S
{
{0,0,0,0},
{0,6,6,0},
{6,6,0,0},
{0,0,0,0}
},
// Z
{
{0,0,0,0},
{7,7,0,0},
{0,7,7,0},
{0,0,0,0}
}
};
// 游戏区域
int gameArea[ROWS][COLS] = {0};
// 当前方块的位置和形状
int curBlock[4][4];
int curRow, curCol, curType;
// 绘制方块
void drawBlock(int row, int col, int type) {
setfillstyle(SOLID_FILL, type);
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
if(shapes[type][i][j] != 0) {
bar((col+j)*BLOCK_SIZE, (row+i)*BLOCK_SIZE,
(col+j+1)*BLOCK_SIZE-1, (row+i+1)*BLOCK_SIZE-1);
}
}
}
}
// 擦除方块
void eraseBlock(int row, int col, int type) {
setfillstyle(SOLID_FILL, BLACK);
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
if(shapes[type][i][j] != 0) {
bar((col+j)*BLOCK_SIZE, (row+i)*BLOCK_SIZE,
(col+j+1)*BLOCK_SIZE-1, (row+i+1)*BLOCK_SIZE-1);
}
}
}
}
// 判断当前方块是否可以移动
bool canMove(int row, int col, int type) {
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
if(shapes[type][i][j] != 0) {
int r = row+i, c = col+j;
if(r<0 || r>=ROWS || c<0 || c>=COLS || gameArea[r][c]!=0) {
return false;
}
}
}
}
return true;
}
// 在游戏区域中放置当前方块
void placeBlock() {
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
if(curBlock[i][j] != 0) {
gameArea[curRow+i][curCol+j] = curType;
}
}
}
}
// 消除一行
void clearRow(int row) {
for(int i=row; i>0; i--) {
for(int j=0; j<COLS; j++) {
gameArea[i][j] = gameArea[i-1][j];
}
}
for(int j=0; j<COLS; j++) {
gameArea[0][j] = 0;
}
score++;
}
// 消行
void clearRows() {
for(int i=0; i<ROWS; i++) {
bool full = true;
for(int j=0; j<COLS; j++) {
if(gameArea[i][j] == 0) {
full = false;
break;
}
}
if(full) {
clearRow(i);
i--;
}
}
}
// 绘制游戏区域
void drawGameArea() {
setfillstyle(SOLID_FILL, BLACK);
bar(0, 0, COLS*BLOCK_SIZE-1, ROWS*BLOCK_SIZE-1);
for(int i=0; i<ROWS; i++) {
for(int j=0; j<COLS; j++) {
if(gameArea[i][j] != 0) {
drawBlock(i, j, gameArea[i][j]);
}
}
}
}
// 初始化游戏
void initGame() {
score = 0;
for(int i=0; i<ROWS; i++) {
for(int j=0; j<COLS; j++) {
gameArea[i][j] = 0;
}
}
}
// 开始游戏
void startGame() {
initGame();
cleardevice();
drawGameArea();
srand(time(NULL));
curType = rand() % 7;
memcpy(curBlock, shapes[curType], sizeof(curBlock));
curRow = 0;
curCol = COLS/2-2;
drawBlock(curRow, curCol, curType);
while(true) {
delay(500);
eraseBlock(curRow, curCol, curType);
if(canMove(curRow+1, curCol, curType)) {
curRow++;
} else {
placeBlock();
clearRows();
curType = rand() % 7;
memcpy(curBlock, shapes[curType], sizeof(curBlock));
curRow = 0;
curCol = COLS/2-2;
}
drawBlock(curRow, curCol, curType);
drawGameArea();
if(!canMove(curRow+1, curCol, curType)) {
outtextxy(0, 0, "Game Over!");
break;
}
}
}
int main() {
initwindow(COLS*BLOCK_SIZE, ROWS*BLOCK_SIZE, "Tetris");
startGame();
getch();
closegraph();
return 0;
}
```
注意:该代码只是一个简单的实现,可能存在一些问题,如有需要请自行修改和完善。
阅读全文