zoukankan      html  css  js  c++  java
  • 从零开始---控制台用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("
    ");
        }
    
        for (i = 0; i < 2 * intX + 2; i++)
        {
            printf("%c", 2);
        }
        printf("
    ");
    }
    
    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("
    ");
        }
    
        for (i = 0; i < 2 * intX + 2; i++)
        {
            printf("%c", 2);
        }
        printf("
    ");
    }
    
    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("
    ");
        }
    
        for (i = 0; i < 2 * intX + 2; i++)
        {
            printf("%c", 2);
        }
        printf("
    ");
    }
    
    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();
                
            }
            
        }
    }

    这里我们已经做出了手动控制移动效果,下一次,就是到达底部碰撞停止的功能了。

  • 相关阅读:
    CDH简易离线部署文档
    算法图解读书笔记
    html 知识整理
    Django admin 组件 原理分析与扩展使用 之 sites.py (一)
    阿里云 centos7 django + uWSGI+Nginx + python3 部署攻略
    git 命令和使用场景总结
    由select引发的思考
    Python 实现单例模式的一些思考
    pep 8 规范的一些记录
    python 垃圾回收机制的思考
  • 原文地址:https://www.cnblogs.com/lsgsanxiao/p/5407636.html
Copyright © 2011-2022 走看看