C# Tutorial – Spawn Animated Wind mill Images Dynamically Using Paint Event and Animator Class
- Subject: C# Tutorials
- Learning Time:
Hi in this tutorial we will take it one step further from the last tutorial and look at how to spawn animated transparent images to windows forms using the paint and the animator class. This tutorial will use some object oriented programming structure, where we will be making a separate class for the Wind Mills and they will be placed dynamically in the windows form application. We will be binding those windmills to a LIST where we will keep track of the items spawned in the form. This way we will be able to check how many items are inside of the list. In the future tutorials we will also show how to spawn and remove them from the screen. This was a really fun tutorial to make and it opens up a lot of opportunities for us to make fun and responsive applications in visuals studio. Hopefully you find this informative and useful.
Lesson Objectives –
- Create a windows form app that spawns multiple windmil images using paint and timer event
- Use object oriented programming to create a windmil class and spawn the images using c# script only
- Work inside of visual studio to make this application
- Use different variables such as Boolean, string, integer and lists to control the application
- Use loops, functions and events in the project
- Work with transparent images and make sure its all being dynamically painted on the form
Video Tutorial –
Download the windmill and background images here
Form1.cs Source Code
namespace Spawn_Different_Windmills_MOOICT { public partial class Form1 : Form { List windMillCollection = new List(); int spawnTimer = 20; int tempTime; public Form1() { InitializeComponent(); } private void GameTimerEvent(object sender, EventArgs e) { tempTime--; if (tempTime < 1) { AddWindMills(); tempTime = spawnTimer; } this.Text = "Total number of WindMills - " + windMillCollection.Count + " MOO ICT"; } private void DrawWindMillsEvent(object sender, PaintEventArgs e) { ImageAnimator.UpdateFrames(); foreach (WindMill tempImg in windMillCollection) { e.Graphics.DrawImage(tempImg.windMill, tempImg.positionX, tempImg.positionY, tempImg.width, tempImg.height); } } private void AddWindMills() { WindMill newWindMill = new WindMill(); windMillCollection.Add(newWindMill); for (int i = 0; i < windMillCollection.Count; i++) { ImageAnimator.Animate(windMillCollection[i].windMill, this.OnFrameChangeHandler); } } private void OnFrameChangeHandler(object? sender, EventArgs e) { this.Invalidate(); } } }
Windmill Class Source Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace Spawn_Different_Windmills_MOOICT { internal class WindMill { Random random = new Random(); public int positionX; public int positionY; public int height; public int width; public Image windMill; public WindMill() { positionX = random.Next(10, 760); positionY = random.Next(10, 400); height = 50; width = 50; windMill = Properties.Resources.Windmill; } } }