c#蛇游戏如何阻止蛇

时间:2012-09-19 17:41:46

标签: c# events timer onkeydown

我必须在学校做一个小型项目的蛇游戏。我已经到目前为止,蛇无休止地向一个方向移动。然而,按下另一个方向只会将方向添加到蛇的运动中。所以当你先按下然后它向东走。我几乎坚持如何解决这个问题。我写了一个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();
        }
    }
}

2 个答案:

答案 0 :(得分:4)

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

要么就是这样,或者你可以有一个总是触发的移动计时器(在游戏启动时启动它),然后存储“当前方向”是什么并在计时器内使用它。

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

答案 1 :(得分:1)

将每个键的计时器声明为私有全局变量,然后在您想要更改路线时调用Start()Stop()方法。

 // Declare
 Timer UpTimer = new Timer();
 Timer DownTimer = new Timer();
 Timer LeftTimer = new Timer();
 Timer RightTimer = new Timer();


 // If Left Key pressed
 UpTimer.Stop();
 DownTimer.Stop();
 LeftTimer.Start();
 RightTimer.Stop();