我正在嘗試創建一個基于回圈文本的“選擇你自己的冒險”故事游戲。代碼內置于單獨的函式中。每個函式都有一個故事元素(以文本格式回傳),用于更新按鈕上的文本的玩家選擇(以字典格式回傳),然后根據玩家所做的選擇(也在字典格式。
完成并正常作業后,文本下方將有兩個按鈕,一旦單擊,將立即更新螢屏上的文本和螢屏底部的按鈕。
從我到目前為止的實驗來看,我似乎無法在它創建視窗的點之前放置一個 while 回圈,并且我無法弄清楚如何在它創建視窗的點之后構建一個回圈。我想我把按鈕映射錯了,但我不知道下一步該去哪里。如果有人有任何想法,那將是最有幫助的。謝謝你。
當前主要功能:
import PySimpleGUI as sg
def main ():
sg.theme("default1")
# All the stuff inside your window.
char_info = character_creation()
char_name = char_info[0]
char_gender = char_info[1]
scene_loading(char_name,char_gender)
# Maps the player choices to the variable current_scene
current_scene = cabin_scene(char_name, char_gender)
# Returns (story_text, player_choices, next_scenes)
# Pulls display text from current scene to display to user
display_text = current_scene [0]
# Pulls returned options from current scene to be mapped to display
current_options = current_scene [1]
option_1 = current_options ["option_1"]
option_2 = current_options ["option_2"]
# Format of dictionary values
# {"option_1":first_scene,"option_2":second_scene}
next_scenes = current_scene [2]
first_scene = next_scenes ["option_1"]
second_scene = next_scenes ["option_2"]
layout = [ [sg.Text(display_text) ],
[sg.Button(option_1),sg.Button(option_2)],
[sg.Button("Cancel")] ]
# Create the Window
window = sg.Window("The Ultimate Choose Your Own Adventure Story", layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == "Cancel": # if user closes window or clicks cancel
break
window. Close()
示例函式回傳值:
story_text = f"""Insert text personalized to user here."""
player_choices = {
"option_1":"HIDE",
"option_2":"ESCAPE"}
next_scenes = {
"option_1":closet_scene,
"option_2":forest_scene}
return story_text, player_choices, next_scenes
uj5u.com熱心網友回復:
簡短的回答是,您可以通過有選擇地隱藏按鈕來獲得所需的內容,但一旦構建了視窗,就無法更改布局。“固定布局”是 PySimpleGUI 的核心要求。
您可以為游戲中的任何元素制作一個包含足夠按鈕的頁面,比如五個。每個故事元素一個,您設定一些按鈕的文本并隱藏其他按鈕。您的更新函式可能如下所示:
# choice_names is a list of strings for the current story event
for i in range(MAX_CHOICES):
if i < len(choice_names):
window[f'option_{i 1}'].update(choice_names[i], visible=True)
else:
window[f'option_{i 1}'].update(visible=False)
上面 Jason Wang 的代碼使用該visible引數隱藏“PREV”和“NEXT”按鈕,因此您可以將其用作另一個示例。這個食譜食譜有一個更復雜的例子。
繼續黑客攻擊!記筆記。
查爾斯
uj5u.com熱心網友回復:
這里的一個簡單腳本可能會有所幫助。
import PySimpleGUI as sg
layout = [
[sg.Text('Page 1/5', size=20, key='TEXT')],
[sg.pin(sg.Button('PREV', visible=False)), sg.Push(), sg.pin(sg.Button('NEXT'))],
]
window = sg.Window('Title', layout)
page = 1
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event in ('PREV', 'NEXT'):
page = 1 if event == 'NEXT' else -1
window['TEXT'].update(f'Page {page}/5')
window['PREV'].update(visible=(page!=1))
window['NEXT'].update(visible=(page!=5))
window.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521413.html
上一篇:使用將月份和日期作為輸入的for回圈回傳一年中的某一天
下一篇:在Python中查找和替換字符
