52行c代码实现俄罗斯方块

2 篇文章 0 订阅

一个简易版的控制台俄罗斯方块游戏


代码如下:


#include <stdio.h>
#include <windows.h>
#include <conio.h>
#define PrintAt(x,y,s) (CC.X=x,CC.Y=y,SetConsoleCursorPosition(hOut,CC),putchar(s))
HANDLE hOut;
char BT, BS, P[2][10][24];
COORD ActB, CC = { 0, 0 }, B[4][7][4] = {
    0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 2, 1, 2, 0, 0, 1, 1, 1, 2, 1, 0, 0, 1, 0, 1, 1, 2, 1, 0, 1, 1, 1, 1, 0, 2, 0, 1, 0, 0, 1, 1, 1, 2, 1, 0, 0, 1, 0, 2, 0, 3, 0,
    0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 2, 0, -1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1, 1, 1, 2, 1, 0, 1, 2, 1, 1, 2, 1, 1, -1, 1, 0, 1, 1, 1, 2,
    0, 0, 0, 1, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0, 2, 0, 0, 1, 0, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 1, 1, 1, 1, 0, 2, 0, 0, 1, 1, 2, 1, 1, 2, 1, 0, 0, 1, 0, 2, 0, 3, 0,
    0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, -1, 0, -1, 1, -1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1, 1, 1, 2, 0, 1, 1, 2, 1, 1, 1, 0, 1, -1, 1, 0, 1, 1, 1, 2,
};
int pb(SHORT*v, SHORT l, int i, int x, int y){
    for (*v += l; i < 4 && (x = ActB.X + B[BS][BT][i].X, y = ActB.Y + B[BS][BT][i].Y, 1); i++)
        if ((x < 0 || x > 9 || (v == &ActB.X && P[0][x][y] == '@')) && (*v -= l, 1)) return 0;
        else if ((P[0][x][y] == '@' && v == &ActB.Y) || y > 23){
            for (i = 0, *v -= l; i < 4 || (y < 4 && (BT = -1), 0); P[0][(ActB.X + B[BS][BT][i].X)][(ActB.Y + B[BS][BT][i].Y)] = '@', i++);
            return 0;
        }
        for (i = 0, *v -= l; i < 4; P[0][(ActB.X + B[BS][BT][i].X)][(ActB.Y + B[BS][BT][i].Y)] = ' ', i++);
        for (i = 0, *v += l; i < 4; P[0][(ActB.X + B[BS][BT][i].X)][(ActB.Y + B[BS][BT][i].Y)] = 'O', i++);
    return 1;
}
void refresh(){
    int x, y, tmp, xx, yy;
    for (y = 24; y > 3; y--){
         for (x = tmp = 0; x < 10; (P[0][x][y] == '@' && tmp++), x++)
            if (P[1][x][y] != P[0][x][y])
                PrintAt(x, y - 4, P[1][x][y] = P[0][x][y]);
         if (tmp == 10)
            for (yy = y - 1, xx = -1; xx++ < 9 || (xx = -1, yy-- > 0); P[0][xx][yy + 1] = P[0][xx][yy]);
    }
}
void shift(int i){
    for (i = 0; i < 4 || (i = 0); P[0][(ActB.X + B[BS][BT][i].X)][(ActB.Y + B[BS][BT][i].Y)] = ' ', i++);
    for (BS += (BS < 3) ? (1) : (-3); i < 4; P[0][(ActB.X + B[BS][BT][i].X)][(ActB.Y + B[BS][BT][i].Y)] = 'O', i++);
}
int main(char k,char x,char y){
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO CCI = { 100, 0 };
    SetConsoleCursorInfo(hOut, &CCI);
    srand((unsigned)main);
    for (x = y = -1; y++ < 23 || (y = -1, x++ < 9); P[0][x][y] = P[1][x][y] = ' ');   
    while (BT + 1 && (BT = rand() % 7, BS = rand() % 4, 1))
        for (ActB.X = ActB.Y = 1; BT + 2 && pb(&ActB.Y, 1, 0, 0, 0); refresh(), Sleep(200))
            if (_kbhit() && (k = _getch()))
                if (k == 'a' || k == 'A') pb(&ActB.X, -1, 0, 0, 0);
                else if (k == 'd' || k == 'D') pb(&ActB.X, 1, 0, 0, 0);
                else if (k == 's' || k == 'S') while (pb(&ActB.Y, 1, 0, 0, 0) || (BT = -2) + 2);
                else if (k == 'w' || k == 'W') shift(0);
    return 0;
}

如有不妥,欢迎指正~

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值