У меня небольшая проблема с моим приложением windowsforms - это игра-змея. Все в порядке, но после нажатия кнопки «Воспроизвести снова» приложение не видит ни одного кей-события (я пытаюсь отладить и поставить точку останова в KeyIsDown (отправитель объекта, KeyEventArgs e), но это не вызов, когда я щелкаю Key. Скажите, в чем проблема?
Полный репозиторий (немного другая версия, но все еще не работает): https://github.com/ldidil/snake-game Сокращенная версия кода:
namespace SnakeGame
{
public partial class Form1 : Form
{
private List<Circle> Snake = new List<Circle>();
private Circle food;
public Form1()
{
InitializeComponent();
new Settings();
DisplayTopScore();
gameTimer.Interval = 1000 / Settings.Speed; //TO DO (change speed lvl)
gameTimer.Tick += UpdateScreen;
gameTimer.Start();
StartGame();
}
private void StartGame()
{
GenerateSnake();
GenerateFood();
}
private void UpdateScreen(object sender, EventArgs e)
{
if (Settings.GameOver == false)
{
if (Input.KeyPress(Keys.Right) && Settings.Direction != Directions.Left)
{
Settings.Direction = Directions.Right;
}
else if (Input.KeyPress(Keys.Left) && Settings.Direction != Directions.Right)
{
Settings.Direction = Directions.Left;
}
else if (Input.KeyPress(Keys.Up) && Settings.Direction != Directions.Down)
{
Settings.Direction = Directions.Up;
}
else if (Input.KeyPress(Keys.Down) && Settings.Direction != Directions.Up)
{
Settings.Direction = Directions.Down;
}
MovePlayer();
}
boardCanvas.Invalidate(); //odśwież plansze
}
private void MovePlayer()
{
for (int i = Snake.Count - 1; i >= 0; i--)
{
if (i == 0)
{
switch (Settings.Direction)
{
case Directions.Right:
Snake[i].X++;
break;
case Directions.Left:
Snake[i].X--;
break;
case Directions.Up:
Snake[i].Y--;
break;
case Directions.Down:
Snake[i].Y++;
break;
}
checkCollision(i);
checkFood();
}
else
{
Snake[i].X = Snake[i - 1].X;
Snake[i].Y = Snake[i - 1].Y;
}
}
}
private void KeyIsDown(object sender, KeyEventArgs e)
{
Input.ChangeState(e.KeyCode, true);
}
private void KeyIsUp(object sender, KeyEventArgs e)
{
Input.ChangeState(e.KeyCode, false);
}
private void Die()
{
topScore.Write();
Settings.GameOver = true;
}
private void UpdateGraphic(object sender, PaintEventArgs e)
{
{
if (!Settings.GameOver)
{
draw(e);
}
else
{ //game over
string gameOver = "Game Over\n" + "Final Score: " + Settings.Points;
endText.Text = gameOver;
endText.Visible = true;
playAgainButton.Visible = true;
exitButton.Visible = true;
}
}
}
private void draw(PaintEventArgs e)
{
for (int i = 0; i < Snake.Count; i++)
{
string location;
if (i == 0)
{
location = (@"..\..\Img\snakeHead.png");
}
else
{
location = (@"..\..\Img\snakeBody.png");
}
var img = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + location);
Rotate(img);
Point p1 = new Point(Snake[i].X *Settings.Width, Snake[i].Y * Settings.Height);
e.Graphics.DrawImage(img, p1);
}
Point p2 = new Point(food.X * Settings.Width, food.Y * Settings.Height);
var appleImage = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + @"..\..\Img\apple.png");
e.Graphics.DrawImage(appleImage, p2);
}
private static void Rotate(Bitmap img)
{
switch (Settings.Direction)
{
case Directions.Right:
break;
case Directions.Left:
img.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case Directions.Up:
img.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
case Directions.Down:
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
}
}
private void playAgainButton_Click(object sender, EventArgs e)
{
endText.Visible = false;
playAgainButton.Visible = false;
exitButton.Visible = false;
new Settings(Form2.nickname, Form2.speed);
Snake.Clear();
StartGame();
}
private void exitButton_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
}
}