-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
159 lines (130 loc) · 4.13 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import tkinter as tk
import random
from time import sleep
import pygame
import sys
import os
# Get the base path (compatible with PyInstaller bundling)
BASE_PATH = getattr(
sys,
'_MEIPASS',
os.path.dirname(os.path.abspath(__file__))
)
# Load sounds dynamically from the correct path
SOUNDS = {
"red": os.path.join(BASE_PATH, "red.wav"),
"green": os.path.join(BASE_PATH, "green.wav"),
"blue": os.path.join(BASE_PATH, "blue.wav"),
"yellow": os.path.join(BASE_PATH, "yellow.wav"),
}
# Initialize pygame mixer
pygame.mixer.init()
sounds = {color: pygame.mixer.Sound(path) for color, path in SOUNDS.items()}
# Initialize variables
sequence = []
player_sequence = []
buttons = {}
score = 0
high_score = 0
base_delay = 0.5 # Base delay for flashing buttons
# Add a delay for showing the sequence and play sound
def flash_button(color, delay):
sounds[color].play() # Play the corresponding sound
buttons[color].config(bg="white")
root.update()
sleep(delay)
buttons[color].config(bg=color)
root.update()
sleep(0.2)
# Add a new color to the sequence
def add_to_sequence():
colors = list(buttons.keys())
sequence.append(random.choice(colors))
# Play the current sequence
def play_sequence():
# Adjust delay based on score
delay = max(0.2, base_delay - (score * 0.02))
for color in sequence:
flash_button(color, delay)
# Check player's input
def check_sequence():
global player_sequence, sequence, score, high_score
if player_sequence == sequence[:len(player_sequence)]:
if len(player_sequence) == len(sequence):
score += 1 # Increment score for completing a sequence
high_score = max(high_score, score) # Update high score
update_score_labels()
status_label.config(text="Correct! Watch the next sequence.")
player_sequence = []
root.after(1000, next_round)
else:
status_label.config(text="Keep going!")
else:
status_label.config(text="Game Over! Press Start to play again.")
player_sequence = []
sequence = []
score = 0 # Reset score
update_score_labels()
# Start a new round
def next_round():
add_to_sequence()
play_sequence()
status_label.config(text="Your turn!")
# Handle button clicks
def button_click(color):
global player_sequence
player_sequence.append(color)
flash_button(color, 0.2) # Fixed delay for player clicks
check_sequence()
# Start game
def start_game():
global sequence, player_sequence, score
sequence = []
player_sequence = []
score = 0 # Reset score at the start
update_score_labels()
status_label.config(text="Watch the sequence!")
next_round()
# Update the score and high score labels
def update_score_labels():
score_label.config(text=f"Score: {score}")
high_score_label.config(text=f"High Score: {high_score}")
# Create the GUI
root = tk.Tk()
root.title("Pymon Says")
# Path to the icon
ICON_PATH = os.path.join(BASE_PATH, "icon.ico")
root.iconbitmap(ICON_PATH) # Set the window icon
# Create status label
status_label = tk.Label(root, text="Press Start to play!", font=("Arial", 16))
status_label.pack(pady=10)
# Create score and high score labels
score_label = tk.Label(root, text="Score: 0", font=("Arial", 14))
score_label.pack()
high_score_label = tk.Label(root, text="High Score: 0", font=("Arial", 14))
high_score_label.pack()
# Create the buttons
frame = tk.Frame(root)
frame.pack()
colors = ["red", "green", "blue", "yellow"]
for color in colors:
btn = tk.Button(
frame,
bg=color,
width=10,
height=5,
command=lambda c=color: button_click(c)
)
btn.grid(
row=0 if color in ["red", "green"] else 1,
column=0 if color in ["red", "blue"] else 1
)
buttons[color] = btn
# Create the Start button
start_button = tk.Button(
root, text="Start",
command=start_game,
font=("Arial", 14)
)
start_button.pack(pady=10)
root.mainloop()