從零開始---控制檯用c寫俄羅斯方塊遊戲(2)

上回說到下移的問題,這篇就說一下刷新的問題

我們控制檯輸出一般都是一行一行的輸出,所以,在輸出屏幕的時候,我們一個畫面閃到另一個畫面的效果

我剛開始弄的是用system("CLS");進行清屏,但還是會有閃爍的效果,接下來我會在上一個博文的代碼,現在貼上代碼

// c.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <string.h>
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define intX 10

#define intY 20

//顯示
void show(char Interface[intY][intX])
{
    int i, j;

    for (i = 0; i < intY; i++)
    {
        printf("%c", 3);
        for (j = 0; j < intX; j++)
        {

            if (Interface[i][j] == 0)
            {
                printf("  ");
            }
            else
            {
                printf("");
            }
        }
        printf("%c", 3);
        printf("\n");
    }

    for (i = 0; i < 2 * intX + 2; i++)
    {
        printf("%c", 2);
    }
    printf("\n");
}

int _tmain(int argc, _TCHAR* argv[])
{
    //界面數組
    char Interface[intY][intX] = 
    { 
        { 0, 0, 0, 2, 2, 2 }, 
        { 0, 0, 0, 0, 2 }, 
        { 0 }, 
        { 0 }, 
        { 0 }, 
        { 0 }, { 0 }, { 0 }, { 0 }, { 0 },{ 0 }, { 0, 0, 0, 0 }, { 0 } 
    }; /*當前狀態*/
    int i = 0;
    int j = 0;
    
    while (true)
    {
        for (i = intY - 1; i >= 0; i--)  /* 繼續下落 */
        {
            for (j = 0; j < intX; j++)
            {
                if (Interface[i][j] == 2)
                {
                    Interface[i + 1][j] = Interface[i][j];

                    Interface[i][j] = 0; /*方塊下移*/
                }
            }
        }

        show(Interface);
        getchar();
        /*getchar();*/
    }
}

接下來我們寫一個函數,命名爲gotoxy(int x ,int y),下面是裏面的實現

void gotoxy(int x, int y)
{
    COORD c;

    c.X = x; 
    c.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); }

上面那個函數要用到引用一個windows.h的文件,這個函數的作用就是直接跳到指定的位置座標進行輸出,這樣做就是一點一點的把原先的輸出屏幕上的東西覆蓋住,這樣減少閃爍效果很

明顯,下面是完全代碼

// c.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <string.h>
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "windows.h"
#include "time.h"
#define intX 10

#define intY 20

void gotoxy(int x, int y)
{
    COORD c;
    c.X = x; 
    c.Y = y;
    
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
//顯示
void show(char Interface[intY][intX])
{
    int i, j;

    for (i = 0; i < intY; i++)
    {
        printf("%c", 3);
        for (j = 0; j < intX; j++)
        {

            if (Interface[i][j] == 0)
            {
                printf("  ");
            }
            else
            {
                printf("");
            }
        }
        printf("%c", 3);
        printf("\n");
    }

    for (i = 0; i < 2 * intX + 2; i++)
    {
        printf("%c", 2);
    }
    printf("\n");
}

int _tmain(int argc, _TCHAR* argv[])
{
    //界面數組
    char Interface[intY][intX] = 
    { 
        { 0, 0, 0, 2, 2, 2 }, 
        { 0, 0, 0, 0, 2 }, 
        { 0 }, 
        { 0 }, 
        { 0 }, 
        { 0 }, { 0 }, { 0 }, { 0 }, { 0 },{ 0 }, { 0, 0, 0, 0 }, { 0 } 
    }; /*當前狀態*/
    int i = 0;
    int j = 0;
    
    while (true)
    {
        for (i = intY - 1; i >= 0; i--)  /* 繼續下落 */
        {
            for (j = 0; j < intX; j++)
            {
                if (Interface[i][j] == 2)
                {
                    Interface[i + 1][j] = Interface[i][j];

                    Interface[i][j] = 0; /*方塊下移*/
                }
            }
        }

        show(Interface);
        gotoxy(0, 0);
        
    }
}

因爲我一講到一個功能性代碼的時候,就會把整個代碼發上來,佔了一大幅字,這不是我想要湊字數,是我說了這些東西,有些初學者,可能不知道怎麼用,也是時間問題,只能慢慢來

上面解決下移問題和刷屏的問題,可以看到一個方塊往下掉後就消失不見了,我們來弄一個手動控制下移,按一次5就下移一次。

因爲很多函數都要用到界面數組信息,所以把它提取出來做成一個全局變量

要做成這個效果,就要用到一個函數了,_kbhit,或者是kbhit,兩個函數的功能是一樣的,只是版本不同,我這裏是vs2013,所以是第一個,這個函數在#include "conio.h",引用這個頭文件。kbhit() 功能及返回值: 檢查當前是否有鍵盤輸入,若有則返回一個非0值,否則返回0。看我代碼裏是怎麼用的,不懂多看幾遍。下面貼出整個代碼:

// c.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <string.h>
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "windows.h"
#include "time.h"

#include "conio.h"
#define intX 10

#define intY 20

//記錄方塊左上角的行和列的位置座標
int Row = 0, Column = 3;

//界面數組
char Interface[intY][intX] =
{
    { 0, 0, 0, 2, 2, 2 },
    { 0, 0, 0, 0, 2 },
    { 0 },
    { 0 },
    { 0 },
    { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0, 0, 0, 0 }, { 0 }
}; /*當前狀態*/

void Down()
{
    int i,j;
    for (i = intY - 1; i >= 0; i--)  /* 繼續下落 */
    {
        for (j = 0; j < intX; j++)
        {
            if (Interface[i][j] == 2)
            {
                Interface[i + 1][j] = Interface[i][j];

                Interface[i][j] = 0; /*方塊下移*/
            }
        }
    }
}
//指定位置輸出
void Gotoxy(int x, int y)
{
    COORD c;
    c.X = x; 
    c.Y = y;
    
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
//顯示
void Show()
{
    int i, j;

    for (i = 0; i < intY; i++)
    {
        printf("%c", 3);
        for (j = 0; j < intX; j++)
        {

            if (Interface[i][j] == 0)
            {
                printf("  ");
            }
            else
            {
                printf("");
            }
        }
        printf("%c", 3);
        printf("\n");
    }

    for (i = 0; i < 2 * intX + 2; i++)
    {
        printf("%c", 2);
    }
    printf("\n");
}

int _tmain(int argc, _TCHAR* argv[])
{
    Show();
    int i = 0;
    int j = 0;
    char control = '2';
    while (true)
    {
        if (!_kbhit())
        {
            control = '2';
        }
        else  
        {
            control = _getch();
        }

        if (control == '5')
        {
            Down();
            Gotoxy(0, 0);
            Show();
            
        }
        
    }
}

這裏我們已經做出了手動控制移動效果,下一次,就是到達底部碰撞停止的功能了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章