首页 > 解决方案 > 在吃豆人游戏上使用 system(" cls ") 而不用快门

问题描述

我一直在 Dev C++ 上从事 Pacman 游戏开发。我使用循环和系统(“cls”)在玩家移动时重新绘制整个更新的地图,这很有效,但是每次玩家按下按钮(箭头键)时都会导致关闭。所以,如果你们有任何想法,那么我不能只更新玩家“角色”而不重绘整个地图吗?或者任何代码都可以使系统(“cls”)工作得更快而不会导致关闭?. 谢谢我真的很感谢你的帮助:D

int main()
{

    char map[31][65] = 
    {  
       "                                                              " ,
       " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ " ,
       " @                                                          @ " ,
       " @                                                          @ " ,
       " @                                                          @ " ,
       " @                                                          @ " ,
       " @@@@@@@@@@@@@@@@@@@@       C                               @ " ,
       " @                                                          @ " ,
       " @                                                          @ " ,
       " @                                                          @ " ,
       " @               @@@@@@@@@@@@@@                             @ " ,
       " @               @                                          @ " ,
       " @               @                                          @ " ,
       " @               @                       @@@@@@@@@@@@@@@@@@ @ " ,
       " @               @                                          @ " ,
       " @               @                                          @ " ,
       " @               @                                          @ " ,
       " @      @@@@@@@@@@                                          @ " ,
       " @                                                          @ " ,
       " @                                @                         @ " ,
       " @                                @                         @ " ,
       " @                                @                         @ " ,
       " @                                @                         @ " ,
       " @                                @@@@@@@@@                 @ " ,
       " @                                        @                 @ " ,
       " @                                        @                 @ " ,
       " @                                        @                 @ " ,
       " @                         X              @                 @ " ,                     //  Position Of Character 'X' = map[27][27]
       " @                                        @                 @ " ,
       " @                                                          @ " ,
       " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ " 

    } ;



    for ( int y = 1 ; y < 31 ; y++ )
    {
        for ( int x = 1 ; x < 65 ; x ++  )
        {
            cout << map[y][x] ;
        }
        cout << endl ;
    }
        cout << map[27][27] ;

    int a = 27 , b = 27 ;
    int c = 0 ;                                                                        // Define What Arrow Key Used to Control The Charater
    while (1)
    {
        c = 0 ;

    switch ( ( c=getch() ) )
    {
        case  KEY_UP : if  ( map[a-1][b] != '@' )
                       {
                         map[a][b] = ' ' ;
                         cout << map[a][b] ;
                         a-- ;
                         map[a][b] = 'X' ;
                         cout << map[a][b] ;
                       }


        break ; 

        case KEY_DOWN : if  ( map[a+1][b] != '@' )
                       {
                         map[a][b] = ' ' ;
                         a++ ;
                         map[a][b] = 'X' ;
                       }
        break ;

        case KEY_LEFT : if  ( map[a][b-1] != '@' )
                       {
                         map[a][b] = ' ' ;
                         b-- ;
                         map[a][b] = 'X' ;
                       }
        break ;

        case KEY_RIGHT : if  ( map[a][b+1] != '@' )
                       {
                         map[a][b] = ' ' ;
                         b++ ;
                         map[a][b] = 'X' ;
                       }
        break ;


    }
     system("cls") ;

        for ( int y = 1 ; y < 31 ; y++ )
    {
        for ( int x = 1 ; x < 65 ; x ++  )
        {
            cout << map[y][x] ;
        }
        cout << endl ;
    }

    }

    return 0 ;
}

标签: c++

解决方案


如果您使用 windows,您可以使用windows API来更新角色

//Return the handle to the console
GetStdHandle( STD_OUTPUT_HANDLE );

//Get the current buffer of the console
GetConsoleScreenBufferInfo( h, &info );

//Write on the console buffer
BOOL WINAPI WriteConsoleOutputCharacter(
  _In_  HANDLE  hConsoleOutput,
  _In_  LPCTSTR lpCharacter,
  _In_  DWORD   nLength,
  _In_  COORD   dwWriteCoord,
  _Out_ LPDWORD lpNumberOfCharsWritten
);

更新:这是一个向您展示如何在 Windows 中使用 API 的最小示例。

#include <stdio.h>
#include <iostream>
#include <vector>
#include <windows.h>

int main()
{
    //Handle to standard output console
    HANDLE g_console_handle;
    //Console informations
    CONSOLE_SCREEN_BUFFER_INFO g_console_info;
    //return
    DWORD ret;
    //get handle to console
    g_console_handle = GetStdHandle( STD_OUTPUT_HANDLE );

    GetConsoleScreenBufferInfo( g_console_handle, &g_console_info );
    //Get console size
    int rows = g_console_info.dwSize.Y;
    int cols = g_console_info.dwSize.X;
    //Create buffer
    std::vector<char> g_buffer = std::vector<char>( rows *cols, ' ' );
    //console coordinate
    COORD origin;
    //Start writing char from first cell
    origin.X = 0;
    origin.Y = 0;
    //Fill console with tilda
    int t, ti;
    for (t =0;t<rows;t++)
    {
        for (ti =0;ti<cols;ti++)
        {
            g_buffer[t*cols+ti] = '~';
        }
    }

    //Windows API to access the console buffer
    WriteConsoleOutputCharacter
    (
        g_console_handle,       //handle to console
        &g_buffer[0],           //pointer to buffer
        rows*cols,              //number of char to write
        origin,                 //coordinate where start writing
        &ret                    //return number of char actually written
    );

}

UPDATE3:显然我不知道 system() 是如何工作的。查找其他答案。


推荐阅读