-
Notifications
You must be signed in to change notification settings - Fork 0
/
game2.py
344 lines (272 loc) · 13.6 KB
/
game2.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import curses
import random
import secrets
import time
import pygame
# 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 main(stdscr):
curses.curs_set(0) # Hide the cursor
stdscr.nodelay(False) # Wait for user input in command mode
stdscr.timeout(-1) # No timeout in command mode
# Generate a random 12-digit seed for the entire gameplay
game_seed = secrets.randbelow(10**12) # Random 12-digit number
stdscr.addstr(0, 0, f"Game Seed: {game_seed}") # Display the seed for replay purposes
stdscr.refresh()
time.sleep(1)
# Define stages (12 unique stages)
stages = generate_stages(12, 20, 10, game_seed)
current_stage = 1 # Start at stage 1
player = {'x': 1, 'y': 1, 'char': '@'} # Player starts at (1, 1)
while True:
stdscr.clear() # Clear the screen
# Draw the current stage
draw_stage(stdscr, stages[current_stage]['map'])
# Draw the player
stdscr.addch(player['y'], player['x'], player['char'])
# Handle command input (allow the player to type commands)
command = handle_command_input(stdscr)
if command == 'm':
# Enter movement mode
current_stage = movement_mode(stdscr, stages, current_stage, player)
elif command == 'music':
# Enter music mode (testing and playing notes anytime)
music_mode(stdscr, current_stage)
elif command == 'quit':
break # Quit the game
def movement_mode(stdscr, stages, current_stage, player):
curses.curs_set(0) # Hide cursor in movement mode
stdscr.nodelay(True) # Non-blocking input in movement mode
stdscr.timeout(100) # Refresh rate in movement mode
height, width = stdscr.getmaxyx()
while True:
stdscr.clear() # Clear screen
# Draw the current stage
draw_stage(stdscr, stages[current_stage]['map'])
# Draw the player
stdscr.addch(player['y'], player['x'], player['char'])
# Show the command menu at the bottom while moving
stdscr.addstr(height - 1, 0, "In movement mode. Press '>' on the exit to go to the next stage or 'q' to quit movement.")
stdscr.refresh()
key = stdscr.getch()
# Handle player movement
if key == curses.KEY_UP and player['y'] > 1 and stages[current_stage]['map'][player['y'] - 1][player['x']] != '#':
player['y'] -= 1
elif key == curses.KEY_DOWN and player['y'] < stages[current_stage]['height'] - 2 and stages[current_stage]['map'][player['y'] + 1][player['x']] != '#':
player['y'] += 1
elif key == curses.KEY_LEFT and player['x'] > 1 and stages[current_stage]['map'][player['y']][player['x'] - 1] != '#':
player['x'] -= 1
elif key == curses.KEY_RIGHT and player['x'] < stages[current_stage]['width'] - 2 and stages[current_stage]['map'][player['y']][player['x'] + 1] != '#':
player['x'] += 1
elif key == ord('>'):
if stages[current_stage]['map'][player['y']][player['x']] == '+': # Player is standing on the exit
success = music_puzzle(stdscr,level=current_stage)
if success:
if current_stage < 12: # Move to the next stage if they pass the puzzle
current_stage += 1
player['x'], player['y'] = 1, 1 # Reset player to starting position
stdscr.addstr(height - 1, 0, f"Moved to stage {current_stage}!") # Notify stage change
stdscr.refresh()
time.sleep(1)
break # Exit movement mode after stage transition
else:
stdscr.addstr(height - 1, 0, "Failed the music puzzle! Try again.") # Notify failure
stdscr.refresh()
time.sleep(1)
elif stages[current_stage]['map'][player['y']][player['x']] == 'M': # Player steps on a music lock
stdscr.addstr(0, 0, "You have encountered a music lock!")
stdscr.refresh()
time.sleep(1)
success = music_puzzle(stdscr,current_stage) # Trigger the music puzzle
if success:
stdscr.addstr(1, 0, "You unlocked the music lock.")
else:
stdscr.addstr(1, 0, "Failed to unlock the music lock. Try again.")
stdscr.refresh()
time.sleep(1)
if stages[current_stage]['map'][player['y']][player['x']] == 'M': # Player steps on a music lock
success = music_puzzle(stdscr, current_stage) # Trigger the music puzzle
if success:
stdscr.addstr(1, 0, "You unlocked the music lock!")
else:
stdscr.addstr(1, 0, "Failed to unlock the music lock. Try again.")
stdscr.refresh()
time.sleep(1)
elif key == ord('q'):
break # Exit movement mode
# Handle stage transition
elif key == ord('>'):
if stages[current_stage]['map'][player['y']][player['x']] == '+': # Player is standing on the exit
if current_stage < 12: # Move to the next stage
current_stage += 1
player['x'], player['y'] = 1, 1 # Reset player to starting position
stdscr.addstr(height - 1, 0, f"Moved to stage {current_stage}!") # Notify stage change
stdscr.refresh()
time.sleep(1)
break # Exit movement mode after stage transition
return current_stage # Return the new current stage after exiting movement mode
def music_mode(stdscr, current_stage):
# This mode is for testing notes and playing sequences anytime
sequence_length = current_stage + 2 # The sequence gets longer as the stage increases
sequence = [random.choice(['C', 'D', 'E', 'F']) for _ in range(sequence_length)]
while True:
stdscr.clear()
stdscr.addstr(0, 0, "Music Mode: Press 't' to test notes, 'p' to play a sequence, 'i' to input the sequence, 'q' to quit music mode.")
stdscr.refresh()
key = stdscr.getch()
# if key == ord('t'):
# test_notes(stdscr) # Let the player test individual notes
if key == ord('p'):
play_sequence(sequence) # Play a random sequence for practice (not tied to the music lock)
elif key == ord('i'):
# Get the user input using the curses-based get_user_input
user_input = get_user_input(stdscr, len(sequence))
user_sequence = [mapping[char] for char in user_input] # Convert user input to note sequence
# Check if the user input matches the sequence
if user_sequence == sequence:
stdscr.addstr(1, 0, "Correct! The lock is unlocked.")
stdscr.refresh()
time.sleep(1)
break # Exit music mode on success
else:
stdscr.addstr(1, 0, "Incorrect sequence. Try again.")
stdscr.refresh()
time.sleep(1)
elif key == ord('q'):
break # Quit music mode
# def test_notes():
# while True:
# print("Test notes by pressing 'a', 's', 'd', or 'f'. Press 'q' to quit.")
# user_input = input("Enter a note (or 'q' to quit): ").strip().lower()
# if user_input == 'q':
# break
# elif user_input in mapping:
# notes[mapping[user_input]].play() # Play the corresponding note
# else:
# print("Invalid input. Try again.")
def music_puzzle(stdscr, level):
"""Play a sequence of notes and ask the player to reproduce it."""
sequence_length = level + 2 # The sequence length increases with the level
sequence = [random.choice(['C', 'D', 'E', 'F']) for _ in range(sequence_length)] # Generate a random note sequence
# Display instructions
height, width = stdscr.getmaxyx()
stdscr.clear()
stdscr.addstr(0, 0, "Music Puzzle: Listen to the sequence and repeat it.")
stdscr.addstr(1, 0, "Press 'a', 's', 'd', or 'f' to input the sequence. Press 'q' to quit.")
stdscr.refresh()
# Play the generated sequence
play_sequence(sequence, stdscr)
# Get user input and compare
user_input = get_user_input(stdscr, len(sequence)) # Get the player's input for the same length as the sequence
user_sequence = [mapping[char] for char in user_input] # Convert the input keys to note sequence
# Check if the user's input matches the generated sequence
if user_sequence == sequence:
stdscr.addstr(2, 0, "Correct! You solved the puzzle.")
stdscr.refresh()
time.sleep(1)
return True # Success
else:
stdscr.addstr(2, 0, "Incorrect sequence. Try again.")
stdscr.refresh()
time.sleep(1)
return False # Failure
def play_sequence(sequence, stdscr):
"""Plays a sequence of notes using pygame and updates the terminal."""
for note in sequence:
stdscr.clear()
stdscr.addstr(0, 0, f"Playing note: {note}") # Display the note being played
stdscr.refresh()
notes[note].play() # Play the sound using pygame
pygame.time.delay(500) # Add a 500ms delay between notes
time.sleep(0.5) # Pause in the terminal to let the player process
def get_user_input(stdscr, length):
"""Get user input for a sequence of notes, limited by the length of the sequence."""
user_input = []
height, width = stdscr.getmaxyx()
# Prompt the user to enter the sequence
stdscr.addstr(height - 2, 0, "Enter the sequence: ")
stdscr.refresh()
while len(user_input) < length:
key = stdscr.getch()
# Capture only valid keys ('a', 's', 'd', 'f')
if chr(key) in mapping:
user_input.append(chr(key)) # Store the valid input character
stdscr.addch(height - 2, len("Enter the sequence: ") + len(user_input) - 1, chr(key)) # Display the character
return user_input # Return the list of valid keys pressed
def generate_sequence(user_input):
return [mapping[char] for char in user_input]
def handle_command_input(stdscr):
"""Handles the command input at the bottom of the screen, showing typed characters."""
height, width = stdscr.getmaxyx()
command = "" # Initialize an empty command
while True:
# Show the prompt and the current command typed
stdscr.addstr(height - 1, 0, "Enter 'm' to move, 'music' for music mode, 'quit' to quit the game. Command: ")
stdscr.addstr(height - 1, len("Enter 'm' to move, 'music' for music mode, 'quit' to quit the game. Command: "), command)
stdscr.refresh()
key = stdscr.getch() # Get user input
if key == 10: # Enter key
break # End command input on Enter
elif key in (curses.KEY_BACKSPACE, 127, 8): # Handle backspace
if command:
command = command[:-1] # Remove last character
elif 32 <= key <= 126: # Printable characters
if len(command) < width - len("Enter 'm' to move, 'music' for music mode, 'quit' to quit the game. Command: ") - 1:
command += chr(key) # Add the typed character to the command
stdscr.addstr(height - 1, len("Enter 'm' to move, 'music' for music mode, 'quit' to quit the game. Command: "), " " * (width - len("Enter 'm' to move, 'music' for music mode, 'quit' to quit the game. Command: ") - 1))
stdscr.refresh()
stdscr.addstr(height - 1, 0, " " * (width - 1)) # Clear the entire command line after Enter
stdscr.refresh()
return command.strip() # Return the typed command, stripped of whitespace
def generate_stages(num_stages, width, height, game_seed):
stages = {}
for i in range(1, num_stages + 1):
stage = generate_stage(width, height, game_seed, i) # Use game_seed and stage number for uniqueness
stages[i] = {'map': stage, 'width': width, 'height': height}
return stages
def generate_stage(width, height, game_seed, stage_number):
stage = []
# Seed the random generator with the game seed combined with the stage number
random.seed(game_seed + stage_number)
# Generate empty stage with boundary walls
for y in range(height):
row = []
for x in range(width):
if y == 0 or y == height - 1 or x == 0 or x == width - 1:
row.append('#') # Boundary walls
else:
row.append(' ') # Empty space inside
stage.append(row)
# Place obstacles randomly
obstacle_count = 10
for _ in range(obstacle_count):
while True:
x = random.randint(1, width - 2)
y = random.randint(1, height - 2)
if stage[y][x] == ' ':
stage[y][x] = '#'
break
# Add an exit at a random position on the right side of the stage
exit_x = width - 2
exit_y = random.randint(1, height - 2)
stage[exit_y][exit_x] = '+'
# Add a music lock at a random position
music_lock_x = random.randint(1, width - 2)
music_lock_y = random.randint(1, height - 2)
stage[music_lock_y][music_lock_x] = 'M' # Music lock tile
return stage
def draw_stage(stdscr, stage):
for y, row in enumerate(stage):
for x, ch in enumerate(row):
stdscr.addch(y, x, ch)
if __name__ == "__main__":
curses.wrapper(main)