c++ - 在吃豆人游戏上使用 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 ;
}
解决方案
如果您使用 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() 是如何工作的。查找其他答案。
推荐阅读
- c++ - 为什么从文件中获取输入时我的字符串为空?
- reactjs - 将 react-select 版本从 1.0.0 升级到 3.1.0
- python - LabelEncoder -- TypeError: 参数必须是字符串或数字
- python - dplyr 中与 top_n() 等效的 Pandas 是什么?
- php - 如何在 laravel 中获取 create() 方法的插入 id?
- java - 我如何将 Fair NER 标记器与 DKpro-core 一起使用
- java - 有没有办法缓存享元模式以减少内存使用?
- c# - 通过标识符字符串从列表中删除重复元素(C#)
- c - 为什么删除函数会在 C 中的链表上产生错误
- java - 我的通知仅适用于虚拟设备,不适用于真实手机