C#GDI蛇游戏

时间:2012-06-15 12:41:52

标签: c#

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

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

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

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

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