C# Tutorial – Create a Football Penalty Kick Game in Visual Studio with Win Forms

Welcome to this Football Penalty Kick Game tutorial. This is the improved version of the existing game and we will be making the similar game as we have before. We fixed some of the syntax’s and made it easier with the full video tutorial for this game. You can check out the other football penalty shootout game.

In this tutorial we will create a football penalty shootout game in visual studio using c# programming language. In this game you are able to click on a selective targets around the goal post and as you select one it will shoot the football towards it. The goal keeper has its own independent movements and he can block your goals from being made. The overall structure of the game is simple and easy to understand.  We are creating this tutorial in Visual Studio 2017 using the Windows Form application and C# programming language. Make sure you download the game assets from the link in this tutorial page to follow along.

Lesson Objectives –

  1. Create a football penalty shootout game in visual studio using c#
  2. Create click events using picture boxes
  3. Create goal keeper movements using lists and strings
  4. Using a timer object to animate the ball and the goal keeper
  5. Using image sprites and changing the goal keepers state within the game
  6. Using various data types such as Integers, Booleans, Strings and List to make the game work
  7. Using windows form components such as Picture Boxes, Timers and Labels to complete the game

 

 

Download the images for this penalty kick game here.

 

Full Source Code for the Football Penalty Kick Game –

Please make sure you follow the video tutorial fully in order to achieve the final result. The best way to learn the code is rewrite the code yourself this way you can double check the syntax and also see how they relate to the overall program.

 

namespace Football_Pentalty_Shootout_Game_MOO_ICT
{
    public partial class Form1 : Form
    {

        // MOOICT.COM 2022
        // made for educational purpose only

        List KeeperPosition = new List { "left", "right", "top", "topLeft", "topRight"};
        List goalTarget;
        int ballX = 0;
        int ballY = 0;
        int goal = 0;
        int miss = 0;
        string state;
        string playerTarget;
        bool aimSet = false;
        Random random = new Random();

        public Form1()
        {
            InitializeComponent();
            goalTarget = new List { left, right, top, topLeft, topRight};
        }

        private void SetGoalTargetEvent(object sender, EventArgs e)
        {
            if (aimSet == true) { return; }

            BallTimer.Start();
            KeeperTimer.Start();
            ChangeGoalKeeperImage();    

            var senderObject = (PictureBox)sender;
            senderObject.BackColor = Color.Beige;

            if (senderObject.Tag.ToString() == "topRight")
            {
                ballX = -7;
                ballY = 15;
                playerTarget = senderObject.Tag.ToString();
                aimSet = true;
            }
            if (senderObject.Tag.ToString() == "right")
            {
                ballX = -11;
                ballY = 15;
                playerTarget = senderObject.Tag.ToString();
                aimSet = true;
            }
            if (senderObject.Tag.ToString() == "top")
            {
                ballX = 0;
                ballY = 20;
                playerTarget = senderObject.Tag.ToString();
                aimSet = true;
            }
            if (senderObject.Tag.ToString() == "topLeft")
            {
                ballX = 8;
                ballY = 15;
                playerTarget = senderObject.Tag.ToString();
                aimSet = true;
            }
            if (senderObject.Tag.ToString() == "left")
            {
                ballX = 7;
                ballY = 8;
                playerTarget = senderObject.Tag.ToString();
                aimSet = true;
            }

            CheckScore();

        }

        private void KeeperTimerEvent(object sender, EventArgs e)
        {
            switch (state)
            {

                case "left":
                    goalKeeper.Left -= 6;
                    goalKeeper.Top = 204;
                    break;
                case "right":
                    goalKeeper.Left += 6;
                    goalKeeper.Top = 204;
                    break;
                case "top":
                    goalKeeper.Top -= 6;
                    break;
                case "topLeft":
                    goalKeeper.Left -= 6;
                    goalKeeper.Top -= 3;
                    break;
                case "topRight":
                    goalKeeper.Left += 6;
                    goalKeeper.Top -= 3;
                    break;
            }

            foreach (PictureBox x  in goalTarget)
            {
                if (goalKeeper.Bounds.IntersectsWith(x.Bounds))
                {

                    KeeperTimer.Stop();
                    goalKeeper.Location = new Point(418, 169);
                    goalKeeper.Image = Properties.Resources.stand_small;

                }
            }


        }

        private void BallTimerEvent(object sender, EventArgs e)
        {
            football.Left -= ballX;
            football.Top -= ballY;

            foreach (PictureBox x  in goalTarget)
            {
                if (football.Bounds.IntersectsWith(x.Bounds))
                {

                    football.Location = new Point(430, 500);
                    ballX = 0;
                    ballY = 0;
                    aimSet = false;
                    BallTimer.Stop();
                }
            }


        }

        private void CheckScore()
        {
            if (state == playerTarget)
            {
                miss++;
                lblMissed.Text = "Missed: " + miss;
            }
            else
            {
                goal++;
                lblScore.Text = "Scored: " + goal;
            }
        }

        private void ChangeGoalKeeperImage()
        {
            KeeperTimer.Start();
            int i = random.Next(0, KeeperPosition.Count);
            state = KeeperPosition[i];

            switch (i)
            {
                case 0:
                    goalKeeper.Image = Properties.Resources.left_save_small;
                    break;
                case 1:
                    goalKeeper.Image = Properties.Resources.right_save_small;
                    break;
                case 2:
                    goalKeeper.Image = Properties.Resources.top_save_small;
                    break;
                case 3:
                    goalKeeper.Image = Properties.Resources.top_left_save_small;
                    break;
                case 4:
                    goalKeeper.Image = Properties.Resources.top_right_save_small;
                    break;
            }
        }

    }
}

 




Comments are closed.