[英]Pacman Game - how to get pacman to move automatically
我正在做一個吃豆人游戲,此刻,當我按向右,向左,向上或向下箭頭鍵時,我的吃豆人正在地圖允許的坐標內移動。 當我按住鍵時它才移動。 我想知道如何做到這一點,以便他在按鍵時自動移動,直到他撞到地圖上的牆為止,這樣我就不需要按住箭頭了。
這是
if (e.KeyCode == Keys.Down)
{
if (coordinates[(pac.xPosition + 16) / 20, (pac.yPosition + 20) / 20].CellType == 'o'
|| coordinates[(pac.xPosition + 16) / 20, (pac.yPosition + 20) / 20].CellType == 'd'
|| coordinates[(pac.xPosition + 16) / 20, (pac.yPosition + 20) / 20].CellType == 'p')
{
pac.setPacmanImage();
pac.setPacmanImageDown(currentMouthPosition);
checkBounds();
}
單元格類型o,p和d是允許他在地圖上移動的唯一單元格。 這些單元格正在文本文件中繪制。
抱歉,如果很難理解我的要求,但我相信這是一個相當簡單的解釋。
先感謝您。
不要在按鍵過程中移動吃豆人,而是使用按鍵來設置方向,然后將“吃豆人”移動到按鍵邏輯之外 。
enum Direction {Stopped, Left, Right, Up, Down};
Direction current_dir = Direction.Stopped;
// Check keypress for direction change.
if (e.KeyCode == Keys.Down) {
current_dir = Direction.Down;
} else if (e.KeyCode == Keys.Up) {
current_dir = Direction.Up;
} else if (e.KeyCode == Keys.Left) {
current_dir = Direction.Left;
} else if (e.KeyCode == Keys.Right) {
current_dir = Direction.Right;
}
// Depending on direction, move Pac-Man.
if (current_dir == Direction.Up) {
// Move Pac-Man up
} else if (current_dir == Direction.Down) {
// Move Pac-Man down
} else if (current_dir == Direction.Left) {
// Move Pac-Man left
} else if (current_dir == Direction.Right) {
// You get the picture..
}
正如BartoszKP的評論所建議的那樣,您將需要在吃豆人的私有變量中設置方向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.