Picture and Button based RPG in canvas, NOT TEXT

Viewed 42

My goal: is to make a choice-based story so far I only made the first 2 pictures which would be the main picture and 2 choices which lead to another picture with choices, like an RPG with choices.

so far everything is alright: I used buttons, tkinter.PhotoImage. But it shows 4 buttons from the start

My question is: How to implement choices or some sort of stuff so the first picture will have 2 buttons, and it will lead to another picture with 4choices=4buttons or something

Here is my code to the end, note it is in hungarian

from random import *
canvas=tkinter.Canvas(height=500, width=500)
canvas.pack()

#first picture
elso=tkinter.PhotoImage(file='first.png')
#second picture is first choice from the first picture
masodik=tkinter.PhotoImage(file='second.png')
#third picture is second choice from the first picture
harmadik=tkinter.PhotoImage(file='third.png')
def square():
    canvas.create_rectangle(200, 400, 300, 500)

def secondsquare():
    canvas.create_rectangle(200, 300, 300, 400)
    
def button1_klik():
    canvas.create_image(250, 200, image=elso)

def button2_klik():
    canvas.create_image(250, 200, image=masodik)

def button3_klik():
    canvas.create_image(250, 200, image=harmadik)
def button4_klik():
    canvas.create_rectangle(250, 500, 500, 800, fill='blue')

    

button1 = tkinter.Button(text='1válasz', command=button1_klik)
button1.pack()

button2=tkinter.Button(text='2válasz', command=button2_klik)
button2.pack()

button3=tkinter.Button(text='3valasz', command=button3_klik)
button3.pack()

button4=tkinter.Button(text='4valasz', command=button4_klik)
button4.pack()
    

1 Answers

You can create a State class and define the choices for each image. The class creates the buttons and when you press it you update the state.

from random import *
import tkinter

class State:
    def __init__(self, image):
        self.image = image
        self.choices = image['children']
        self.buttons = []
        canvas.create_image(250, 200, image=self.image['element'])
        for i in self.choices:
            button = tkinter.Button(text=i['name'], command=lambda i=i: button_klik(i))
            self.buttons.append(button)
            button.pack()

    def update(self, image):
        for b in self.buttons:
            b.destroy()
        self.image = image
        self.choices = image['children']
        self.buttons = []
        canvas.delete("all")
        canvas.create_image(250, 200, image=self.image['element'])
        for i in self.choices:
            button = tkinter.Button(text=i['name'], command=lambda i=i: button_klik(i))
            self.buttons.append(button)
            button.pack()
        
top = tkinter.Tk()
canvas=tkinter.Canvas(height=500, width=500)
canvas.pack()

image1 = {"name": "elso", "element": tkinter.PhotoImage(file='first.png')}
image2 = {"name": "masodik", "element": tkinter.PhotoImage(file='second.png')}
image3 = {"name": "harmadik", "element": tkinter.PhotoImage(file='third.png')}

image1['children'] = [image2, image3]
image2['children'] = [image1, image3]
image3['children'] = [image1, image2]

state = State(image1)
    
def button_klik(choice):
        state.update(choice)


top.mainloop()
Related