//Evento que se encarga de parsear la entrada por teclado private void Snake_KeyDown_1(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Down: this.nextDirection = SnakeController.Directions.DOWN; break; case Keys.Up: this.nextDirection = SnakeController.Directions.UP; break; case Keys.Left: this.nextDirection = SnakeController.Directions.LEFT; break; case Keys.Right: this.nextDirection = SnakeController.Directions.RIGHT; break; case Keys.Space: if (timer1.Enabled) { timer1.Stop(); } else { timer1.Start(); } break; } }
//Evento que se lanza cada vez que hay un tick en el timer. private void timer1_Tick(object sender, EventArgs e) { if (snake.hasColision(canvasSnake.Width, canvasSnake.Height)) { this.isGameOver = true; timer1.Stop(); player.Stop(); looserPlayer.Play(); } else { if (!this.nextDirection.Equals(SnakeController.Directions.NO_KEY)) { snake.setDirection(this.nextDirection); this.nextDirection = SnakeController.Directions.NO_KEY; } snake.refresh(); if (snake.eatMeat(meat.getMeatPixel())) { incrementScore(meat.getActualValue()); meat.generateMeat(snake.getSnakeBody()); timer1.Interval -= (timer1.Interval * this.timerReduction) / 100; } else { meat.decrementMeatScore(1); } } canvasSnake.Invalidate(); }
// Funcion que se encarga de iniciar un nuevo juego private void newGame() { player.Stop(); scienceMode = false; if (dificilToolStripMenuItem.Checked) { this.meat = new MeatController(canvasSnake.Width, canvasSnake.Height, Color.Black, this.pixelLength, defaultMeatVal, 1); this.timerReduction = 7; this.timer1.Interval = 140; } else if (mediaToolStripMenuItem.Checked) { this.meat = new MeatController(canvasSnake.Width, canvasSnake.Height, Color.Black, this.pixelLength, defaultMeatVal, 1); this.timerReduction = 6; this.timer1.Interval = 170; } else if (sCIENCEMODEToolStripMenuItem.Checked) { player.PlayLooping(); this.meat = new MeatController(canvasSnake.Width, canvasSnake.Height, Color.Black, pixelLength, defaultMeatVal, 1); this.timerReduction = 8; this.timer1.Interval = 100; scienceMode = true; } else { this.meat = new MeatController(canvasSnake.Width, canvasSnake.Height, Color.Black, this.pixelLength, defaultMeatVal, 1); this.timerReduction = 5; this.timer1.Interval = 200; } this.snake = new SnakeController(initialX, initialY, pixelLength, Color.Black); meat.generateMeat(snake.getSnakeBody()); this.nextDirection = SnakeController.Directions.NO_KEY; this.score.Text = "0"; this.isGameOver = false; canvasSnake.Invalidate(); }