-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicGame.py
110 lines (88 loc) · 3.02 KB
/
musicGame.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
import pygame
import random
# Initialize pygame for sound
pygame.init()
# Notes
notes = {
'C': pygame.mixer.Sound('C.wav'),
'D': pygame.mixer.Sound('D.wav'),
'E': pygame.mixer.Sound('E.wav'),
'F': pygame.mixer.Sound('F.wav')
}
keys = ['a', 's', 'd', 'f'] # Mapping: a = C, s = D, etc.
mapping = {'a': 'C', 's': 'D', 'd': 'E', 'f': 'F'}
def play_sequence(seq):
for note in seq:
notes[note].play()
pygame.time.delay(500) # Delay between notes
def get_user_input():
inp = input("Enter the sequence (use 'a', 's', 'd', 'f'): ").strip()
if not inp: # Check if the input is empty
print("Input cannot be empty. Please enter a valid sequence.")
return get_user_input() # Ask for input again
for char in inp:
if char not in mapping and char != "q":
print("Invalid option. Please choose again.")
return get_user_input()
else:
return inp
def show_main_menu():
print("\nOptions:")
print("p - Play the sequence again")
print("i - Input manue")
print("q - Quit the game")
choice = input("Select an option: ").lower()
return choice
def show_input_menu():
print("\nOptions:")
print("i - Input sequence")
print("t - Test notes")
print("b - go back")
choice = input("Select an option: ").lower()
return choice
def test_notes():
while True:
user_input = get_user_input()
if user_input == "q":
break
user_sequence = generate_sequence(user_input)
play_sequence(user_sequence)
def generate_sequence(user_input):
print(user_input)
return [mapping[char] for char in user_input]
def main():
level = 1
while True:
print(f"\nLevel {level}")
sequence = [random.choice(['C', 'D', 'E', 'F']) for _ in range(level + 2)]
# Play the sequence
play_sequence(sequence)
while True:
choice = show_main_menu()
if choice == 'p':
# Replay the sequence
play_sequence(sequence)
elif choice == 'i':
choice = show_input_menu()
if choice == "i":
# Input the answer
user_input = get_user_input()
user_sequence = generate_sequence(user_input)
play_sequence(user_sequence)
if user_sequence == sequence:
print("Correct! Moving to next level.")
level += 1
break
else:
print("Incorrect, try again.")
if choice == "t":
test_notes()
elif choice == 'q':
# Quit the game
print("Thanks for playing!")
pygame.quit()
exit()
else:
print("Invalid option. Please choose again.")
if __name__ == "__main__":
main()