Josh Quezada
Posted on November 14, 2024
Today's tutorial is a pattern that in my opinion we as programmers use very commonly but sometimes we don't even stop to investigate what we are using or why we are using it (Me included).
Since I saw this pattern, I immediately thought it could be used in video games. Pattern Matching is based on verifying what type of information is being used to perform some action or some block of code using Boolean expressions such as if or switch.
This patter allows you to use multiple operations but I'll leave official documentation for it here.
Anyway, in today's tutorial I will use an object, creating it, programming its movement, actions and of course, verifying the actions taken by the user to do something.
1.First of all we'll use a "Windows Forms App".
2.Secondly we won't create any object using ToolBox.
3.Finally I'm using Visual Studio 2022 using C# 8.0.
The file we are interested on is "Form1.cs" (you can change the name of the file if you want).
I won't change anything about 'Form1()' method but I'll modify the 'InitializeCommponent()' later.
We need to create initial positions and size of our game object at that file:
//Initial positions of the GameObject
private int x = 100;
private int y = 100;
//Size of the GameObject
private int objectSize = 50;
As I said I will not use any object from toolBox, so we need to draw the object, in this case I preferred to use a circle but the Graphics
allows you to create multiple objects:
//Draw object at cuttent position
private void OnPaint(object sender, PaintEventArgs e)
{
using (var brush = new SolidBrush(Color.Green))
{
e.Graphics.FillEllipse(brush, x, y, objectSize, objectSize);
}
}
Now we get into the pattern, we have the next code where is using the switch expression:
private void OnKeyDown(Object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
x -= 10;
break;
case Keys.Right:
x += 10;
break;
case Keys.Up:
y -= 10;
break;
case Keys.Down:
y += 10;
break;
}
//Redraw the object after moving
this.Invalidate();
}
What is the Pattern matching doing here?
Simple, it verifies the key event we programmed (in this case I decided to use the arrow keys but can be changed very easily) and depending of what we pressed is going to do, moving up, moving down, you can even program "z" direction, remember that game development base is mathematics.
And finally we can go to "InitializeComponent()" method I mentioned before.
-> Setting DoubleBuffered
to true gives us the perspective of an animation and helps reduce or avoid information flickering.
-> We call our Paint method and our Key event method.
private void InitializeComponent()
{
this.DoubleBuffered = true;
this.Paint += new PaintEventHandler(OnPaint);
this.KeyDown += new KeyEventHandler(OnKeyDown);
SuspendLayout();
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
BackColor = SystemColors.ButtonShadow;
ClientSize = new Size(800, 450);
Name = "Form1";
Text = "PatternMatching";
ResumeLayout(false);
}
And that's all! You programmed a movement for an object and you learned about the pattern matching.
In conclusion
You can use the pattern matching using switch, if, or any change expression. As well this pattern contains other types of patterns that can be used depending on what you want to do (Positional Pattern, Logical Pattern, etc). Of course this pattern can be used on multiple types of apps, is not mandatory to use it on videogames.
Remember this is just an example for everyone to understand and if you have something to add please let me know and grow together.
References
Posted on November 14, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.