Exemplo n.º 1
0
        // Generates new powerup for snake to chase
        public powerupBody generatePowerup()
        {
            // make sure the previous powerup has been removed
            if (prevPowerup != null)
            {
                prevPowerup.Visible = false;
            }

            // select random spot on grid for powerup
            Random rand  = new Random();
            Point  point = new Point(rand.Next(1, 39) * 20, rand.Next(1, 22) * 20);

            // Makes sure the Powerup is not placed in an illegal location
            while (forbiddenLocations.Contains(point))
            {
                // Might not be needed to update rand. But I think it allows me
                // to get a new random seed
                rand    = new Random();
                point.X = rand.Next(1, 39) * 20;
                point.Y = rand.Next(1, 22) * 20;
            }

            // Creates powerup and saves the location of the previous powerup
            powerupBody powerup = createPowerup(point);

            powerupLocation = powerup.Location;

            return(powerup);
        }
Exemplo n.º 2
0
        // Creates new powerup
        public powerupBody createPowerup(Point point)
        {
            powerupBody body = new powerupBody();

            body.Image    = Image.FromFile(Application.StartupPath + @"\Images\powerup.jpg");
            body.Location = point;
            body.Name     = "Powerup";
            body.Size     = new Size(20, 20);
            body.SizeMode = PictureBoxSizeMode.AutoSize;
            gamePanel.Controls.Add(body);
            return(body);
        }
Exemplo n.º 3
0
        // The actual start playing function
        private void startGameButton_Click(object sender, EventArgs e)
        {
            // Cleanup from any previous rounds by making everything invisible
            for (int i = 1; i < snakeWholeBody.Count(); i++)
            {
                snakeWholeBody[i].Visible = false;
            }

            // make sure snake is alive
            dead = false;

            // Clear previous forbidden locations
            forbiddenLocations.Clear();

            // reset intentions to defaults
            intentionsList = new List <string> {
                "up", "up", "up", "up", "up", "up", "up"
            };

            startGameButton.Visible = false;

            // reset snake body and previous inputs to default
            snakeWholeBody = new List <snakeBody> {
            };
            prevIntention  = "up";
            prevMove       = "up";
            intention      = "up";

            // Clean up the tick event handelers from previous rounds
            // if the first time nothing happens. On subsequent loops
            // this prevents the speed from doubleing each time
            timer.Tick -= new EventHandler(TimerEventProcessor);

            // reset score
            score           = 0;
            scoreLabel.Text = "Score: " + score;

            // Create initial Snake
            Point     startPoint = new Point(400, 200);
            snakeBody head       = createHead(startPoint);

            snakeWholeBody.Add(head);
            startPoint.Y += 20;
            snakeBody body1 = createBodyPart(startPoint, head);

            snakeWholeBody.Add(body1);
            startPoint.Y += 20;
            snakeBody body2 = createBodyPart(startPoint, body1);

            snakeWholeBody.Add(body2);
            startPoint.Y += 20;
            snakeBody body3 = createBodyPart(startPoint, body2);

            snakeWholeBody.Add(body3);
            startPoint.Y += 20;
            snakeBody body4 = createBodyPart(startPoint, body3);

            snakeWholeBody.Add(body4);
            startPoint.Y += 20;
            snakeBody body5 = createBodyPart(startPoint, body4);

            snakeWholeBody.Add(body5);
            startPoint.Y += 20;
            snakeBody body6 = createBodyPart(startPoint, body5);

            snakeWholeBody.Add(body6);
            prevTail = body6;

            //Initialize first powerup
            prevPowerup = generatePowerup();

            // Sets the timer interval to 0.08 seconds.
            timer.Tick    += new EventHandler(TimerEventProcessor);
            timer.Interval = 80;
            timer.Enabled  = true;
            timer.Start();
        }
Exemplo n.º 4
0
        // This function uses the most recent intention from the user to move the sanke
        private void move()
        {
            // temporary variable to mark the snakes next location as "forbidden" on the
            // next loop. This is to cuase a game over whe the head touches the tail
            List <Point> tempLocations = new List <Point>();

            // Iterate though each part of the snake and move it based on the appropriate
            // intention ftom the Intention List
            for (int i = 0; i < snakeWholeBody.Count(); i++)
            {
                snakeBody piece     = snakeWholeBody[i];
                string    direction = intentionsList[i];

                // Switch case used to increment direction. 20 is used to make
                // sure the snake stay on the grid
                switch (direction)
                {
                case "left":
                    piece.Left -= 20;
                    break;

                case "right":
                    piece.Left += 20;
                    break;

                case "up":
                    piece.Top -= 20;
                    break;

                case "down":
                    piece.Top += 20;
                    break;

                default:
                    piece.Top -= 20;
                    break;
                }

                // Update temporary Locations
                tempLocations.Add(piece.Location);
            }

            // Checks to see if the snake is out of bounds or if the head has
            // touched its body (i.e. forbidden locations)
            if ((snakeWholeBody[0].Location.Y <= 0) ||
                (snakeWholeBody[0].Location.Y >= 460) ||
                (snakeWholeBody[0].Location.X <= 0) ||
                (snakeWholeBody[0].Location.X >= 780) ||
                forbiddenLocations.Contains(snakeWholeBody[0].Location))
            {
                // Remove the head from teh screen for future rounds
                snakeWholeBody[0].Visible = false;

                // Reset forbidden locations and intentions to the defaults
                forbiddenLocations = new List <Point>();
                intentionsList     = new List <string> {
                    "up", "up", "up", "up", "up", "up", "up"
                };

                // Update to allow Game Over to trigger
                dead = true;
            }

            // Checks for Powerups
            if (snakeWholeBody[0].Location == powerupLocation)
            {
                // updates forbidden locations for next move
                forbiddenLocations = tempLocations;

                // Generates new Powerup
                prevPowerup = generatePowerup();

                //Iterates score
                score          += 10;
                scoreLabel.Text = "Score: " + score.ToString();

                // Creates new tail piece, adds it to the snake and extends the intentions list
                prevTail = createBodyPart(prevTailPrevLocation, prevTail);
                snakeWholeBody.Add(prevTail);
                intentionsList.Add(intentionsList[intentionsList.Count() - 1]);
            }
            else
            {
                // updates forbidden locations for next move
                forbiddenLocations = tempLocations;
            }

            // Saves the current move and Tal location for next move
            prevMove             = intentionsList[0];
            prevTailPrevLocation = prevTail.Location;

            // Updates intention List
            intentionsList.RemoveAt(intentionsList.Count() - 1);
            intentionsList.Insert(0, intention);
        }