NOVINKA - Online rekvalifikační kurz Python programátor. Oblíbená a studenty ověřená rekvalifikace - nyní i online.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Diskuze: Vytvoření tlačítek - směr pohybu

Aktivity
Avatar
Pavel Adamus
Člen
Avatar
Pavel Adamus:22.1.2022 8:47

Ahoj, jsem úplný začátečník a chtěl jsem naprogramovat ovladač pro ukazatele směru. Už mi to tak nějakou dobu leží a nedokážu to zprovoznit. Je to skoro funkční jen to nedělá ten výpočet pohybu.

Řádek 9-11 je definice, která udává číslo podle toho kterou jsem zmáčkl šipku. Ty šipky jsou napsané na řádku 45-97 a čísla které to má hlásit jsou v "command". Problém je to, že to číslo které generuje ta definice potřebuju nějak dostat do "pohyb". To je ten řádek 13 , ale takhle to nefuguje. To pak má navazovat na ten řádek 106-132 , kdy to podle toho vygenerovaného čísla udělá pohyb x/y.

Kdyby někdo věděl jak na to, tak bych mu byl moc vděčný. Díky i za snahu.

import tkinter
from tkinter import ttk
import sys
from typing import MutableMapping, NoReturn

def exit():
    sys.exit(0)

def number(n):
    lambda: str(n)
    print(n) #test čísel

pohyb = number

x = str(0)
y = str(0)
v = str(0)
s = str(0)
h = str(0)
w = str(0)

# ("1 = ↙")
# ("2 = ↓")
# ("3 = ↘")
# ("4 = ←")
# ("5 = ∙")
# ("6 = →")
# ("7 = ↖")
# ("8 = ↑")
# ("9 = ↗")


while not (int(h) == 36 and int(w) == 36): #hodnoty značí cílovou čáru, musí dosáhnout obou hodnot
    root = tkinter.Tk()
    root.title("Pohyb")

    canvas = tkinter.Canvas(root, width=300, height=320, background='white')
    canvas.pack()

    nested_window = tkinter.Frame(relief=tkinter.RAISED)
    canvas.create_window(150, 10, width=310, height=20, window=nested_window)
    label = tkinter.Label(nested_window, text="Zvol směr")
    label.pack()

    nested_window = tkinter.Frame(relief=tkinter.RAISED)
    canvas.create_window(50, 70, window=nested_window)
    button1 = tkinter.Button(nested_window, text="↖", command=lambda:number (str(7)))
    button1.pack()
    button1.config( height = 5, width = 10 )

    nested_window = tkinter.Frame(relief=tkinter.RAISED)
    canvas.create_window(150, 70, window=nested_window)
    button2 = tkinter.Button(nested_window, text="↑", command=lambda:number (str(8)))
    button2.pack()
    button2.config( height = 5, width = 10 )

    nested_window = tkinter.Frame(relief=tkinter.RAISED)
    canvas.create_window(250, 70, window=nested_window)
    button3 = tkinter.Button(nested_window, text="↗", command=lambda:number (str(9)))
    button3.pack()
    button3.config( height = 5, width = 10 )

    nested_window = tkinter.Frame(relief=tkinter.RAISED)
    canvas.create_window(50, 170, window=nested_window)
    button4 = tkinter.Button(nested_window, text="←", command=lambda:number (str(4)))
    button4.pack()
    button4.config( height = 5, width = 10 )

    nested_window = tkinter.Frame(relief=tkinter.RAISED)
    canvas.create_window(150, 170, window=nested_window)
    button5 = tkinter.Button(nested_window, text="∙", command=lambda:number (str(5)))
    button5.pack()
    button5.config( height = 5, width = 10 )

    nested_window = tkinter.Frame(relief=tkinter.RAISED)
    canvas.create_window(250, 170, window=nested_window)
    button6 = tkinter.Button(nested_window, text="→", command=lambda:number (str(6)))
    button6.pack()
    button6.config( height = 5, width = 10 )

    nested_window = tkinter.Frame(relief=tkinter.RAISED)
    canvas.create_window(50, 270, window=nested_window)
    button7 = tkinter.Button(nested_window, text="↙", command=lambda:number (str(1)))
    button7.pack()
    button7.config( height = 5, width = 10 )

    nested_window = tkinter.Frame(relief=tkinter.RAISED)
    canvas.create_window(150, 270, window=nested_window)
    button8 = tkinter.Button(nested_window, text="↓", command=lambda:number (str(2)))
    button8.pack()
    button8.config( height = 5, width = 10 )

    nested_window = tkinter.Frame(relief=tkinter.RAISED)
    canvas.create_window(250, 270, window=nested_window)
    button9 = tkinter.Button(nested_window, text="↘", command=lambda:number (str(3)))
    button9.pack()
    button9.config( height = 5, width = 10 )

    nested_window = tkinter.Frame(relief=tkinter.RAISED)
    canvas.create_window(0, 0, window=nested_window)
    button10 = tkinter.Button(root, text="Zavřít hru", command=exit)
    button10.pack()

    root.mainloop()

    if pohyb == 1:
        x = -1
        y = -1
    if pohyb == 2:
        x = -1
        y = 0
    if pohyb == 3:
        x = -1
        y = +1
    if pohyb == 4:
        x = 0
        y = -1
    if pohyb == 5:
        x = 0
        y = 0
    if pohyb == 6:
        x = 0
        y = +1
    if pohyb == 7:
        x = +1
        y = -1
    if pohyb == 8:
        x = +1
        y = 0
    if pohyb == 9:
        x = +1
        y = +1

    v = int(v) + int(x)
    s = int(s) + int(y)
    print (str(v) + " Pohyb Výška")   #pohyb po mapě; nový směr + setrvačnost z posledního tahu
    print (str(s) + " Pohyb Šířka")

    h = int(h) + int(v)
    w = int(w) + int(s)
    print (str(h) + " Poloha Výška")   #aktuální pozice na mapě
    print (str(w) + " Poloha Šířka")

print ("Dojel jsi do cíle!")

Zkusil jsem: K téhle struktuře jsem se dopracoval postupně a nevím jestli je možné to takhle zprovoznit a lámu si stím hlavu už docela dlouho.

Chci docílit: Funkčnosti.

 
Odpovědět
22.1.2022 8:47
Avatar
Pavel
Člen
Avatar
Pavel:22.1.2022 17:16

Ten pohyb nic nevolá, takže to logicky nemůže fungovat, krom toho v něm jenom měníš hodnoty x a y, nepřidáváš ani neubíráš.
Jinak number (str(7)) je rovnák na ohýbák.

import tkinter
import sys


class Car:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.v = 0
        self.s = 0
        self.h = 0
        self.w = 0

    def make_move(self, n):
        if self.check_position():
            if n == 1:
                self.x += -1
                self.y += -1
            if n == 2:
                self.x += -1
                self.y += 0
            if n == 3:
                self.x += -1
                self.y += 1
            if n == 4:
                self.x += 0
                self.y += -1
            if n == 5:
                self.x += 0
                self.y += 0
            if n == 6:
                self.x += 0
                self.y += 1
            if n == 7:
                self.x += 1
                self.y += -1
            if n == 8:
                self.x += 1
                self.y += 0
            if n == 9:
                self.x += 1
                self.y += 1

            self.v += self.x
            self.s += self.y


            self.h += self.v
            self.w += self.s
            print(f"pohyb V: {self.v}\npohyb Š: {self.s}\npoloha V: {self.h}\npoloha Š: {self.w}\n")

            return True

        print('Dojel jsi do cíle!')
        return False


    def check_position(self):
        if self.h >= 36 and self.w >= 36:
            return False
        return True


class Game():
    '''
        ("1 = ↙")
        ("2 = ↓")
        ("3 = ↘")
        ("4 = ←")
        ("5 = ∙")
        ("6 = →")
        ("7 = ↖")
        ("8 = ↑")
        ("9 = ↗")
    '''
    B_W = 10
    B_H = 5

    def __init__(self):
        self.root = tkinter.Tk()
        self.car = Car()
        self.root.title("Pohyb")

        self.label = tkinter.Label(self.root, text="Zvol směr")
        self.label.pack()


        # Top row of buttons
        top_row = tkinter.Frame(self.root, relief=tkinter.RAISED)
        top_row.pack(side=tkinter.TOP)

        self.button1 = tkinter.Button(top_row, height=self.B_H, width=self.B_W, text="↖", command=lambda: self.car.make_move(7) or self.deactivate_buttons())
        self.button1.pack(side=tkinter.LEFT)

        self.button2 = tkinter.Button(top_row, height=self.B_H, width=self.B_W, text="↑", command=lambda: self.car.make_move(8) or self.deactivate_buttons())
        self.button2.pack(side=tkinter.LEFT)

        self.button3 = tkinter.Button(top_row, height=self.B_H, width=self.B_W, text="↗", command=lambda: self.car.make_move(9) or self.deactivate_buttons())
        self.button3.pack(side=tkinter.LEFT)

        # Middle row of buttons
        middle_row = tkinter.Frame(self.root, relief=tkinter.RAISED)
        middle_row.pack(side=tkinter.TOP)

        self.button4 = tkinter.Button(middle_row, height=self.B_H, width=self.B_W, text="←", command=lambda: self.car.make_move(4) or self.deactivate_buttons())
        self.button4.pack(side=tkinter.LEFT)

        self.button5 = tkinter.Button(middle_row, height=self.B_H, width=self.B_W, text="∙", command=lambda: self.car.make_move(5) or self.deactivate_buttons())
        self.button5.pack(side=tkinter.LEFT)

        self.button6 = tkinter.Button(middle_row, height=self.B_H, width=self.B_W, text="→", command=lambda: self.car.make_move(6) or self.deactivate_buttons())
        self.button6.pack(side=tkinter.LEFT)

        # Bottom row of buttons
        bottom_row = tkinter.Frame(self.root, relief=tkinter.RAISED)
        bottom_row.pack(side=tkinter.TOP)

        self.button7 = tkinter.Button(bottom_row, height=self.B_H, width=self.B_W, text="↙", command=lambda: self.car.make_move(1) or self.deactivate_buttons())
        self.button7.pack(side=tkinter.LEFT)

        self.button8 = tkinter.Button(bottom_row, height=self.B_H, width=self.B_W, text="↓", command=lambda: self.car.make_move(2) or self.deactivate_buttons())
        self.button8.pack(side=tkinter.LEFT)

        self.button9 = tkinter.Button(bottom_row, height=self.B_H, width=self.B_W, text="↘", command=lambda: self.car.make_move(3) or self.deactivate_buttons())
        self.button9.pack(side=tkinter.LEFT)

        # Exit button
        self.button10 = tkinter.Button(self.root, text="Zavřít hru", command=self.exit)
        self.button10.pack(side=tkinter.BOTTOM)

        self.root.mainloop()

    def deactivate_buttons(self):
        self.button1["state"] = tkinter.DISABLED
        self.button2["state"] = tkinter.DISABLED
        self.button3["state"] = tkinter.DISABLED
        self.button4["state"] = tkinter.DISABLED
        self.button5["state"] = tkinter.DISABLED
        self.button6["state"] = tkinter.DISABLED
        self.button7["state"] = tkinter.DISABLED
        self.button8["state"] = tkinter.DISABLED
        self.button9["state"] = tkinter.DISABLED

    def exit(self):
        self.root.destroy()
        sys.exit(0)


game = Game()
 
Nahoru Odpovědět
22.1.2022 17:16
Avatar
Pavel Adamus
Člen
Avatar
Odpovídá na Pavel
Pavel Adamus:22.1.2022 20:26

Super, díky za pomoc! 🤝

 
Nahoru Odpovědět
22.1.2022 20:26
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 3 zpráv z 3.