top of page

Moms Who Code: Color Match Memory Game With Python + wxPython

Welcome back to Moms Who Code: Creative Projects for Life & Learning!  If this is your first time here, I recommend starting with the introductory post where I explain the heart behind this series and who it's for (spoiler: busy, creative moms like you and me who want to build cool things with code).

Mom holding her toddler excited about the laptop screen they are looking at with a color match memory game in wxPython and Python

This month, I’m so excited to share our very first project: a Color Match Memory Game that you can build using Python and wxPython. This is a perfect little intro project to get you comfortable working with graphical interfaces, and it’s simple enough to complete even if you only have short pockets of time here and there.


What We’re Building

This is a basic memory game where the player clicks on squares to reveal hidden colors and tries to find the matching pairs. The game tracks how many moves it takes to complete all matches. It’s a fun, interactive way to start learning how to code with a simple game that your young children can play.


Tools You’ll Need

  • Python 3.x (any recent version will do)

  • wxPython (you can install it with pip install wxPython)

  • A text editor or IDE you like (I personally love VSCode for this)


If you’re brand new to wxPython, don’t worry. You can go to this read my post about wxPython to read about how to get setup with a sample project before you start this one.


Coding Your Color Match Memory Game

We will start off by creating a 4 X 4 grid of buttons that hide colors underneath. When a player clicks a button, it shows the color. If two buttons match, they stay visible. If not, they flip back.

import wx           # GUI library for making desktop apps
import random       # Used to shuffle colors randomly

# Define the game window
class MemoryGame(wx.Frame):

    def __init__(self):
        # Set up the main game window
        super().__init__(None, title="Color Match Memory Game", size=(400, 400))
        
        # Add a panel to hold all the buttons
        self.panel = wx.Panel(self)
        
        # Create a 4x4 grid with 5px padding between buttons
        self.grid = wx.GridSizer(4, 4, 5, 5)

        # List of 8 colors, each appearing twice (total of 16 cards)
        self.colors = ['red', 'blue', 'green', 'yellow', 
                       'orange', 'purple', 'pink', 'cyan'] * 2
        random.shuffle(self.colors)  # Shuffle to mix them up

        self.buttons = []    # Store the button widgets
        self.revealed = []   # Keep track of revealed buttons
        self.moves = 0       # Count how many turns have been taken
        self.matched = []    # List of locked buttons to prevent matched cards from being reclicked

        # Create and place 16 buttons into the grid
        for i in range(16):
            btn = wx.Button(self.panel, size=(80, 80))
            btn.SetBackgroundColour(wx.Colour(200, 200, 200))  # Gray when hidden
            btn.Bind(wx.EVT_BUTTON, self.on_click)  # Set up click event
            self.buttons.append(btn)
            self.grid.Add(btn, 0, wx.EXPAND)

        # Set the layout and show the game
        self.panel.SetSizer(self.grid)
        self.Show()

    def on_click(self, event):
        # Handle when a button is clicked
        btn = event.GetEventObject()
        idx = self.buttons.index(btn)

        # Don't allow clicking if:
        # - It's already matched
        # - It's already revealed
        if btn in self.matched or btn.GetBackgroundColour() != wx.Colour(200, 200, 200):
            return

        # Reveal the color behind the button
        btn.SetBackgroundColour(self.colors[idx])
        self.revealed.append((btn, self.colors[idx]))

        # If two cards are revealed, check for a match
        if len(self.revealed) == 2:
            wx.CallLater(500, self.check_match)  # Wait 500ms before checking

    def check_match(self):
        # Compare the two revealed buttons
        btn1, color1 = self.revealed[0]
        btn2, color2 = self.revealed[1]

        self.moves += 1  # Increase move count

        # If they don’t match, hide the colors again
        if color1 != color2:
            btn1.SetBackgroundColour(wx.Colour(200, 200, 200))
            btn2.SetBackgroundColour(wx.Colour(200, 200, 200))
        else:
            self.matched.extend([btn1, btn2])

        # Reset for next turn
        self.revealed = []

        # Check for winner
        if len(self.matched) == len(self.buttons):
            # Win message
            wx.MessageBox(f"You won in {self.moves} moves!", "Congratulations")


# This part ensures the game runs when you run the script
if __name__ == '__main__':
    app = wx.App(False)
    frame = MemoryGame()
    app.MainLoop()

What’s Happening in the Code?

  • Buttons are placed in a 4x4 grid with each hiding a color.

  • When a button is clicked, its color is revealed.

  • Two buttons at a time are compared. If the colors match, they stay revealed. If not, they go back to gray.

  • The game keeps track of how many moves the player has made.


Running Your Color Match Memory Game

Once the code is saved in a file (you can call it something like memory_game.py), run it by opening your terminal or command prompt and typing:

python memory_game.py 

And just like that, you have your first game! Test it out to see how it runs, then try a few of the enhancements below.


Game Enhancements

Now that you’ve got a working and polished game, you could challenge yourself by adding some or all of the enhancements that I have listed before.


  • Add a restart button

  • Track and display time

  • Add emojis instead of colors

  • Let your child design the button graphics — make it a family coding-art collab!

  • Add Levels or Difficulty Modes and let your kiddo choose from Easy (4x4), Medium (5x5), or Hard (6x6) layouts — simply change the grid size and add more color pairs.

  • Offer fun themes like “Animals,” “Desserts,” “Under the Sea,” or “Outer Space.” Each theme could come with a different set of icons or color palettes.


This is a great little afternoon activity and helps teach them logic and sequencing in a playful way.


Final Coding Inspiration

I hope this first project inspires you to keep building things—even if it’s five minutes here, twenty minutes there. Coding doesn’t have to be all or nothing. Sometimes just making something small and fun can really spark joy and help us keep learning.


Let me know if you try it, or tag me if you share it online! I’d love to feature some of your projects in a future post. If you have any ideas for our next project in this series, then leave it below in the comments too.

4 Comments


Guest
2 days ago

sunwinw.net hôm bữa mình thấy bạn share nên bấm vào coi thử cho biết thôi. Mình cũng không ngồi tìm hiểu sâu hay làm gì nhiều, chủ yếu xem giao diện họ làm ra sao. Vừa vào là thấy trang sắp xếp khá thoáng, các khối nội dung tách bạch nên nhìn không rối mắt. Mình để ý cái menu nằm chỗ dễ thấy, chuyển qua lại vài mục cũng mượt, không phải kéo lên kéo xuống tìm hoài. Chữ với bảng thông tin canh theo cột nhìn gọn, lướt nhanh vẫn nắm được ý chính. Nói chung cảm giác kiểu làm cho người mới vào cũng không bị ngợp, vì mọi thứ chia thành từng khung rõ ràng ngay…

Like

Guest
4 days ago

Really liked how this was written — it’s the kind of thing you can skim once and still get what the point is. Nothing felt padded out, and the examples didn’t come off like textbook filler. I ended up clicking around https://newimage.io/ halfway through because it reminded me of the same “keep it simple” vibe, and it didn’t feel like a maze. What helped most here was the way everything’s split into small chunks, so you don’t lose the thread when you scroll. Also appreciated that the page doesn’t throw weird formatting at you, so your eyes aren’t constantly hunting for where you left off. The headings and short sections make the site feel easy to move through.

Like

Guest
Jun 01

KUWIN mình mới ghé thử vì thấy bạn bè nhắc hoài, vào kiểu xem nhanh giao diện thôi chứ chưa chơi gì. Cảm giác đầu tiên là trang nhìn khá gọn, chữ nghĩa dễ đọc, không bị nhồi nhét. Mình lướt qua phần giải đáp thấy họ có nhắc chuyện kết quả vận hành bằng RNG chuẩn quốc tế, đọc qua cũng đỡ lăn tăn hơn chút. Mấy mục trên menu đặt ngay ngắn nên bấm qua lại không bị lạc, nhất là lúc muốn tìm thông tin thì không phải kéo quá nhiều. Nói chung trải nghiệm “xem thử” thôi mà thấy ổn, vì các khối “TIN TỨC” và “Hướng Dẫn & Giải Đáp” tách riêng rõ ràng ngay…

Like

Guest
May 23

https://tylekeopro.com/ dạo này thấy mọi người nhắc hoài nên mình cũng bấm vào coi thử cho biết, kiểu lướt nhanh chứ không ngồi đọc kỹ. Cảm giác đầu tiên là trang nhìn sáng và thoáng, không bị nhồi chữ nên mắt đỡ mệt. Mình thích cái cách họ chia nội dung thành từng khối rõ ràng, kéo xuống là biết mình đang ở phần nào luôn, không bị lạc. Thanh menu cũng để ngay chỗ dễ thấy nên muốn chuyển mục thì bấm cái là qua, khỏi phải mò. Nói chung mình chỉ cần giao diện dễ nhìn vậy là đủ để nhớ mặt trang rồi. Lướt một vòng thấy bố cục theo block với menu đặt nổi bật nên…

Like

The Southern Tech Lady

  • Dribbble
  • Behance
  • Instagram
  • Facebook
  • Twitter
  • LinkedIn

©2025 by The Southern Tech Lady

bottom of page