【C语言】俄罗斯方块的源代码

GitHub:【C语言】实现俄罗斯方块源代码

Head.h

#ifndef _HEAD_H_
#define _HEAD_H_

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define _CRT_SECURE_NO_WARNINGS 1

//界面的相关的参数
#define WALL_SQUARE_WIDTH 10 //围墙方块的宽度
#define xWALL_SQUARE_NUM 30 //x轴方向的方块的数目
#define yWALL_SQUARE_WIDTH 46 //y轴方向的方块的数目
#define GAME_WALL_WIDTH  (WALL_SQUARE_WIDTH*xWALL_SQUARE_NUM) //游戏区域的宽度 300 
#define GAME_WALL_HTGH (WALL_SQUARE_WIDTH*yWALL_SQUARE_WIDTH) //游戏区域的高度 460

#define WINDOW_WIDTH 480 // 游戏总窗口宽度 480 
#define WINDOW_HIGH 460  // 游戏总窗口高度 460

//定义俄罗斯方块的相关参数
#define ROCK_SQUARE_WIDTH (2*WALL_SQUARE_WIDTH) //俄罗斯方块的大小是围墙的两倍 20
#define xROCK_SQUARE_NUM  ((GAME_WALL_WIDTH -20)/ROCK_SQUARE_WIDTH) // 游戏区x轴放的方块数目:14 
#define yROCK_SQUARE_NUM  ((GAME_WALL_HTGH -20)/ROCK_SQUARE_WIDTH)  // 游戏区y轴放的方块数目:22


//定义移动方块的相关操作
#define DIRECT_UP    3  
#define DIRECT_DOWN  2      
#define DIRECT_LEFT  -1  
#define DIRECT_RIGHT 1 


/*数据结构-线性表(结构体数组)*/
typedef struct ROCK
{
    //用来表示方块的形状(每一个字节是8位,用每4位表示方块中的一行)  
    unsigned short rockShapeBits;
    int          nextRockIndex;  //下一个方块,在数组中的下标  
} RockType;

//方块在图形窗口中的位置(即定位4*4大块的左上角坐标)  
typedef struct LOCATE
{
    int left;
    int top;
} RockLocation_t;

//全局变量-游戏板的状态描述(即表示当前界面哪些位置有方块)  
//0表示没有,1表示有(多加了两行和两列,形成一个围墙,便于判断方块是否能够移动)  
int game_board[yROCK_SQUARE_NUM + 2][xROCK_SQUARE_NUM + 2] = { 0 };
int game_socres = 0; //全局分数

// 把俄罗斯方块的19种类放到数组中
int rockTypeNum = 19;
RockType RockArray[19] = { (0, 0) };

//预览区的方块的位置
RockLocation_t preRockLocation = {GAME_WALL_WIDTH+70,70};
//每次生成初始化方块的位置
RockLocation_t initRockLocation = { (WALL_SQUARE_WIDTH + 100), WALL_SQUARE_WIDTH };
//分数显示的位置


//各个文件中的函数
// 画出界面以及画出方块Draw.h中
void DrawGameWindow();
void DisplayRock(int rockIdx, RockLocation_t*  LocatePtr, bool displayed);
//初始化Init源文件
void InitGame();

//game.h
void PlayGame();
bool IsGameOver();


#endif

Draw.h

#include"Head.h"


//画出游戏界面
void DrawGameWindow()
{
    //先画出围墙
    setcolor(BLUE);
    setlinestyle(PS_SOLID,NULL,0);
    setfillcolor(BLUE);
    //画出上下围墙
    for (int x = WALL_SQUARE_WIDTH; x <= GAME_WALL_WIDTH; x += WALL_SQUARE_WIDTH)
    {
        fillrectangle(x - WALL_SQUARE_WIDTH, 0, x, WALL_SQUARE_WIDTH); //上
        fillrectangle(x - WALL_SQUARE_WIDTH, GAME_WALL_HTGH - WALL_SQUARE_WIDTH, x, GAME_WALL_HTGH);//下
    }
    //画出左右围墙
    for (int y = WALL_SQUARE_WIDTH; y <= GAME_WALL_HTGH; y += WALL_SQUARE_WIDTH)
    {
        fillrectangle(0, y, WALL_SQUARE_WIDTH, y + WALL_SQUARE_WIDTH);//左
        fillrectangle(GAME_WALL_WIDTH - WALL_SQUARE_WIDTH, y, GAME_WALL_WIDTH, y + WALL_SQUARE_WIDTH);//右
    }

    //画出右边统计分数及相关东西

    //画出分割线
    setcolor(WHITE);
    setlinestyle(PS_DASH,2);
    line(GAME_WALL_WIDTH + 20, 0, GAME_WALL_WIDTH + 20, GAME_WALL_HTGH);

    //设置字体
    LOGFONT font;
    gettextstyle(&font);
    settextstyle(18, 0, _T("宋体"));
    font.lfQuality = ANTIALIASED_QUALITY;//设置输出效果为抗锯齿 
    //1显示预览形状
    outtextxy(GAME_WALL_WIDTH + 80, 30, _T("预览"));

    outtextxy(GAME_WALL_WIDTH + 80, 170, _T("分数"));

    outtextxy(GAME_WALL_WIDTH + 65, 250, _T("操作说明"));
    outtextxy(GAME_WALL_WIDTH + 40, 290, _T("w a s d控制方向"));
    outtextxy(GAME_WALL_WIDTH + 40, 335, _T("空格 暂停"));

    //显示分数
    setcolor(RED);
    outtextxy(GAME_WALL_WIDTH + 90, 200, '0');
}

//在游戏区显示编号为rockIdx的方块
void DisplayRock(int rockIdx,  RockLocation_t*  LocatePtr, bool displayed)
{
    int color;//方块的填充颜色
    int lineColor = WHITE;//线的颜色
    int boardFalg = 0;
    int xRock = 0;
    int yRock = 0;
    unsigned short rockCode = RockArray[rockIdx].rockShapeBits;
    //如果displayed为true的话,将方块块颜色设置为white,game_board对应的位置设置为1;
    //如果displayed为false的话,将方块块颜色设置为black,game_board对应的位置设置为0;
    displayed ? (color = RED, boardFalg = 1) : (color = BLACK,lineColor = BLACK, boardFalg = 0);

    setcolor(lineColor);
    setfillcolor(color);

    setlinestyle(PS_SOLID);//设置为实线,
    xRock = LocatePtr->left;
    yRock = LocatePtr->top;

    int count = 0;//每4个换行,记录坐标偏移量
    unsigned short mask = 1;
    for (int i = 1; i <= 16; ++i)
    {

        mask = 1 << (16 - i);
        if ((rockCode & mask) != 0) //如果不为0的话,就画出小方块
        {       
            fillrectangle(xRock , yRock, xRock + ROCK_SQUARE_WIDTH, yRock + ROCK_SQUARE_WIDTH);
        }
        if (i % 4 == 0) //换行
        {
            yRock = yRock + ROCK_SQUARE_WIDTH;  
            xRock = xRock = LocatePtr->left;
        }
        else
        {
            xRock += ROCK_SQUARE_WIDTH;
        }
    }
}

Init.h

#include"Head.h"

static void ShapeStrToBit(unsigned char *rockShapeStr, unsigned short& rockShapeBit);
static void ReadRcok();

void InitGame()
{
    //把全局游戏游戏版初始化,边界初始化为1
    for (int i = 0; i < xROCK_SQUARE_NUM + 2; i++)
    {
        game_board[0][i] = 1;  //上边界
        game_board[yROCK_SQUARE_NUM + 1][i] = 1; //下边界
    }
    for (int i = 0; i < yROCK_SQUARE_NUM + 2; i++)
    {
        game_board[i][0] = 1 ; //左边界
        game_board[i][xROCK_SQUARE_NUM + 1] = 1; //右边界
    }
    //读取俄罗斯方块 
    ReadRcok();

}

//从文件中读取方块的形状存储到rockArray中
void ReadRcok()
{
    FILE* fp = fopen("RockShape.ini","r");
    if (NULL == fp)
    {
        printf("打开文件失败\n");
        return;
    }
    unsigned char readBuf[1024]; //fp读取到字符串readbuf中
    unsigned short rockShapeBit = 0;//存放方块形状,占16比特位
    unsigned char rockShapeStr[16];//存放方块字符串
    int ShapeStrIdx = 0;
    int rockNum = 0;//统计方块的个数以及存放方块数组RockArray的下标
    int rocknext = 0;//方块数组中下一个形状
    int rockShapeStart = 0;//同一类型的形状
    while (true)
    {
        size_t readSize = fread(readBuf, 1, 1024, fp);
        if (readSize == 0)
            break;
        //处理readbuf
        for (size_t idx = 0; idx < readSize; ++idx)
        {
            //将字符存放到rockShapeStr中
            while (ShapeStrIdx < 16 && idx < readSize)
            {
                if (readBuf[idx] == '@' || readBuf[idx] == '#')
                {
                    rockShapeStr[ShapeStrIdx] = (unsigned char)readBuf[idx];
                    ++ShapeStrIdx;
                }
                ++idx; //可能idx == readSize了 
                if (readBuf[idx] == '*')//修改上一次方块的next值
                {
                    idx += 5;
                    RockArray[--rockNum].nextRockIndex = rockShapeStart;
                    rockNum++;
                    rockShapeStart = rockNum;
                    rocknext = rockShapeStart ;
                }
            }
            //可能没有填满
            if (ShapeStrIdx < 16)
            {
                break;
            }
            else //填满shapestr
            {
                ShapeStrIdx = 0;//置0
                //将rockShapeStr 转为rockShapeBit
                ShapeStrToBit(rockShapeStr, rockShapeBit);
                rocknext++;
                RockArray[rockNum].rockShapeBits = rockShapeBit;
                RockArray[rockNum].nextRockIndex = rocknext;
                rockNum++;
            }
        }
    }
    fclose(fp);
}
//将从文件中读取的字符串(长度默认为16)转换成 unsigned short
void ShapeStrToBit(unsigned char *rockShapeStr, unsigned short& rockShapeBit)
{
    rockShapeBit = 0;
    for (size_t idx = 0; idx < 16; ++idx)
    {
        if (rockShapeStr[idx] == '@') //1
        {
            rockShapeBit |= (1 << (16 - idx - 1));
        }
        // #为0 不需要处理
    }
}

game.h

#include"Head.h"
#define _CRT_SECURE_NO_WARNINGS 1

bool MoveAble(int rockIndex, RockLocation_t* currentLocatePtr, int f_direction);
void SetGameBoardFlag(int rockIdx, RockLocation_t* curRockLocation);
void UserHitKeyBoard(char userHit, int* RockIndex, RockLocation_t* curRockLocation);
void FullLine();
void UpdateSocres(int scores);
void DelCurLine(int rowIdx);
bool IsGameOver();


void PlayGame()
{
    char userHit = 0;//用户敲击键盘
    int curRockIndex = 0;//当前方块的rockArray下标
    int nextRockIndex = 0;//下次
    RockLocation_t curRockLocation;
    curRockLocation.left = initRockLocation.left;
    curRockLocation.top = initRockLocation.top;
    DWORD oldtime = 0;
    srand((unsigned int)time(NULL));
    curRockIndex = rand() % rockTypeNum;
    nextRockIndex = rand() % rockTypeNum;
    //画出预览区初始化方块
    //在初始位置和预览区显示方块形状
    DisplayRock(curRockIndex, &initRockLocation, 1);
    DisplayRock(nextRockIndex, &preRockLocation, 1);
    bool moveAbled = false;
    while (true)
    {
        //判断当前方块是否落地(判断能否再下移):如果落地,判断是否满行,再判断是否结束游戏, 改变game_board ,画出下次初始化的方块,以及生成新的预览方块
        //
        moveAbled = MoveAble(curRockIndex, &curRockLocation, DIRECT_DOWN);
        if (!moveAbled) //判断是否落地,不能下移表示落地
        {
            //修改game_board的值
            SetGameBoardFlag(curRockIndex, &curRockLocation);
            FullLine(); 
            if (IsGameOver())
            {
                MessageBox(NULL, _T("游戏结束"), _T("GAME OVER"), MB_OK);
                exit(0);
            }       
            //为下次生成模块开始准备
            DisplayRock(nextRockIndex, &preRockLocation, false);//擦除旧的方块
            curRockIndex = nextRockIndex;
            nextRockIndex = rand() % rockTypeNum; //生成新的预览方块
            DisplayRock(curRockIndex, &initRockLocation, 1);
            DisplayRock(nextRockIndex, &preRockLocation, 1);
            FlushBatchDraw();
            //修改curRockLocation的值
            curRockLocation.left = initRockLocation.left;
            curRockLocation.top = initRockLocation.top; 
        }

        if (kbhit()) //如果敲击键盘了 就处理按键
        {
            userHit = getch();
            UserHitKeyBoard(userHit, &curRockIndex, &curRockLocation);
        }

        //没有 就自动下移一个单位 :不能用else,因为可能按键不是上下左右
        DWORD newtime = GetTickCount();
        if (newtime - oldtime >= (unsigned int)(300) && moveAbled == TRUE)
        {
            oldtime = newtime;
            DisplayRock(curRockIndex, &curRockLocation, false);
            curRockLocation.top += ROCK_SQUARE_WIDTH; //下落一格  
        }
        //AutomaticDownMove(curRockIndex, &curRockLocation);
        //画出新方块
        DisplayRock(curRockIndex, &curRockLocation, 1);
        FlushBatchDraw();
        Sleep(20);
    }
}


//响应键盘命令时间
void UserHitKeyBoard(char userHit, int* RockIndex, RockLocation_t* curRockLocation)
{
    switch (userHit)
    {
    case 'W':
    case 'w'://↑
        if (MoveAble(RockArray[*RockIndex].nextRockIndex, curRockLocation, DIRECT_UP))
        {
            DisplayRock(*RockIndex, curRockLocation, false);
            *RockIndex = RockArray[*RockIndex].nextRockIndex;
        }
        break;
    case 'S':
    case 's'://↓
        if (MoveAble(*RockIndex, curRockLocation, DIRECT_DOWN))
        {
            DisplayRock(*RockIndex, curRockLocation, false);
            curRockLocation->top += 2 * (ROCK_SQUARE_WIDTH);
            if (!MoveAble(*RockIndex, curRockLocation, DIRECT_DOWN))
            {
                curRockLocation->top -= ROCK_SQUARE_WIDTH;
            }
        }
        break;
    case 'A':
    case 'a': //←
        if (MoveAble(*RockIndex, curRockLocation, DIRECT_LEFT))
        {
            DisplayRock(*RockIndex, curRockLocation, false);
            curRockLocation->left -= ROCK_SQUARE_WIDTH;
        }
        break;
    case 'D':
    case 'd': //→
        if (MoveAble(*RockIndex, curRockLocation, DIRECT_RIGHT))
        {
            DisplayRock(*RockIndex, curRockLocation, FALSE);
            curRockLocation->left += ROCK_SQUARE_WIDTH;
        }
        break;
    case ' ': //暂停
        while (1)
        {
            userHit = getch();
            if (userHit == ' ')
                break;
        }
        break;
    default:
        break;
    }
}

//判断是否满行,满行消除,然后计算得分
void FullLine()
{
    bool linefull = true;
    int idx = yROCK_SQUARE_NUM;//从最后一行往上查找 22
    int count = 0;
    while (count != xROCK_SQUARE_NUM ) //遇到空行 14
    {
        linefull = true;
        count = 0;
        for (int i = 1; i <= xROCK_SQUARE_NUM; ++i)
        {
            if (game_board[idx][i] == 0)
            {
                linefull = false;
                count++;
            }
        }
        if (linefull) //满行,消除当前行,更新分数
        {
            DelCurLine(idx);
            game_socres += 3;
            UpdateSocres(game_socres);
            idx++;//因为下面要减1
        }
        idx--;
    }

}
void UpdateSocres(int scores)
{
    setcolor(RED);
    TCHAR s[10];
    _stprintf(s, _T("%d"), scores);
    outtextxy(GAME_WALL_WIDTH + 90, 200, s);
}
//消除当前行
void DelCurLine(int rowIdx)
{
    //擦除当前行
    setcolor(BLACK);
    setfillcolor(BLACK);
    for (int i = 1; i < xROCK_SQUARE_NUM; ++i)
    {
        fillrectangle(WALL_SQUARE_WIDTH + (i - 1)*ROCK_SQUARE_WIDTH, (rowIdx - 1)*ROCK_SQUARE_WIDTH + WALL_SQUARE_WIDTH,
                  WALL_SQUARE_WIDTH + i*ROCK_SQUARE_WIDTH, rowIdx*ROCK_SQUARE_WIDTH + WALL_SQUARE_WIDTH);
    }
    //把上面的向下移
    int cnt = 0;
    while (cnt != xROCK_SQUARE_NUM) //直到遇到是空行的为止  
    {
        cnt = 0;
        for (int i = 1; i <= xROCK_SQUARE_NUM; i++)
        {
            game_board[rowIdx][i] = game_board[rowIdx - 1][i];

            //擦除上面的一行  
            setcolor(BLACK);
            setfillcolor(BLACK);
            fillrectangle(WALL_SQUARE_WIDTH + ROCK_SQUARE_WIDTH*i - ROCK_SQUARE_WIDTH ,
                WALL_SQUARE_WIDTH + ROCK_SQUARE_WIDTH*(rowIdx - 1) - ROCK_SQUARE_WIDTH ,
                WALL_SQUARE_WIDTH + ROCK_SQUARE_WIDTH*i,
                WALL_SQUARE_WIDTH + ROCK_SQUARE_WIDTH*(rowIdx - 1));

            //显示下面的一行  
            if (game_board[rowIdx][i] == 1)
            {
                setcolor(WHITE);
                setfillcolor(RED);
                fillrectangle(WALL_SQUARE_WIDTH + ROCK_SQUARE_WIDTH*i - ROCK_SQUARE_WIDTH ,
                    WALL_SQUARE_WIDTH + ROCK_SQUARE_WIDTH*rowIdx - ROCK_SQUARE_WIDTH ,
                    WALL_SQUARE_WIDTH + ROCK_SQUARE_WIDTH*i,
                    WALL_SQUARE_WIDTH + ROCK_SQUARE_WIDTH*rowIdx);
            }

            if (game_board[rowIdx][i] == 0)
                cnt++;            //统计一行是不是 都是空格  
        }//for  
        rowIdx--;
    }
}

//是否可以移动方块
bool MoveAble(int rockIndex, RockLocation_t* currentLocatePtr, int f_direction)
{
    int mask;
    int rockX;
    int rockY;

    rockX = currentLocatePtr->left;
    rockY = currentLocatePtr->top;

    mask = (unsigned short)1 << 15;
    for (int i = 1; i <= 16; i++)
    {
        //与掩码相与为1的 即为方块上的点  
        if ((RockArray[rockIndex].rockShapeBits & mask) != 0)
        {
            //判断能否移动(即扫描即将移动的位置 是否与设置的围墙有重叠)  
            //若是向上(即翻滚变形)  
            if (f_direction == DIRECT_UP)
            {
                //因为此情况下传入的是下一个方块的形状,故我们直接判断此方块的位置是否已经被占 
                //判断下一个方块
                if (game_board[(rockY - WALL_SQUARE_WIDTH) / ROCK_SQUARE_WIDTH + 1]
                    [(rockX - WALL_SQUARE_WIDTH) / ROCK_SQUARE_WIDTH + 1] == 1)
                    return false;
            }
            //如果是向下方向移动  
            else if (f_direction == DIRECT_DOWN)
            {
                if (game_board[(rockY - WALL_SQUARE_WIDTH) / ROCK_SQUARE_WIDTH + 2]
                    [(rockX - WALL_SQUARE_WIDTH) / ROCK_SQUARE_WIDTH + 1] == 1)
                    return false;
            }
            else //如果是左右方向移动  
            {   //f_direction的DIRECT_LEFT为-1,DIRECT_RIGHT为1,故直接加f_direction即可判断。  
                if (game_board[(rockY - WALL_SQUARE_WIDTH) / ROCK_SQUARE_WIDTH + 1]
                    [(rockX - WALL_SQUARE_WIDTH) / ROCK_SQUARE_WIDTH + 1 + f_direction] == 1)
                    return false;
            }
        }

        //每4次 换行 转到下一行继续  
        i % 4 == 0 ? (rockY += ROCK_SQUARE_WIDTH, rockX = currentLocatePtr->left)
            : rockX += ROCK_SQUARE_WIDTH;

        mask >>= 1;
    }

    return true;

}
//给游戏game_board设置标记表示已经占了
void SetGameBoardFlag(int rockIdx, RockLocation_t* curRockLocation)
{
    int mask;
    int rockX;
    int rockY;

    rockX = curRockLocation->left;
    rockY = curRockLocation->top;

    mask = (unsigned int)1 << 15;
    for (int i = 1; i <= 16; i++)
    {
        //与掩码相与为1的 即为方块上的点  
        if ((RockArray[rockIdx].rockShapeBits & mask) != 0)
        {
            game_board[(rockY - WALL_SQUARE_WIDTH) / ROCK_SQUARE_WIDTH + 1]
                [(rockX - WALL_SQUARE_WIDTH) / ROCK_SQUARE_WIDTH + 1] = 1;
        }

        //每4次 换行 转到下一行继续画  
        i % 4 == 0 ? (rockY += ROCK_SQUARE_WIDTH, rockX = curRockLocation->left)
            : rockX += ROCK_SQUARE_WIDTH;

        mask >>= 1;
    }
}
//判断游戏是否结束
bool IsGameOver()
{
    bool topLineHaveRock = false;//顶行是否有方块
    bool bottomLineHaveRock = false;

    for (int i = 1; i < xROCK_SQUARE_NUM; ++i)
    {
        if (game_board[1][i] == 1)
            topLineHaveRock = true;
        if (game_board[yROCK_SQUARE_NUM][i] == 1)
            bottomLineHaveRock = true;
    }
    if (bottomLineHaveRock && topLineHaveRock)
        return true;
    else 
        return false;
}

main.cpp

#include"Head.h"
#include"Draw.h"
#include"Init.h"
#include"game.h"

int main()
{
    initgraph(WINDOW_WIDTH,WINDOW_HIGH);

    DrawGameWindow();
    //使用 API 函数修改窗口名称  
    HWND hWnd = GetHWnd();
    SetWindowText(hWnd, _T("俄罗斯方块"));
    InitGame();
    PlayGame();
    getchar();
    closegraph();
    system("pause");
    return  0;
}

配置文件:RockShape.ini

@###
@###
@@##
####

@@@#
@###
####
####

@@##
#@##
#@##
####

##@#
@@@#
####
####

****
#@##
#@##
@@##
####

@###
@@@#
####
####

@@##
@###
@###
####

@@@#
##@#
####
####

****
#@##
@@@#
####
####

@###
@@##
@###
####

@@@#
#@##
####
####

#@##
@@##
#@##
####

****
#@##
@@##
@###
####

@@##
#@@#
####
####

****
@###
@@##
#@##
####

#@@#
@@##
####
####

****
@###
@###
@###
@###

@@@@
####
####
####

****
@@##
@@##
####
####

****
  • 74
    点赞
  • 339
    收藏
    觉得还不错? 一键收藏
  • 29
    评论
以下是一个简单的 C 语言俄罗斯方块源代码: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define WIDTH 10 #define HEIGHT 20 int board[HEIGHT][WIDTH]; int shapes[7][4][2] = { {{0, 1}, {0, 0}, {1, 0}, {1, 1}}, // O {{0, 2}, {0, 1}, {0, 0}, {0, -1}}, // I {{0, 1}, {0, 0}, {1, 0}, {2, 0}}, // L {{0, 1}, {0, 0}, {-1, 0}, {-2, 0}}, // J {{0, 1}, {0, 0}, {0, -1}, {1, -1}}, // S {{0, 1}, {0, 0}, {0, -1}, {-1, -1}}, // Z {{0, 1}, {0, 0}, {1, 0}, {0, -1}} // T }; typedef struct { int x; int y; int shape; int rotation; } Piece; Piece current; int getRandomShape() { return rand() % 7; } void newPiece() { current.x = WIDTH / 2; current.y = 0; current.shape = getRandomShape(); current.rotation = 0; } int canPlacePiece(int x, int y, int shape, int rotation) { for (int i = 0; i < 4; i++) { int dx = shapes[shape][i][0]; int dy = shapes[shape][i][1]; int nx = x + dx; int ny = y + dy; if (nx < 0 || nx >= WIDTH || ny < 0 || ny >= HEIGHT) { return 0; } if (board[ny][nx] != 0) { return 0; } } return 1; } void placePiece() { for (int i = 0; i < 4; i++) { int dx = shapes[current.shape][i][0]; int dy = shapes[current.shape][i][1]; int nx = current.x + dx; int ny = current.y + dy; board[ny][nx] = current.shape + 1; } } void removePiece() { for (int i = 0; i < 4; i++) { int dx = shapes[current.shape][i][0]; int dy = shapes[current.shape][i][1]; int nx = current.x + dx; int ny = current.y + dy; board[ny][nx] = 0; } } void rotatePiece(int direction) { removePiece(); current.rotation = (current.rotation + direction) % 4; if (!canPlacePiece(current.x, current.y, current.shape, current.rotation)) { current.rotation = (current.rotation - direction) % 4; } placePiece(); } void movePiece(int dx, int dy) { removePiece(); current.x += dx; current.y += dy; if (!canPlacePiece(current.x, current.y, current.shape, current.rotation)) { current.x -= dx; current.y -= dy; } placePiece(); } void printBoard() { for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { printf("%c", board[y][x] == 0 ? '.' : '#'); } printf("\n"); } } int main() { srand(time(NULL)); newPiece(); placePiece(); printBoard(); while (1) { char c = getchar(); if (c == 'q') { break; } if (c == 'a') { movePiece(-1, 0); } if (c == 'd') { movePiece(1, 0); } if (c == 's') { movePiece(0, 1); } if (c == 'w') { rotatePiece(1); } printBoard(); } return 0; } ``` 这个源代码实现了一个简单的俄罗斯方块游戏,可以使用 WASD 控制方块的移动和旋转。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值