如何使各個方向蛇移動時間可持續無需按按鈕總是C#GDI貪吃蛇遊戲
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());
}
}
你需要在你的問題上付出更多的努力。什麼是錯的,你期望發生什麼,發生了什麼?此外,請嘗試僅發佈相關代碼。你不能粘貼所有的代碼,並要求我們修復它。 – TJHeuvel