gpt4 book ai didi

c# 蛇游戏如何停止蛇

转载 作者:行者123 更新时间:2023-11-30 15:02:33 24 4
gpt4 key购买 nike

我必须为学校的一个小项目做一个贪吃蛇游戏。到目前为止,我已经知道蛇在一个方向上不停地移动。然而,按另一个方向只会将那个方向添加到蛇的运动中。所以当你先向上然后向左时,它会向东北方向移动。我几乎不知道如何解决这个问题。我写了一个 stop() 方法,但我不知道如何实现它。如果你们能给我一些提示,我将不胜感激。

            using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Snake
{
class Snake
{
//Attributes go here....
private Form frmSnake;
private static Button btnSnake;
private static Label lblStatus;
private static Timer timerUP;
private static Timer timerLEFT;
private static Timer timerDOWN;
private static Timer timerRIGHT;

//Initialize a Snake Object...
public Snake()
{
initComponents();
initTimers ();
startAllTimers ();
initEventHandler(this);
frmSnake.ShowDialog();
}

//give the Components of snake object values
private void initComponents()
{
frmSnake = new Form();
lblStatus = new Label();
btnSnake = new Button();

frmSnake.Controls.Add(btnSnake);
frmSnake.Controls.Add(lblStatus);
frmSnake.SetBounds(400, 400, 700, 550);
frmSnake.Text = "Snake";

lblStatus.SetBounds(0, 495, 700, 20);
lblStatus.BackColor = System.Drawing.Color.Red;

btnSnake.SetBounds((frmSnake.Width / 2) - (btnSnake.Width / 2), ((frmSnake.Height - lblStatus.Height) / 2) - (btnSnake.Height / 2), 10, 10);
}

private void initTimers ()
{
timerUP = new Timer ();
timerLEFT = new Timer ();
timerDOWN = new Timer ();
timerRIGHT = new Timer ();
timerUP.Enabled = true;
timerUP.Interval = 100;
timerLEFT.Enabled = true;
timerLEFT.Interval = 100;
timerDOWN.Enabled = true;
timerDOWN.Interval = 100;
timerRIGHT.Enabled = true;
timerRIGHT.Interval = 100;
}

private void disposeTimers ()
{
timerUP.Dispose ();
timerLEFT.Dispose ();
timerDOWN.Dispose ();
timerRIGHT.Dispose ();
}

private void stopTimer (Timer timer)
{
timer.Stop ();
}

private void stopAllTimers ()
{
timerUP.Stop ();
timerDOWN.Stop ();
timerLEFT.Stop();
timerRIGHT.Stop();
}
private void startAllTimers ()
{
timerUP.Start ();
timerDOWN.Start ();
timerRIGHT.Start ();
timerLEFT.Start ();
}

private void startTimer (Timer timer)
{
timer.Start ();
}

//initializes the event handler which handles a key press W,A,S,D from user
//W,A,S,D correspondents UP,LEFT,DOWN,RIGHT
private void initEventHandler(Snake snake)
{
snake.frmSnake.KeyPreview = true;
snake.frmSnake.KeyDown += new KeyEventHandler(snake.frm_KeyDown);
}

//handle the key press event sent from previous method
private void frm_KeyDown(object sender, KeyEventArgs e)
{
disposeTimers ();
initTimers ();
switch ( e.KeyData )
{

//if up is pressed
case Keys.W:
{
startTimer ( timerUP );
timerUP.Tick += new EventHandler ( timerUP_Tick );
}
break;
//if left is pressed
case Keys.A:
{
startTimer ( timerLEFT );
timerLEFT.Tick += new EventHandler ( timerLEFT_Tick );
//move snake 5 spaces
}
break;
//if down is pressed
case Keys.S:
{
startTimer ( timerDOWN );
timerDOWN.Tick += new EventHandler ( timerDOWN_Tick );
//move snake 5 spaces
}
break;
//if right is pressed
case Keys.D:
{
startTimer ( timerRIGHT );
timerRIGHT.Tick += new EventHandler ( timerRIGHT_Tick );
//move snake 5 spaces
}
break;
}
}

void timerRIGHT_Tick ( object sender, EventArgs e )
{
btnSnake.Location = new Point ( btnSnake.Location.X + 5, btnSnake.Location.Y );
}

void timerDOWN_Tick ( object sender, EventArgs e )
{
btnSnake.Location = new Point ( btnSnake.Location.X, btnSnake.Location.Y + 5 );
}

void timerLEFT_Tick ( object sender, EventArgs e )
{
btnSnake.Location = new Point ( btnSnake.Location.X - 5, btnSnake.Location.Y );
}

void timerUP_Tick ( object sender, EventArgs e )
{
//move snake 5 spaces
btnSnake.Location = new Point ( btnSnake.Location.X, btnSnake.Location.Y - 5 );
}



private void setLabelStatus(string text, Color color)
{
lblStatus.Text = "";
lblStatus.BackColor = color;
lblStatus.Text = text;
}

//main method initializes a snake game
static void Main(string[] args)
{
Snake snake = new Snake();
}
}
}

最佳答案

您需要跟踪已经创建的计时器,以便稍后删除它们(大概在“停止”方法内)。

或者,您可以设置一个始终触发的移动计时器(在游戏启动时启动),然后存储“当前方向”并在计时器内使用它。

第二种方法可能更好,因为您稍后可以在其中滚动一些“每帧”游戏逻辑(检查蛇是否吃掉自己等),而不是在所有各种计时器回调中复制它。

关于c# 蛇游戏如何停止蛇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12500072/

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