Untitled

mail@pastecode.io avatar
unknown
python
8 days ago
3.7 kB
2
Indexable
Never
import json
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk

# Loadni json z file
with open('table.json', 'r', encoding='utf-8') as file:
    data = json.load(file)


# tahle funkce ti najde events co matchujou class conditions
def find_events(event_cycle, class_condition):
    events = []  # vytvoris mempty list kam budes ukladat matching eventy
    for event in data:  # loop skrz kazdej event
        if event['eventCycle'] == event_cycle and event['classCondition'] == class_condition:
            events.append(event)  # pokud match tak to prida event do listu events
    return events


# tahle funkce printuje text pro matching json event
def print_event(event_id):
    for event in data:
        if event['id'] == event_id:
            return event['text'], event['nextCycle'], event['class'], event['eventType'], event[
                'eventImg']  # a vrati nextCycle, class a type


# Load images based on eventImg
def load_image(event_img):
    try:
        return ImageTk.PhotoImage(Image.open(event_img))
    except IOError:
        return None


# user input fuinkce
def handle_user_input(events, event_type):
    # Clear previous buttons
    for widget in button_frame.winfo_children():
        widget.destroy()

    if len(events) > 1:  # ukaze text "tva odpoved" jen pokud odpovedi je vic jak 1
        tk.Label(button_frame, text="Your choices:").pack()

        for i, ev in enumerate(events):
            if ev['eventType'] == event_type:
                button = tk.Button(button_frame, text=ev['text'], command=lambda ev=ev: process_choice(ev))
                button.pack(fill='x')

    else:  # pokud je jen jedna moznost na option, answer etc tak ti ji ukaze rovnou a neceka na input
        process_choice(events[0])


def process_choice(chosen_event):
    next_cycle = chosen_event['nextCycle']
    event_text, next_cycle, next_class, next_event_type, event_img = print_event(chosen_event['id'])

    text_label.config(text=event_text)

    # Load and display the image
    img = load_image(event_img)
    if img:
        image_label.config(image=img)
        image_label.image = img
    else:
        image_label.config(image='')

    if next_cycle in ['Option', 'Text', 'Start', 'End']:
        next_events = find_events(next_cycle, next_class)
        if next_events:
            handle_user_input(next_events, next_event_type)
        else:
            messagebox.showinfo("Info", f"No valid {next_cycle} events found")

        if next_cycle == 'gameOver':
            messagebox.showinfo("Game Over", "The game has ended.")
            root.destroy()


# tkinter na simple gui
root = tk.Tk()
root.title("Text-based Game")
root.geometry("1280x720")  # okno size


text_frame = tk.Frame(root)
text_frame.pack(pady=50)
text_label = tk.Label(text_frame, text="", wraplength=1200)
text_label.pack()
image_label = tk.Label(text_frame)
image_label.pack()
button_frame = tk.Frame(root)
button_frame.pack(pady=50)

# Najdi id1 a printni text = zacatek hry na id1
for event in data:
    if event['id'] == 1:
        text_label.config(text=event['text'])
        img = load_image(event['eventImg'])
        if img:
            image_label.config(image=img)
            image_label.image = img

        if event['nextCycle'] in ['Option', 'Start', 'End']:
            events = find_events(event['nextCycle'], event['class'])
            if events:
                handle_user_input(events, event['eventType'])
            else:
                messagebox.showinfo("Info", f"No valid {event['nextCycle']} events found")
        break

root.mainloop()
Leave a Comment