gpt4 book ai didi

c++ - 使用系统 ("cls ") 不关闭 Pacman 游戏

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:30 25 4
gpt4 key购买 nike

我一直致力于 Dev C++ 上的 Pacman 游戏开发。我使用循环和系统(“cls”)在玩家移动时重新绘制整个更新的 map 并且这有效,但每次玩家按下按钮(箭头键)时它都会导致关闭。所以如果你们有任何想法,那么我只能更新玩家“角色”而不重绘整个 map ?或者任何代码可以使系统(“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
);

}

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

关于c++ - 使用系统 ("cls ") 不关闭 Pacman 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56575617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com