private void MyForm_KeyDown(object sender, KeyEventArgs e) { if ((e.KeyCode == Keys.Enter) && !(game.GetGameStatus())) { label.Dispose(); panel.Dispose(); panel = new MyPanel(1); this.Controls.Add(panel); game.GameStop(); game = new Game(this); game.ChangeGameStatus(); } else if (e.KeyCode == Keys.Up) { game.UpdateMovement(0); } else if (e.KeyCode == Keys.Down) { game.UpdateMovement(1); } else if (e.KeyCode == Keys.Left) { game.UpdateMovement(2); } else if (e.KeyCode == Keys.Right) { game.UpdateMovement(3); } }
public MyForm() { this.Size = new Size(600, 600); this.BackColor = Color.Black; this.Text = "Snake"; panel = new MyPanel(0); this.Controls.Add(panel); label = new Label(); label.ForeColor = Color.White; label.Size = new Size(400, 100); label.Location = new Point(130, 250); label.Font = new Font("Arial", 24, FontStyle.Bold); label.Text = "Press Enter to Start"; label.AutoSize = false; panel.Controls.Add(label); this.KeyDown += new KeyEventHandler(this.MyForm_KeyDown); CenterToScreen(); game = new Game(this); }
public void GameOver() { MyPanel panel = window.GetPanel(); Label label = window.GetLabel(); panel.Invoke((MethodInvoker) delegate { panel.Dispose(); panel = new MyPanel(0); window.Controls.Add(panel); label = new Label(); label.ForeColor = Color.White; label.Size = new Size(400, 100); label.Location = new Point(180, 250); label.Font = new Font("Arial", 24, FontStyle.Bold); label.Text = "GAME OVER"; label.AutoSize = false; panel.Controls.Add(label); window.ChangeLabel(label); window.ChangePanel(panel); }); this.ChangeGameStatus(); }
public void ChangePanel(MyPanel pan) { panel = pan; }
private void UpdateGame(Object source, System.Timers.ElapsedEventArgs e) { //System.Diagnostics.Debug.WriteLine(move); MyPanel panel = window.GetPanel(); panel.Invalidate(); Snake snake = panel.GetSnake(); if (VerifyHit(snake, panel.GetApple())) { snake.IncreaseSnake(); panel.GetApple().ChangePos(); if (movement == 0) { snake.ChangePosX(snake.SnakeLength() - 1, snake.GetPosX(snake.SnakeLength() - 2) - 10); snake.ChangePosY(snake.SnakeLength() - 1, snake.GetPosY(snake.SnakeLength() - 2)); } if (movement == 1) { snake.ChangePosX(snake.SnakeLength() - 1, snake.GetPosX(snake.SnakeLength() - 2) + 10); snake.ChangePosY(snake.SnakeLength() - 1, snake.GetPosY(snake.SnakeLength() - 2)); } if (movement == 2) { snake.ChangePosX(snake.SnakeLength() - 1, snake.GetPosX(snake.SnakeLength() - 2)); snake.ChangePosY(snake.SnakeLength() - 1, snake.GetPosY(snake.SnakeLength() - 2) - 10); } if (movement == 3) { snake.ChangePosX(snake.SnakeLength() - 1, snake.GetPosX(snake.SnakeLength() - 2)); snake.ChangePosY(snake.SnakeLength() - 1, snake.GetPosY(snake.SnakeLength() - 2) + 10); } } for (int i = snake.SnakeLength() - 1; i > 0; i--) { if (snake.GetPosX(i) == snake.GetPosX(0) && snake.GetPosY(i) == snake.GetPosY(0)) { timer.Stop(); panel.Invalidate(); this.GameOver(); } snake.ChangePosX(i, snake.GetPosX(i - 1)); snake.ChangePosY(i, snake.GetPosY(i - 1)); } if (movement == 0) { snake.DecreasePosY(0); } else if (movement == 1) { snake.IncreasePosY(0); } else if (movement == 2) { snake.DecreasePosX(0); } else { snake.IncreasePosX(0); } if (snake.GetPosX(0) == 600) { snake.ChangePosX(0, 0); } else if (snake.GetPosX(0) == 0) { snake.ChangePosX(0, 600); } else if (snake.GetPosY(0) == 600) { snake.ChangePosY(0, 0); } else if (snake.GetPosY(0) == 0) { snake.ChangePosY(0, 600); } }