Diskuze: Vytvoření tlačítek - směr pohybu
Zobrazeno 3 zpráv z 3.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
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()
Zobrazeno 3 zpráv z 3.