⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eluosi.txt

📁 C语言写的俄罗斯方块程序 其中的主要逻辑有: (1)由于c的随机性函数不好
💻 TXT
📖 第 1 页 / 共 2 页
字号:
        return;
    if(( b_success=CanRotate()))
    {
        EraseBlock(block,BoardLeft,BoardTop,CellSize);
        memcpy(curBlock.c,BufferCells,16);
        DrawBlock(block,BoardLeft,BoardTop,CellSize);
    }
    return b_success;
}


/* erase a block, only fill the filled cell with background color */
void _INNER_HELPER EraseBlock(Block *block,int bdLeft,int bdTop,int cellSize)
{
    int i,j;
    setfillstyle(SOLID_FILL,BkGndColor);
    for(i=0;i<block->size;i++)
    {
        for(j=0;j<block->size;j++)
        {
            if(block->c[i][j] && (block->y+j>=0))
            {
                floodfill(
                    bdLeft+cellSize*(i+block->x)+cellSize/2,
                    bdTop+cellSize*(j+block->y)+cellSize/2,
                    BorderColor);
            }
        }
    }
}

/* move by the direction if can, donothing if cannot
* return value: true - success, false - cannot move toward this direction
*/
int MoveBlock(Block *block,int dx,int dy)
{
    int b_canmove=CanMove(dx,dy);
    if(b_canmove)
    {
        EraseBlock(block,BoardLeft,BoardTop,CellSize);
        curBlock.x+=dx;
        curBlock.y+=dy;
        DrawBlock(block,BoardLeft,BoardTop,CellSize);
    }
    return b_canmove;
}

/* drop the block to the bottom! */
int DropBlock(Block *block)
{
    EraseBlock(block,BoardLeft,BoardTop,CellSize);
    while(CanMove(0,1))
    {
        curBlock.y++;
    }
    DrawBlock(block,BoardLeft,BoardTop,CellSize);
    return 0;/* return value is assign to the block's alive */
}


/* init the graphics mode, draw the board grid */
void InitGame()
{
    int i,j,gdriver=DETECT,gmode;
    struct time sysTime;
    /* draw board cells */
    memset(Board,0,BoardWidth*BoardHeight*2);
    memset(nextBlock.c,0,16);
    strcpy(info_help,"P: Pause Game. --by hoodlum1980");
    initgraph(&gdriver,&gmode,"c:\\tc\\");
    setcolor(BorderColor);
    for(i=0;i<=BoardWidth;i++)
    {
        line(BoardLeft+i*CellSize, BoardTop, BoardLeft+i*CellSize, BoardTop+ BoardHeight*CellSize);
    }
    for(i=0;i<=BoardHeight;i++)
    {
        line(BoardLeft, BoardTop+i*CellSize, BoardLeft+BoardWidth*CellSize, BoardTop+ i*CellSize);
    }
    /* draw board outer border rect */
    rectangle(BoardLeft-CellSize/4, BoardTop-CellSize/4,
        BoardLeft+BoardWidth*CellSize+CellSize/4,
        BoardTop+BoardHeight*CellSize+CellSize/4);

    /* draw next block grids */
    for(i=0;i<=4;i++)
    {
        line(NBBoardLeft+i*NBCellSize, NBBoardTop, NBBoardLeft+i*NBCellSize, NBBoardTop+4*NBCellSize);
        line(NBBoardLeft, NBBoardTop+i*NBCellSize, NBBoardLeft+4*NBCellSize, NBBoardTop+ i*NBCellSize);
    }

    /* draw score rect */
    rectangle(ScoreBoardLeft,ScoreBoardTop,ScoreBoardLeft+ScoreBoardWidth,ScoreBoardTop+ScoreBoardHeight);
    DisplayScore();

    /* set new seed! */
    gettime(&sysTime);
    srand(sysTime.ti_hour*3600+sysTime.ti_min*60+sysTime.ti_sec);

    GenerateBlock(&nextBlock);
    NextBlock();    /* create first block */
    setcolor(DARKGRAY);
    outtextxy(InfoLeft,InfoTop+20,"Up  -rotate  Space-drop");
    outtextxy(InfoLeft,InfoTop+35,"Left-left    Right-right");
    outtextxy(InfoLeft,InfoTop+50,"Esc -exit");
    DisplayInfo(info_help);
}

/* set the isFilled and fillcolor data to the board */
void _INNER_HELPER FillBoardData()
{
    int i,j;
    for(i=0;i<curBlock.size;i++)
    {
        for(j=0;j<curBlock.size;j++)
        {
            if(curBlock.c[i][j] && (curBlock.y+j)>=0)
            {
                Board[curBlock.x+i][curBlock.y+j][0]=1;
                Board[curBlock.x+i][curBlock.y+j][1]=curBlock.color;
            }
        }
    }
}

/* draw one line of the board */
void _INNER_HELPER PaintBoard()
{
    int i,j,fillcolor;
    for(j=max((TopLine-4),0);j<BoardHeight;j++)
    {
        for(i=0;i<BoardWidth;i++)
        {
            fillcolor=Board[i][j][0]? Board[i][j][1]:BkGndColor;
            setfillstyle(SOLID_FILL,fillcolor);
            floodfill(BoardLeft+i*CellSize+CellSize/2,BoardTop+j*CellSize+CellSize/2,BorderColor);
        }
    }
}

/* check if one line if filled full and increase the totalScore! */
void _INNER_HELPER CheckBoard()
{
    int i,j,k,score=10,sum=0,topy,lines=0;
    /* we find the top empty line! */
    j=topy=BoardHeight-1;
    do
    {
        sum=0;
        for(i=0;i< BoardWidth; i++)
        {
            sum+=Board[i][topy][0];
        }
        topy--;
    } while(sum>0 && topy>0);

    /* remove the full filled line (max remove lines count = 4) */
    do
    {
        sum=0;
        for(i=0;i< BoardWidth; i++)
            sum+=Board[i][j][0];

        if(sum==BoardWidth)/* we find this line is full filled, remove it! */
        {
            /* move the cells data down one line */
            for(k=j; k > topy;k--)
            {
                for(i=0;i<BoardWidth;i++)
                {
                    Board[i][k][0]=Board[i][k-1][0];
                    Board[i][k][1]=Board[i][k-1][1];
                }
            }
            /*make the top line empty! */
            for(i=0;i<BoardWidth;i++)
            {
                Board[i][topy][0]=0;
                Board[i][topy][1]=0;
            }
            topy++;        /* move the topline downward one line! */
            lines++;    /* lines <=4 */
            TotalScore+=score;
            score*=2;    /* adding: 10, 30, 70, 150 */
        }
        else
            j--;
    } while(sum>0 && j>topy && lines<4);
    /* speed up the game when score is high, minimum is 400 */
    FrameTime=max(1200-100*(TotalScore/200), 400);
    TopLine=topy;/* update the top line */
    /* if no lines remove, only add 1: */
    if(lines==0)
        TotalScore++;
}

/* display the score */
void _INNER_HELPER DisplayScore()
{
    setcolor(BkGndColor);
    outtextxy(ScoreBoardLeft+5,ScoreBoardTop+5,info_score);
    setcolor(ScoreColor);
    sprintf(info_score,"Score: %d",TotalScore);
    outtextxy(ScoreBoardLeft+5,ScoreBoardTop+5,info_score);
}

/* we call this function when a block is inactive. */
void UpdateBoard()
{
    FillBoardData();
    CheckBoard();
    PaintBoard();
    DisplayScore();
}

/* pause the game, and timer handler stop move down the block! */
int PauseGame()
{
    int key=0;
    DisplayInfo("Press P to Start or Resume!");
    while(key!=K_P && key!=K_ESC)
    {
        while(!(key=GetKeyCode())){}
    }
    DisplayInfo(info_help);
    return key;
}

/* quit the game and do cleaning work. */
void QuitGame()
{
    closegraph();
}
/* the entry point function. */
void main()
{
    int i,flag=1,j,key=0,tick=0;
    InitGame();
    if(PauseGame()==K_ESC)
        goto GameOver;
    /* wait until a key pressed */
    while(key!=K_ESC)
    {
        /* wait until a key pressed */
        while(!(key=GetKeyCode()))
        {
            tick++;
            if(tick>=FrameTime)
            {
                /* our block has dead! (can't move down), we get next block */
                if(!MoveBlock(&curBlock,0,1))
                {
                    UpdateBoard();
                    NextBlock();
                    if(!CanMove(0,1))
                        goto GameOver;
                }
                tick=0;
            }
            delay(100);
        }
        switch(key)
        {
            case K_LEFT:
                MoveBlock(&curBlock,-1,0);
                break;
            case K_RIGHT:
                MoveBlock(&curBlock,1,0);
                break;
            case K_DOWN:
                MoveBlock(&curBlock,0,1);
                break;
            case K_UP:
                RotateBlock(&curBlock);
                break;
            case K_SPACE:
                DropBlock(&curBlock);
                break;
            case K_P:
                PauseGame();
                break;
        }
    }
GameOver:
    DisplayInfo("GAME OVER!  Press any key to exit!");
    getch(); /* wait the user Press any key. */
    QuitGame();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -