C# GDI 贪吃蛇游戏

本文关键字:游戏 GDI | 更新日期: 2023-09-27 18:37:14

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

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());
    }
}

C# GDI 贪吃蛇游戏

您需要在

表单上有一个timer,并且对于每个刻度在最后一个方向上移动一个正方形。

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

不是

Form1_KeyDown立即使用 moveSnake 处理击键,而是将击键值存储在表单级变量中,然后使用计时器moveSnake处理它。

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