c语言写一个俄罗斯方块程序
时间: 2023-06-16 12:08:33 浏览: 105
以下是一个简单的俄罗斯方块程序的C代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 10
#define HEIGHT 20
#define BLOCK_NUM 7
int block[BLOCK_NUM][4][4] =
{
{ {0,1,0,0}, {0,1,0,0}, {0,1,0,0}, {0,1,0,0} },
{ {0,0,0,0}, {1,1,1,1}, {0,0,0,0}, {0,0,0,0} },
{ {0,1,1,0}, {0,1,0,0}, {0,1,0,0}, {0,0,0,0} },
{ {0,1,0,0}, {0,1,1,0}, {0,0,1,0}, {0,0,0,0} },
{ {0,0,1,0}, {0,1,1,0}, {0,1,0,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} }
};
int board[HEIGHT][WIDTH] = {0};
void clearScreen()
{
system("cls");
}
void printBoard()
{
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");
}
}
int checkCollision(int blockID, int x, int y)
{
int i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (block[blockID][i][j] != 0 &&
(x + j >= WIDTH || x + j < 0 ||
y + i >= HEIGHT || y + i < 0 ||
board[y + i][x + j] != 0))
return 1;
}
}
return 0;
}
void placeBlock(int blockID, int x, int y)
{
int i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (block[blockID][i][j] != 0)
board[y + i][x + j] = blockID + 1;
}
}
}
void removeBlock(int blockID, int x, int y)
{
int i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (block[blockID][i][j] != 0)
board[y + i][x + j] = 0;
}
}
}
void rotateBlock(int blockID)
{
int i, j;
int temp[4][4];
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
temp[i][j] = block[blockID][i][j];
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
block[blockID][i][j] = temp[3 - j][i];
}
}
}
int main()
{
srand((unsigned)time(NULL));
int blockID = rand() % BLOCK_NUM;
int x = WIDTH / 2 - 2;
int y = 0;
int score = 0;
int speed = 20;
while (1)
{
clearScreen();
if (checkCollision(blockID, x, y + 1) == 0)
{
removeBlock(blockID, x, y);
y++;
placeBlock(blockID, x, y);
}
else
{
placeBlock(blockID, x, y);
blockID = rand() % BLOCK_NUM;
x = WIDTH / 2 - 2;
y = 0;
score += 10;
speed = 20 - score / 100;
if (speed < 1) speed = 1;
Sleep(speed);
for (int i = 0; i < HEIGHT; i++)
{
int isFullLine = 1;
for (int j = 0; j < WIDTH; j++)
{
if (board[i][j] == 0)
{
isFullLine = 0;
break;
}
}
if (isFullLine == 1)
{
for (int k = i; k > 0; k--)
{
for (int j = 0; j < WIDTH; j++)
{
board[k][j] = board[k - 1][j];
}
}
score += 100;
}
}
}
if (_kbhit())
{
int key = _getch();
switch (key)
{
case 'w':
case 'W':
case ' ':
rotateBlock(blockID);
if (checkCollision(blockID, x, y) == 1)
rotateBlock(blockID);
break;
case 'a':
case 'A':
if (checkCollision(blockID, x - 1, y) == 0)
{
removeBlock(blockID, x, y);
x--;
placeBlock(blockID, x, y);
}
break;
case 'd':
case 'D':
if (checkCollision(blockID, x + 1, y) == 0)
{
removeBlock(blockID, x, y);
x++;
placeBlock(blockID, x, y);
}
break;
case 's':
case 'S':
while (checkCollision(blockID, x, y + 1) == 0)
{
removeBlock(blockID, x, y);
y++;
placeBlock(blockID, x, y);
}
break;
case 'q':
case 'Q':
return 0;
}
}
printBoard();
printf("Score: %d\n", score);
}
return 0;
}
```
这个程序使用了Windows的控制台API来清空屏幕和改变光标位置。如果你是在其他操作系统上运行此程序,你需要使用相应的清屏函数来代替 `system("cls")`。
阅读全文