用C语言实现俄罗斯方块小游戏简单代码
时间: 2023-12-19 15:05:19 浏览: 94
当然可以!以下是一个简单的用C语言实现俄罗斯方块小游戏的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
#define WIDTH 10
#define HEIGHT 20
int board[HEIGHT][WIDTH] = {0};
int currentPiece[4][4] = {0};
int currentX = 0, currentY = 0;
void drawBoard() {
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
if (board[i][j] == 0)
printf(" ");
else
printf("*");
}
printf("\n");
}
}
void drawPiece() {
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (currentPiece[i][j] == 1) {
board[currentY + i][currentX + j] = 1;
}
}
}
}
int checkCollision() {
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (currentPiece[i][j] == 1 && (currentX + j >= WIDTH || currentX + j < 0 || currentY + i >= HEIGHT || board[currentY + i][currentX + j] == 1))
return 1;
}
}
return 0;
}
void rotatePiece() {
int tempPiece[4][4];
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
tempPiece[i][j] = currentPiece[i][j];
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
currentPiece[i][j] = tempPiece[3 - j][i];
}
}
}
void clearLines() {
int i, j;
for (i = HEIGHT - 1; i >= 0; i--) {
int lineComplete = 1;
for (j = 0; j < WIDTH; j++) {
if (board[i][j] == 0) {
lineComplete = 0;
break;
}
}
if (lineComplete) {
int k;
for (k = i; k > 0; k--) {
for (j = 0; j < WIDTH; j++) {
board[k][j] = board[k - 1][j];
}
}
for (j = 0; j < WIDTH; j++) {
board[0][j] = 0;
}
}
}
}
int main() {
char action;
int gameOver = 0;
// 初始化随机数种子
srand((unsigned int)time(NULL));
// 初始化方块形状
int pieces[7][4][4] = {
{{1, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}},
{{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}},
{{1, 1, 1, 0},
{1, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}},
{{1, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}},
{{1, 1, 1, 0},
{0, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}},
{{1, 1, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0}},
{{1, 1, 1, 0},
{1, 1, 1, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}}
};
// 游戏循环
while (!gameOver) {
// 清空当前方块
int i, j;
for (i = currentY; i < currentY + 4; i++) {
for (j = currentX; j < currentX + 4; j++) {
if (i >= 0 && i < HEIGHT && j >= 0 && j < WIDTH) {
if (currentPiece[i - currentY][j - currentX] == 1) {
board[i][j] = 0;
}
}
}
}
// 生成新的方块
int nextPiece = rand() % 7;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
currentPiece[i][j] = pieces[nextPiece][i][j];
}
}
currentX = WIDTH / 2 - 2;
currentY = 0;
// 绘制当前方块
drawPiece();
// 绘制游戏界面
system("cls");
drawBoard();
// 等待用户输入
action = getch();
// 处理用户输入
switch (action) {
case 'a':
currentX--;
if (checkCollision())
currentX++;
break;
case 'd':
currentX++;
if (checkCollision())
currentX--;
break;
case 's':
currentY++;
if (checkCollision()) {
currentY--;
drawPiece();
clearLines();
if (checkCollision()) {
gameOver = 1;
}
}
break;
case 'w':
rotatePiece();
if (checkCollision())
rotatePiece();
break;
case 'q':
gameOver = 1;
break;
}
}
printf("Game Over!\n");
return 0;
}
```
这是一个基本的俄罗斯方块游戏代码,使用了Windows.h头文件中的conio.h库来获取用户输入,使用了Windows的命令行窗口来进行绘制。你可以使用gcc编译器来编译运行该代码。
注意:这只是一个简单的示例,可能还需要进行进一步的优化和改进,比如添加计分系统、游戏结束判断等。希望对你有所帮助!
阅读全文