Zum Hauptinhalt springen
Podcasts & Audio Briefings9to5Google: Googlebook Eve(18.09.2026 um 22:56 Uhr)
YouTube Security VideosDream it, Build it, Ship it! | GitHub Copilot Day(18.09.2026 um 22:42 Uhr)
Web Security TippsGoogle Workspace Weekly Recap - September 18, 2026(18.09.2026 um 20:38 Uhr)
Sichere ProgrammierungWhy I Built ToolStash: Free, Client-Side Developer Tools(18.09.2026 um 22:27 Uhr)
Sichere ProgrammierungFour bugs shipping a live x402 endpoint(18.09.2026 um 22:40 Uhr)
Sichere ProgrammierungAI-Assisted Coding: The Authentication Bug We Almost Overlooked(18.09.2026 um 22:45 Uhr)
Sichere ProgrammierungRL 3: Bellman Equations and Markov Decision Processes (1950s–1960s)(18.09.2026 um 22:47 Uhr)
Podcasts & Audio Briefings9to5Google: Googlebook Eve(18.09.2026 um 22:56 Uhr)
YouTube Security VideosDream it, Build it, Ship it! | GitHub Copilot Day(18.09.2026 um 22:42 Uhr)
Web Security TippsGoogle Workspace Weekly Recap - September 18, 2026(18.09.2026 um 20:38 Uhr)
Sichere ProgrammierungWhy I Built ToolStash: Free, Client-Side Developer Tools(18.09.2026 um 22:27 Uhr)
Sichere ProgrammierungFour bugs shipping a live x402 endpoint(18.09.2026 um 22:40 Uhr)
Sichere ProgrammierungAI-Assisted Coding: The Authentication Bug We Almost Overlooked(18.09.2026 um 22:45 Uhr)
Sichere ProgrammierungRL 3: Bellman Equations and Markov Decision Processes (1950s–1960s)(18.09.2026 um 22:47 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Pattern matching for everyone (Using in video games)

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

Official Documentation
Switch expression
DoubleBuffered

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Pattern matching for everyone (Using in video games)

Thematisch verwandte Begriffe: Pattern, matching, everyone, Using · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-61591 | djust provides Phoenix LiveView-style reactive server-side rendering for…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Rechts: Artikel Ziehen Links: RSS
Hoch: nächster Artikel Runter: zurück / schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Rechts: Original Links: RSS-Ansicht
↗ Original-Quelle
Social Reaktionen Stimme abgeben (+5 Karma)
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick