如何让蛇在各个方向连续移动而不必总是按下按钮
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());
}
}