我一直在开发Dev C ++上的Pacman游戏。当播放器移动时,我使用Looping和system(“ cls”)重新绘制整个更新的地图,此方法有效,但是每次播放器按下按钮(箭头键)时,都会导致关闭。因此,如果你们有任何想法,那么我只能更新播放器“角色”而无需重绘整个地图?还是任何代码都可以使system(“ 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 ;
}
答案 0 :(得分:0)
如果您使用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()是如何工作的。查找其他答案。