-1

如何让蛇在各个方向连续移动而不必总是按下按钮

public partial class Form1 : Form
{
    Rectangle rectangle;
    Size recSize;
    Point firstPoint;
    Point[,] grid;
    Graphics graphics;
    Point[] snake;
    Random rng;
    Pen pen;
    int width = 0;
    int height = 0;

    public Form1()
    {
        InitializeComponent();

        firstPoint = new Point(0, 0);
        recSize = new Size(this.ClientSize.Width, this.ClientSize.Height);
        rectangle = new Rectangle(firstPoint, recSize);
        graphics = this.CreateGraphics();
        width = rectangle.Width;
        height = rectangle.Height;
        grid = new Point[width/4, height/4];
        snake = new Point[400];
        rng = new Random();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        pen = new Pen(new SolidBrush(Color.Green));
        //e.Graphics.DrawRectangle(pen, rectangle);
        e.Graphics.FillRectangle(new SolidBrush(Color.Green), rectangle);

    }

    private void GameButton_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < width / 4; i++)
        {
            for (int j = 0; j < height / 4; j++)
            {
                grid[i, j] = new Point();
                grid[i, j].X = firstPoint.X + (i * 4);
                grid[i, j].Y = firstPoint.Y + (j * 4);
                graphics.DrawEllipse(new Pen(new SolidBrush(Color.FromArgb(255, 0,0,0))), new Rectangle(grid[i, j], new Size(2, 2)));   
            }
        }

        snake[0] = new Point();
        snake[0] = grid[width /4/ 2 , height /4/ 2 ];
        graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black)), new Rectangle(snake[0], new Size(4, 4)));
        graphics.FillEllipse(new SolidBrush(Color.Black), new Rectangle(snake[0], new Size(4, 4)));

    }


    private void moveSnake(KeyEventArgs e)
    {

           switch (e.KeyData)
           {
               case Keys.Up:

                       this.graphics.Clear(Color.Green);
                       snake[0].Y -= 4;
                       graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black)), new Rectangle(snake[0], new Size(4, 4)));
                       graphics.FillEllipse(new SolidBrush(Color.Black), new Rectangle(snake[0], new Size(4, 4)));
                       graphics.Flush();
                       this.Invalidate();
                       System.Threading.Thread.Sleep(500);
                       // this.Refresh();
                       //moveSnake(e);


                        break;
                    case Keys.Down:
                        this.graphics.Clear(Color.Green);
                        snake[0].Y += 4;

                        graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black)), new Rectangle(snake[0], new Size(4, 4)));
                        graphics.FillEllipse(new SolidBrush(Color.Black), new Rectangle(snake[0], new Size(4, 4)));
                        this.Invalidate();
                        System.Threading.Thread.Sleep(500);
                        //this.Refresh();
                        break;
                    case Keys.Left:
                        this.graphics.Clear(Color.Green);
                        snake[0].X -= 4;

                        graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black)), new Rectangle(snake[0], new Size(4, 4)));
                        graphics.FillEllipse(new SolidBrush(Color.Black), new Rectangle(snake[0], new Size(4, 4)));
                        this.Invalidate();
                        System.Threading.Thread.Sleep(500);
                        //this.Refresh();
                        break;
                    case Keys.Right:
                        this.graphics.Clear(Color.Green);
                        snake[0].X += 4;

                        graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black)), new Rectangle(snake[0], new Size(4, 4)));
                        graphics.FillEllipse(new SolidBrush(Color.Black), new Rectangle(snake[0], new Size(4, 4)));
                        this.Invalidate();
                        System.Threading.Thread.Sleep(500);
                        //this.Refresh();
                        break;
                }





    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {

        moveSnake(e);
        this.Refresh();
    }


    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        MessageBox.Show(e.KeyChar.ToString());
    }
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);
        MessageBox.Show(e.KeyChar.ToString());
    }
}
4

2 回答 2

3

您需要timer在表格上使用一个,并且每个刻度线在最后一个方向上移动一个正方形。

有了这个,你可以加快蛇更高的难度。

于 2012-06-15T12:45:21.787 回答
1

与其Form1_KeyDown立即使用 处理击键moveSnake,不如将击键值存储在表单级别变量中,然后moveSnake使用计时器对其进行处理。

也摆脱System.Threading.Thread.Sleep(500); 在里面moveSnake

于 2012-06-15T12:55:53.510 回答