Example #1
0
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // Set the player object to movingUp
            if (e.KeyCode == Keys.W)
            {
                playerOne.movingUp = true;
            }

            // Set the player object to be movingDown
            if (e.KeyCode == Keys.S)
            {
                playerOne.movingDown = true;
            }

            // Fire an arrow if there is not one in play and at it to the list of game objects
            if (e.KeyCode == Keys.Space && arrow == null)
            {
                x     = GameWindow.Width / 15; // x coordinate for arrow spawn
                y     = playerOne.yPos + 36;   // current y coordinate of player bow
                arrow = new MissileObject(x, y);
                objects.Add(arrow);
            }

            // Restart the application and close the previous instance if the game is over
            if (e.KeyCode == Keys.R && gameOverState == true)
            {
                Application.Restart();
                Environment.Exit(0);
            }

            // Close the application
            if (e.KeyCode == Keys.Escape)
            {
                Application.Exit();
            }
        }
Example #2
0
        private void FrameTick_Tick(object sender, EventArgs e)
        {
            // Update position of all GameObjects
            foreach (GameObject n in objects)
            {
                n.UpdatePos();
            }

            for (int i = 0; i < enemyObjects.Count; i++) // Loop through all EnemyObjects
            {
                EnemyObject m = enemyObjects[i];

                if (m.xPos < 0) // Check if an EnemyObject has passed the player
                {
                    gameOverState = true;

                    // Stop timers
                    FrameTick.Enabled = false;
                    SpawnTick.Enabled = false;
                    BobTick.Enabled   = false;

                    // Remove in game score counter
                    ScoreLabel.Visible = false;

                    // Position Game Over message based on resolution and turn it on
                    GameOverLabel.Left    = GameWindow.Width / 2 - GameOverLabel.Width / 2;
                    GameOverLabel.Top     = GameWindow.Height / 2 - GameOverLabel.Height / 2 - 32;
                    GameOverLabel.Visible = true;

                    // Position final score relative to Game Over message and turn it on
                    FinalScoreLabel.Text    = "You scored: " + playerPoints + " - Press R to restart.";
                    FinalScoreLabel.Left    = GameWindow.Width / 2 - FinalScoreLabel.Width / 2;
                    FinalScoreLabel.Top     = GameOverLabel.Bottom + 13;
                    FinalScoreLabel.Visible = true;
                }

                if (arrow != null && IsColliding(m) == true) // Check if the an arrow has collided with an EnemyObject
                {
                    // Update the score
                    playerPoints   += m.killPoints;
                    ScoreLabel.Text = "Score: " + playerPoints;

                    // Calculate where to put unused objects based on resolution
                    y = GameWindow.Height + 256;

                    // Move the arrow off screen and set to null for garbage collection
                    arrow.SendToTrash(y);
                    arrow = null;

                    // Move the EnemyObject off screen and set to null for garbage collection
                    m.SendToTrash(y);
                    m = null;
                }
            }

            if (arrow != null && arrow.xPos > GameWindow.Width) // Check if an arrow has left the screen
            {
                arrow = null;                                   // Set arrow to null for garbage collection, and to let the player fire a new arrow
            }

            GameWindow.Invalidate(); // Invalidate the picture box to force the graphics to update
        }