C# OOP Tutorial Create a Butterfly Catching Game In Windows Forms and Visual Studio

Welcome to this tutorial from MOO ICT. In this tutorial we will be making a Butterfly Catching game in C# and Win Forms in Visual Studio. This tutorial will be a collection of all of the methods we have used in the previous tutorials and we will be able to spawn different GIF images using the Image and Image Animator class in C#. We will be using OOP programming principles to create a butterfly class and then use that class to create a Butterfly Object in the game and animate and move them using the timer component.  The butterflies we generate in the game will be completely transparent so when the images are overlapping you will be able to see the butterfly animation behind it. We will be creating this only using C# and Win Forms so no use for a games engine such as Unity or GODOT. Hope you enjoy this working on this project and lets get started.

Lesson Objectives-

  1. Create a Butterfly catching game using C#
  2. Create a Custom Butterfly Class in C#
  3. Dynamically Paint the images to the form
  4. Create Multiple Forms in Visual Studio. 1 for the title screen with the start button and 2 for the games window
  5. Design both forms using the visual studio components such as picture boxes, labels and timer
  6. Use public functions from the class to move the butterfly randomly across the screen and change directions when needed.
  7. Use variety of variables such as integers, float, string and Boolean.
  8. Since Image class doesn’t have a CLICK event we will need to make one ourselves to register when we click on a butterfly image
  9. Use Image animator class to animate the GIF images
  10. Use an Image array to randomly assignment a different colour butterfly
  11. Create custom functions to display game and restart the game after the time limit has been reached

 

Download the butterfly catching game images

Get C# Butterfly Catching Game Tutorial on GitHub

Butterfly Class Source Code –

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace Butterfly_Catching_Game_MOO_ICT
{
    internal class Butterfly
    {
        public Image butterfly_image;
        public int positionX;
        public int positionY;
        public int height;
        public int width;
        public int speedX, speedY, limit, moveLimit;
        Random rand = new Random();

        public Butterfly()
        {
            limit = rand.Next(200,400);
            moveLimit = limit;

            speedX = rand.Next(-5, 5);
            speedY = rand.Next(-5, 5);

            height = 43;
            width = 60;

        }

        public void MoveButterfly()
        {
            moveLimit--;

            if (moveLimit < 0)
            {
                if (speedX < 0)
                {
                    speedX = rand.Next(2, 5);
                }
                else
                {
                    speedX = rand.Next(-5, -2);
                }
                if (speedY < 0)
                {
                    speedY = rand.Next(2, 5);
                }
                else
                {
                    speedY = rand.Next(-5, -2);
                }

                moveLimit = rand.Next(200, limit);
            }
        }
    }
}

Game Window Source Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Butterfly_Catching_Game_MOO_ICT
{
    public partial class GameWindow : Form
    {
        float timeLeft = 10f;
        int caught = 0;
        int spawnTime = 0;
        int spawnLimit = 30;
        List<Butterfly> butterfly_list = new List<Butterfly>();
        Random rand = new Random();

        Image[] butterfly_images = {Properties.Resources._01, Properties.Resources._02, Properties.Resources._03, Properties.Resources._04, Properties.Resources._05, Properties.Resources._06, Properties.Resources._07, Properties.Resources._08, Properties.Resources._09, Properties.Resources._10 };



        public GameWindow()
        {
            InitializeComponent();
        }

        private void GameTimerEvent(object sender, EventArgs e)
        {
            lblTime.Text = "Time Left: " + timeLeft.ToString("#") + ".s";
            lblCaught.Text = "Caught: " + caught;
            timeLeft -= 0.03f;

            if (butterfly_list.Count < spawnLimit)
            {
                spawnTime--;

                if (spawnTime < 1)
                {
                    MakeButterfly();
                    spawnTime = spawnLimit;
                }
            }

            foreach (Butterfly butterfly in butterfly_list)
            {
                butterfly.MoveButterfly();

                butterfly.positionX += butterfly.speedX;

                if (butterfly.positionX < 0 || butterfly.positionX + butterfly.width  > this.ClientSize.Width)
                {
                    butterfly.speedX = -butterfly.speedX;

                    if (butterfly.positionX < 0)
                    {
                        butterfly.positionX = butterfly.positionX + 10;
                    }
                    else if (butterfly.positionX + butterfly.width > this.ClientSize.Width)
                    {
                        butterfly.positionX = butterfly.positionX - 10;
                    }
                }

                butterfly.positionY += butterfly.speedY;

                if (butterfly.positionY < 0 || butterfly.positionY + butterfly.height > this.ClientSize.Height - 50 )
                {
                    butterfly.speedY = -butterfly.speedY;

                    if (butterfly.positionY < 0)
                    {
                        butterfly.positionY = butterfly.positionY + 10;
                    }
                    else if (butterfly.positionY + butterfly.height > this.ClientSize.Height - 50)
                    {
                        butterfly.positionY = butterfly.positionY - 10;
                    }
                }


            }


            if (timeLeft < 1)
            {
                GameOver();
            }

            this.Invalidate();

        }

        private void FormClickEvent(object sender, EventArgs e)
        {
            foreach (Butterfly butterfly in butterfly_list.ToList())
            {
                MouseEventArgs mouse = (MouseEventArgs)e;

                if (mouse.X >= butterfly.positionX && mouse.Y >= butterfly.positionY && mouse.X < butterfly.positionX + butterfly.width && mouse.Y < butterfly.positionY + butterfly.height)
                {
                    butterfly_list.Remove(butterfly);
                    caught++;
                }
            }
        }

        private void FormPaintEvent(object sender, PaintEventArgs e)
        {
            ImageAnimator.UpdateFrames();

            foreach (Butterfly butterfly in butterfly_list)
            {
                e.Graphics.DrawImage(butterfly.butterfly_image, butterfly.positionX, butterfly.positionY, butterfly.width, butterfly.height);
            }

        }

        private void MakeButterfly()
        {
            int i = rand.Next(butterfly_images.Length);

            Butterfly newButterFly = new Butterfly();
            newButterFly.butterfly_image = butterfly_images[i];
            newButterFly.positionX = rand.Next(50, this.ClientSize.Width - 200);
            newButterFly.positionY = rand.Next(50, this.ClientSize.Height - 200);
            butterfly_list.Add(newButterFly);
            ImageAnimator.Animate(newButterFly.butterfly_image, this.OnFrameChangedHandler);
        }

        private void OnFrameChangedHandler(object? sender, EventArgs e)
        {
            this.Invalidate();
        }

        private void RestartGame()
        {
            this.Invalidate();
            butterfly_list.Clear();
            caught = 0;
            timeLeft = 60f;
            spawnTime = 0;
            lblTime.Text = "Time: 00";
            lblCaught.Text = "Caught: 0";
            GameTimer.Start();
        }

        private void GameOver()
        {
            GameTimer.Stop();
            MessageBox.Show("Times Up!!, You've Caught " + caught + " butterflies. Click ok to try again.", "MOO says: ");
            RestartGame();
        }
    }
}

 




Comments are closed.