C编写俄罗斯方块源代码
时间: 2023-10-09 17:11:28 浏览: 107
以下是一个简单的C语言实现俄罗斯方块游戏的源代码,供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 12
#define HEIGHT 22
int board[WIDTH][HEIGHT] = { 0 };
int score = 0;
// 俄罗斯方块的七种类型
int shapes[7][4][4] = {
{ {0,0,0,0}, {0,1,1,0}, {0,1,1,0}, {0,0,0,0} }, // 方块
{ {0,0,0,0}, {0,0,2,0}, {0,2,2,2}, {0,0,0,0} }, // L形
{ {0,0,0,0}, {0,3,0,0}, {0,3,3,3}, {0,0,0,0} }, // J形
{ {0,0,0,0}, {0,0,0,0}, {4,4,4,4}, {0,0,0,0} }, // 长条
{ {0,0,0,0}, {0,5,5,0}, {0,0,5,5}, {0,0,0,0} }, // Z形
{ {0,0,0,0}, {0,0,6,6}, {0,6,6,0}, {0,0,0,0} }, // 反Z形
{ {0,0,0,0}, {0,7,7,7}, {0,0,7,0}, {0,0,0,0} } // T形
};
// 随机生成一个新的方块
int new_shape()
{
return rand() % 7;
}
// 在棋盘上绘制方块
void draw(int shape, int x, int y)
{
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (shapes[shape][i][j] != 0) {
board[x + j][y + i] = shapes[shape][i][j];
}
}
}
}
// 从棋盘上移除方块
void erase(int shape, int x, int y)
{
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (shapes[shape][i][j] != 0) {
board[x + j][y + i] = 0;
}
}
}
}
// 判断一个方块是否可以放在指定的位置
int can_place(int shape, int x, int y)
{
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
int bx = x + j;
int by = y + i;
if (shapes[shape][i][j] != 0) {
if (bx < 0 || bx >= WIDTH || by >= HEIGHT) {
return 0;
}
if (by >= 0 && board[bx][by] != 0) {
return 0;
}
}
}
}
return 1;
}
// 将当前方块的位置向下移动一格
int move_down(int shape, int *x, int *y)
{
if (can_place(shape, *x, *y + 1)) {
erase(shape, *x, *y);
*y += 1;
draw(shape, *x, *y);
return 1;
}
return 0;
}
// 将当前方块的位置向左移动一格
void move_left(int shape, int *x, int *y)
{
if (can_place(shape, *x - 1, *y)) {
erase(shape, *x, *y);
*x -= 1;
draw(shape, *x, *y);
}
}
// 将当前方块的位置向右移动一格
void move_right(int shape, int *x, int *y)
{
if (can_place(shape, *x + 1, *y)) {
erase(shape, *x, *y);
*x += 1;
draw(shape, *x, *y);
}
}
// 旋转当前方块
void rotate(int shape, int *x, int *y)
{
int tmp[4][4];
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
tmp[i][j] = shapes[shape][i][j];
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
shapes[shape][i][j] = tmp[3 - j][i];
}
}
if (!can_place(shape, *x, *y)) {
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
shapes[shape][i][j] = tmp[i][j];
}
}
}
erase(shape, *x, *y);
draw(shape, *x, *y);
}
// 将消除的行向下移动
void shift_down(int n)
{
int i, j;
for (i = n; i > 0; i--) {
for (j = 0; j < WIDTH; j++) {
board[j][i] = board[j][i - 1];
}
}
for (j = 0; j < WIDTH; j++) {
board[j][0] = 0;
}
}
// 检查是否有可以消除的行,并进行消除
int check_lines()
{
int i, j;
int n = 0;
for (i = 0; i < HEIGHT; i++) {
int full = 1;
for (j = 0; j < WIDTH; j++) {
if (board[j][i] == 0) {
full = 0;
break;
}
}
if (full) {
shift_down(i);
n++;
}
}
return n;
}
// 显示游戏界面
void display()
{
printf("\033[2J");
printf("\033[0;0H");
printf("SCORE: %d\n", score);
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
switch (board[j][i]) {
case 0: printf(" "); break;
case 1: printf("\033[42m \033[0m"); break;
case 2: printf("\033[44m \033[0m"); break;
case 3: printf("\033[43m \033[0m"); break;
case 4: printf("\033[47m \033[0m"); break;
case 5: printf("\033[41m \033[0m"); break;
case 6: printf("\033[45m \033[0m"); break;
case 7: printf("\033[46m \033[0m"); break;
}
}
printf("\n");
}
}
int main()
{
srand(time(NULL));
int shape = new_shape();
int x = WIDTH / 2 - 2;
int y = 0;
draw(shape, x, y);
while (1) {
display();
if (!move_down(shape, &x, &y)) {
int n = check_lines();
score += n * n * 100;
shape = new_shape();
x = WIDTH / 2 - 2;
y = 0;
if (!can_place(shape, x, y)) {
break;
}
draw(shape, x, y);
}
if (_kbhit()) {
int ch = _getch();
switch (ch) {
case 'a': move_left(shape, &x, &y); break;
case 'd': move_right(shape, &x, &y); break;
case 'w': rotate(shape, &x, &y); break;
case 's': while (move_down(shape, &x, &y)) {} break;
}
}
Sleep(100);
}
display();
printf("GAME OVER\n");
printf("SCORE: %d\n", score);
return 0;
}
```
这个程序使用了Windows系统的控制台颜色输出来绘制俄罗斯方块的图形,因此在其他系统上可能无法正常运行。如果需要在其他系统上运行,需要修改绘制图形的部分。
阅读全文