Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add game_sate functionality
  • Loading branch information
wenstef committed Dec 28, 2024
commit fde543f2d511000a86fb04202b2e38eca207f0f6
32 changes: 5 additions & 27 deletions Games/Whiskers_Toy_Quest/game_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,15 @@ def __init__(self, whiskers_choices):
self.whiskers_choices = whiskers_choices
self.current_state = 'intro'

def get_next_line(self):
if self.current_state in self.whiskers_choices:
story = self.whiskers_choices[self.current_state]
if self.whiskers_choices.current_story_index < len(story):
line = story[self.whiskers_choices.current_story_index]
if self.whiskers_choices.current_char_index < len(line):
# Get the next character
char = line[self.whiskers_choices.current_char_index]
self.whiskers_choices.current_char_index += 1
return char
else:
# If the whole line is displayed, move to the next line
self.whiskers_choices.current_story_index += 1
self.whiskers_choices.current_char_index = 0
return '\n' # New line after the whole line is displayed
else:
# Move to the next state after the story is complete
self.transition_to_next_state()
return None # No more lines to display
return None

def get_current_stage_info(self):
if self.current_state == 'intro':
return { 'scene_desc': self.get_next_line(), 'question': None }
"""Retrieve the current stage's information."""
return self.whiskers_choices.get_stage_info(self.current_state)

def transition_to_next_state(self, choice=None):
def transition_to_next_state(self, choice):
"""Move to the next state based on the user's choice."""
current_info = self.whiskers_choices.get_stage_info(self.current_state)
next_states = current_info['next_state']
next_states = current_info.get('next_state', {})
if choice in next_states:
self.current_state = next_states[choice]
return True
else:
return False
return False
17 changes: 11 additions & 6 deletions Games/Whiskers_Toy_Quest/whiskers_choices.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
class WhiskersChoices:
def __init__(self):
self.current_state = "intro"
self.current_story_index = 0
self.current_char_index = 0
self.choices = {
"intro": {
"image": "living_room.jpg",
Expand All @@ -23,13 +20,14 @@ def __init__(self):
"image": "under_bed.jpg",
"scene_desc": "You found a hidden stash of toys under the bed! But none of them are Whiskers' favorite toy.",
"question": "What would you like to do next? Type 'take toy' or 'leave the bedroom' and press Enter.",
"next_state": {"take toy": "play_with_toy", "leave the bedroom": "hallway"},
"next_state": {"take toy": "play_with_toy_ending", "leave the bedroom": "hallway"},
},
"hallway": {
"image": "hallway.jpg",
"scene_desc": "You are in the hallway. There are several rooms you can explore. But you can also leave the house and go outside.",
"question": "Where would you like to go? Type 'kitchen', 'living room', or 'leave the house and go outside' and press Enter.",
"next_state": {"kitchen": "kitchen", "living room": "living_room", "outside": "outside"},
"next_state": {"kitchen": "kitchen", "living room": "living_room", "leave the house and go outside":
"outside"},
},
"kitchen": {
"image": "eating.jpg",
Expand All @@ -53,7 +51,7 @@ def __init__(self):
"image": "eating.jpg",
"scene_desc": "You are eating food in the kitchen. Yum! But you're so distracted by the food that you forget about the toy. You can continue eating or start over.",
"question": "What would you like to do next? Type 'continue eating' or 'leave the kitchen' and press Enter.",
"next_state": {"continue eating": "eating_ending", "leave the kitchen": "start_over"},
"next_state": {"continue eating": "eating_ending", "leave the kitchen": "hallway"},
},
"tree": {
"image": "tree.jpg",
Expand Down Expand Up @@ -91,6 +89,13 @@ def __init__(self):
"question": "Would you like to keep looking for the toy or take a nap? Type 'keep looking' or 'nap'.",
"next_state": {"keep looking": "hallway", "nap": "nap_ending"},
},
"play_with_toy_ending": {
"image": "whiskers.jpg",
"scene_desc": "Whiskers plays with another toy, it's not his favorite. Maybe next time he'll "
"find it.",
"question": "The adventure ends here. Would you like to start over? Type 'yes' or 'no'.",
"next_state": {"yes": "intro", "no": "ending"},
},
"nap_ending": {
"image": "whiskers.jpg",
"scene_desc": "Whiskers falls asleep on the couch, dreaming again about his toy. Maybe next time he'll find it.",
Expand Down