Skip to content

Commit 05a88b1

Browse files
Merge pull request #815 from AshokKumar7070/patch-7
Create money_memory_game.py
2 parents c128cbf + a08a058 commit 05a88b1

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

money_memory_game.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import random
2+
import tkinter as tk
3+
from tkinter import messagebox
4+
5+
# Initialize the main window
6+
window = tk.Tk()
7+
window.title("Money Memory Game")
8+
window.geometry("400x400")
9+
10+
# List of card values (numbers for simplicity)
11+
cards = list(range(1, 9)) * 2
12+
random.shuffle(cards)
13+
14+
# Variables to track the game state
15+
first_card = None
16+
first_button = None
17+
matches_found = 0
18+
attempts = 0
19+
20+
# Function to check for matches between two selected cards
21+
def check_match(btn, idx):
22+
global first_card, first_button, matches_found, attempts
23+
24+
# Disable the button and show the card value
25+
btn.config(text=str(cards[idx]), state="disabled")
26+
27+
# First card selection
28+
if first_card is None:
29+
first_card = cards[idx]
30+
first_button = btn
31+
else:
32+
# Second card selection
33+
if first_card == cards[idx]: # If cards match
34+
matches_found += 1
35+
first_card = None
36+
first_button = None
37+
38+
# Check if all matches are found
39+
if matches_found == 8:
40+
messagebox.showinfo("Game Over", f"Congratulations! You won in {attempts} attempts!")
41+
else: # If cards don't match
42+
window.after(1000, hide_cards, btn, first_button)
43+
first_card = None
44+
first_button = None
45+
46+
attempts += 1
47+
48+
# Function to hide cards if they don't match
49+
def hide_cards(btn1, btn2):
50+
btn1.config(text="?", state="normal")
51+
btn2.config(text="?", state="normal")
52+
53+
# Create the buttons for the game board (4x4 grid)
54+
buttons = []
55+
for i in range(16):
56+
btn = tk.Button(window, text="?", width=10, height=3,
57+
command=lambda i=i: check_match(buttons[i], i))
58+
btn.grid(row=i // 4, column=i % 4)
59+
buttons.append(btn)
60+
61+
# Start the game
62+
window.mainloop()

0 commit comments

Comments
 (0)