Exemple #1
0
        public void GameOver()
        {
            //stop the game and display the screen briefly before moving to score screen
            gameTimer.Enabled = false;
            Refresh();
            System.Threading.Thread.Sleep(1000);

            //add current score to scorelist
            HighScore hs = new HighScore("temp", snakeLength);

            MainForm.scoreList.Add(hs);

            //sort the scores list and then add reverse order to show highest first
            MainForm.scoreList.Sort((x, y) => x.score.CompareTo(y.score));
            MainForm.scoreList.Reverse();

            //only keep top 5 scores
            if (MainForm.scoreList.Count == 6)
            {
                MainForm.scoreList.RemoveAt(5);
            }

            MainForm.ChangeScreen(this, "ScoreScreen");
        }
Exemple #2
0
        // Helper method for killing a snake
        private void snakeDeath(Snake s)
        {
            for (int i = 1; i < s.Verts.Count(); i++)
            {
                Model.Point p1 = s.Verts[i - 1];
                Model.Point p2 = s.Verts[i];

                if (p1.X == p2.X)    //moving vertically
                {
                    if (p1.Y > p2.Y) //moving down
                    {
                        for (int j = p1.Y; j > p2.Y; j--)
                        {
                            if (r.Next(0, 100) < snakeRecycleRate * 100)
                            {
                                int  id = foodID++;
                                Food f  = new Food(id, new Model.Point(p1.X, j));
                                foods.Add(id, f);
                                world[p1.X, j] = f;
                            }
                            else
                            {
                                world[p1.X, j] = null;
                            }
                        }
                    }
                    else //moving up
                    {
                        for (int j = p1.Y; j < p2.Y; j++)
                        {
                            if (r.Next(0, 100) < snakeRecycleRate * 100)
                            {
                                int  id = foodID++;
                                Food f  = new Food(id, new Model.Point(p1.X, j));
                                foods.Add(id, f);
                                world[p1.X, j] = f;
                            }
                            else
                            {
                                world[p1.X, j] = null;
                            }
                        }
                    }
                }
                else //Moving horizontally
                {
                    if (p1.X > p2.X) //moving left
                    {
                        for (int j = p1.X; j > p2.X; j--)
                        {
                            if (r.Next(0, 100) < snakeRecycleRate * 100)
                            {
                                int  id = foodID++;
                                Food f  = new Food(id, new Model.Point(j, p1.Y));
                                foods.Add(id, f);
                                world[j, p1.Y] = f;
                            }
                            else
                            {
                                world[j, p1.Y] = null;
                            }
                        }
                    }
                    else //moving right
                    {
                        for (int j = p1.X; j < p2.X; j++)
                        {
                            if (r.Next(0, 100) < snakeRecycleRate * 100)
                            {
                                int  id = foodID++;
                                Food f  = new Food(id, new Model.Point(j, p1.Y));
                                foods.Add(id, f);
                                world[j, p1.Y] = f;
                            }
                            else
                            {
                                world[j, p1.Y] = null;
                            }
                        }
                    }
                }
            }

            // the head of the snake is always set to null on snake death
            if (world[s.Head().X, s.Head().Y] != null)
            {
                if (object.ReferenceEquals(world[s.Head().X, s.Head().Y].GetType(), typeof(Snake)))
                {
                    world[s.Head().X, s.Head().Y] = null;
                }
            }

            //send data to sql server
            int    length = s.Length;
            Thread t1     = new Thread(() => HighScore.addEntry(s.Name, s.AliveDuration.ToString(), length));

            t1.Start();

            // set the verts to -1, -1
            s.Verts = new Model.Point[2] {
                new Model.Point(-1, -1), new Model.Point(-1, -1)
            };
            // add the snake to the garbage
            garbageSnake.Add(s);
        }