//moves the snake, checks if it has reached a fruit, or hit the edges or itself private void timer1_Tick(object sender, EventArgs e) { Random r = new Random(); g.FillRectangle(new SolidBrush(Color.Black), fruit.toRectangle()); g.Clear(Color.White); label1.Text = "Score: " + score.ToString(); //check if snake has reached fruit, if it has, increase score, and play sound if (s.head().Equals(fruit)) { fruit = new GamePoint(r.Next(60), r.Next(40)); s.add(); score += scoreMultiplier; if (sounds) fruitplayer.controls.play(); } s.move(); g.FillRectangle(new SolidBrush(Color.Black), fruit.toRectangle()); s.draw(g); //check if snake has hit edges or itself if (!(s.head().x < 60 && s.head().x >= 0 && s.head().y < 40 && s.head().y >= 0) || s.headhit()) { timer1.Stop(); if (MessageBox.Show("Game Over. Your Score is: " + score.ToString() + " Start a new game?", "Game Over", MessageBoxButtons.YesNo, MessageBoxIcon.None) == DialogResult.Yes) newGame(); else pauseToolStripMenuItem.Text = "New Game"; } else this.Refresh(); if (sounds) { if (backgroundplayer.playState == WMPPlayState.wmppsStopped) { backgroundplayer.controls.play(); } } }
//starts a new game private void newGame() { score = 0; Random r = new Random(); fruit = new GamePoint(r.Next(60), r.Next(40)); g.Clear(Color.White); g.FillRectangle(new SolidBrush(Color.Black), fruit.toRectangle()); s = new Snake(); s.draw(g); timer1.Start(); }