用C语言写一个俄罗斯方块
时间: 2023-12-29 09:45:47 浏览: 89
好的,下面是一个简单的俄罗斯方块游戏的C语言实现,希望能够帮到你。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define ROWS 20
#define COLUMNS 10
char gameMap[ROWS][COLUMNS] = {0}; // 游戏地图
int currentShape[4][4] = {0}; // 当前方块
int currentRow, currentColumn; // 当前方块的行列位置
int score = 0; // 得分
void initGame();
void drawMap();
void drawShape();
void generateShape();
int checkCollision(int direction);
void rotateShape();
void clearLines();
void gameOver();
int main()
{
initGame();
while (1)
{
drawMap();
drawShape();
Sleep(300);
if (checkCollision(1)) // 检查方块是否碰到底部或其他方块
{
for (int i = 0; i < 4; i++)
{
gameMap[currentRow + i][currentColumn] += currentShape[i][0];
gameMap[currentRow + i][currentColumn + 1] += currentShape[i][1];
gameMap[currentRow + i][currentColumn + 2] += currentShape[i][2];
gameMap[currentRow + i][currentColumn + 3] += currentShape[i][3];
}
generateShape();
clearLines();
if (checkCollision(1)) // 如果新生成的方块已经碰到底部或其他方块,游戏结束
{
gameOver();
break;
}
}
if (_kbhit()) // 监听键盘输入
{
char key = _getch();
switch (key)
{
case 'w': // 旋转方块
rotateShape();
break;
case 'a': // 左移方块
if (!checkCollision(2)) currentColumn--;
break;
case 'd': // 右移方块
if (!checkCollision(3)) currentColumn++;
break;
case 's': // 下移方块
if (!checkCollision(1)) currentRow++;
break;
case ' ': // 直接落到底部
while (!checkCollision(1)) currentRow++;
break;
}
}
}
return 0;
}
void initGame()
{
// 初始化随机数种子
srand((unsigned int)time(NULL));
// 初始化游戏地图
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
if (i == ROWS - 1 || j == 0 || j == COLUMNS - 1) gameMap[i][j] = 1;
}
}
// 生成第一个方块
generateShape();
}
void drawMap()
{
system("cls");
printf("Score: %d\n", score);
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
if (gameMap[i][j] == 0) printf(" ");
else if (gameMap[i][j] == 1) printf("#");
else printf("*");
}
printf("\n");
}
}
void drawShape()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (currentShape[i][j]) gameMap[currentRow + i][currentColumn + j] = 2;
}
}
}
void generateShape()
{
// 生成随机方块
int index = rand() % 7;
int shapes[7][4][4] = {
{{0,0,0,0},{0,0,0,0},{0,1,1,0},{0,1,1,0}}, // 方块
{{0,0,0,0},{0,0,1,0},{0,0,1,0},{0,0,1,1}}, // 条块
{{0,0,0,0},{0,0,1,0},{0,0,1,0},{0,1,1,0}}, // L型块
{{0,0,0,0},{0,1,0,0},{0,1,0,0},{0,1,1,0}}, // J型块
{{0,0,0,0},{0,0,1,0},{0,1,1,0},{0,1,0,0}}, // Z型块
{{0,0,0,0},{0,1,0,0},{0,1,1,0},{0,0,1,0}}, // 反Z型块
{{0,0,0,0},{0,1,1,0},{0,0,1,1},{0,0,0,0}} // T型块
};
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
currentShape[i][j] = shapes[index][i][j];
}
}
// 初始位置在顶部中间
currentRow = 0;
currentColumn = COLUMNS / 2 - 2;
}
int checkCollision(int direction)
{
// direction: 1-下移,2-左移,3-右移
switch (direction)
{
case 1:
for (int i = 3; i >= 0; i--)
{
for (int j = 0; j < 4; j++)
{
if (currentShape[i][j])
{
if (gameMap[currentRow + i + 1][currentColumn + j]) return 1;
}
}
}
break;
case 2:
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (currentShape[i][j])
{
if (gameMap[currentRow + i][currentColumn + j - 1]) return 1;
}
}
}
break;
case 3:
for (int i = 0; i < 4; i++)
{
for (int j = 3; j >= 0; j--)
{
if (currentShape[i][j])
{
if (gameMap[currentRow + i][currentColumn + j + 1]) return 1;
}
}
}
break;
}
return 0;
}
void rotateShape()
{
int temp[4][4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
temp[i][j] = currentShape[i][3-j];
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
currentShape[i][j] = temp[i][j];
}
}
if (checkCollision(0)) // 如果旋转后方块与其他方块重叠,则还原
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
currentShape[i][j] = temp[i][3-j];
}
}
}
}
void clearLines()
{
int lines = 0;
for (int i = ROWS - 2; i >= 0; i--)
{
int flag = 1;
for (int j = 1; j < COLUMNS - 1; j++)
{
if (gameMap[i][j] == 0)
{
flag = 0;
break;
}
}
if (flag)
{
lines++;
for (int k = i; k >= 1; k--)
{
for (int l = 1; l < COLUMNS - 1; l++)
{
gameMap[k][l] = gameMap[k-1][l];
}
}
i++;
}
}
score += lines * 10;
}
void gameOver()
{
system("cls");
printf("Game Over! Your score is %d.\n", score);
system("pause");
}
```
这个俄罗斯方块游戏使用了控制台窗口来显示游戏界面,通过字符来表示方块和地图。游戏地图使用了二维数组来表示,其中0表示空白,1表示边框,2表示方块。游戏流程包括初始化游戏、绘制地图、绘制方块、生成方块、检查碰撞、旋转方块、消除行、游戏结束等几个函数。
阅读全文