-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
277 lines (213 loc) · 10.1 KB
/
game.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
import curses
import random
import secrets
import pygame
pygame.init()
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 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 play_sequence(seq):
for note in seq:
notes[note].play()
pygame.time.delay(500) # Delay between notes
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()
# 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 generate_sequence(user_input):
print(user_input)
return [mapping[char] for char in user_input]
def music_mode(stdscr, current_stage):
# This mode is for testing notes and playing sequences anytime
while True:
# Display the music mode menu
stdscr.clear()
stdscr.addstr(0, 0, "Music Mode: Press 't' to test notes, 'p' to play a sequence, 'q' to quit music mode.")
stdscr.refresh()
key = stdscr.getch()
if key == ord('t'):
test_notes() # Let the player test individual notes
elif key == ord('p'):
# Play a random sequence for practice (not tied to the music lock)
sequence = [random.choice(['C', 'D', 'E', 'F']) for _ in range(current_stage + 2)]
play_sequence(sequence) # Play the sequence
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 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 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()
success = music_puzzle(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()
curses.napms(1000) # Pause for a moment to show the result
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()
break # Exit movement mode after stage transition
return current_stage # Return the new current stage after exiting movement mode
def music_puzzle(level):
# The sequence length increases with the level
sequence_length = level + 2
sequence = [random.choice(['C', 'D', 'E', 'F']) for _ in range(sequence_length)]
# Play the sequence
play_sequence(sequence)
# Get user input
user_input = get_user_input()
user_sequence = generate_sequence(user_input)
# Check if the input matches the sequence
if user_sequence == sequence:
print("Correct! Moving to the next stage.")
return True # Success
else:
print("Incorrect sequence.")
return False # Failure
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)