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

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

咱們控制檯輸出通常都是一行一行的輸出,因此,在輸出屏幕的時候,咱們一個畫面閃到另外一個畫面的效果數組

我剛開始弄的是用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),下面是裏面的實現spa

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

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

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

明顯,下面是徹底代碼blog

// 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);
        
    }
}

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

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

由於不少函數都要用到界面數組信息,因此把它提取出來作成一個全局變量it

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

// 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();
            
        }
        
    }
}

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

相關文章
相關標籤/搜索