chooventerp/chooventerp.py

107 lines
3.4 KiB
Python
Raw Permalink Normal View History

2024-07-30 06:04:59 +00:00
# Chooventerp --- A choose-venture interpreter
# Copyright © 2020, 2021, 2024 Adam Faiz <adam.faiz@disroot.org>
#
# Chooventerp is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# Chooventerp is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Chooventerp. If not, see <http://www.gnu.org/licenses/>.
from PIL import Image
progress=[]
def prepare_story(story):
global progress
book={}
with open(story) as tale:
for line in tale:
page=line.split('|')[0]
line=line.rstrip('\n')
line=line.replace('/','\n')
book[page]=line
return book
def read_page(page,book,show_images=True):
global progress
options={}
current_page=book[page]
page_num,title,text,suspense,choices=current_page.split('|')
if choices=='END':
choices='Go back~back;Start over~restart'
choices=choices.split(';')
for choice in choices:
choice=choice.split('~')
if len(choice)==3:
option=choice[0]
beckon=choice[1]
option_page=choice[2]
options[option_page]=option+'~'+beckon
else:
option,option_page=choice
options[option_page]=option
print(title)
print('----------------------------------------------------------------------------')
text=text.split('~')
for t in range(len(text)):
print(text[t])
if show_images:
try:
if t==0:
img=Image.open('img/'+page+'.jpg')
else:
img=Image.open('img/'+page+'('+str(t)+').jpg')
answer=input('Show Image?(y/n)')
if answer=='y':
img.show()
except IOError:
continue
print('\n'+suspense+'\n')
option_page=''
for option_page in options:
beckon='Go to Page'
if len(options[option_page].split('~'))==2:
option,beckon=options[option_page].split('~')
options[option_page]=option
print(options[option_page]+':'+' '+beckon+' '+option_page)
next_page=''
while (next_page not in options) and (next_page.lower() not in ['back','restart']):
next_page=input()
next_page=next_page.lower()
if next_page=='back':
if len(progress)<1:
read_page('i',book)
else:
back_page=progress[len(progress)-1]
del progress[len(progress)-1]
read_page(back_page,book)
elif next_page=='restart':
read_page('i',book)
else:
progress.append(page)
read_page(next_page,book)
def start_story():
book=prepare_story('story.txt')
read_page('i',book)
answer=input('Would you like a message before we start this story?(y/n)')
if answer=='y':
try:
with open("message.txt") as message:
for line in message:
line=line.rstrip('\n')
print(line)
input()
except IOError:
print('message.txt not found, skipping')
start_story()