Skip to content

Commit

Permalink
Merge pull request #815 from AshokKumar7070/patch-7
Browse files Browse the repository at this point in the history
Create money_memory_game.py
  • Loading branch information
Ayushparikh-code authored Oct 27, 2024
2 parents c128cbf + a08a058 commit 05a88b1
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions money_memory_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import random
import tkinter as tk
from tkinter import messagebox

# Initialize the main window
window = tk.Tk()
window.title("Money Memory Game")
window.geometry("400x400")

# List of card values (numbers for simplicity)
cards = list(range(1, 9)) * 2
random.shuffle(cards)

# Variables to track the game state
first_card = None
first_button = None
matches_found = 0
attempts = 0

# Function to check for matches between two selected cards
def check_match(btn, idx):
global first_card, first_button, matches_found, attempts

# Disable the button and show the card value
btn.config(text=str(cards[idx]), state="disabled")

# First card selection
if first_card is None:
first_card = cards[idx]
first_button = btn
else:
# Second card selection
if first_card == cards[idx]: # If cards match
matches_found += 1
first_card = None
first_button = None

# Check if all matches are found
if matches_found == 8:
messagebox.showinfo("Game Over", f"Congratulations! You won in {attempts} attempts!")
else: # If cards don't match
window.after(1000, hide_cards, btn, first_button)
first_card = None
first_button = None

attempts += 1

# Function to hide cards if they don't match
def hide_cards(btn1, btn2):
btn1.config(text="?", state="normal")
btn2.config(text="?", state="normal")

# Create the buttons for the game board (4x4 grid)
buttons = []
for i in range(16):
btn = tk.Button(window, text="?", width=10, height=3,
command=lambda i=i: check_match(buttons[i], i))
btn.grid(row=i // 4, column=i % 4)
buttons.append(btn)

# Start the game
window.mainloop()

0 comments on commit 05a88b1

Please sign in to comment.