0

Here is my code simply i want to shoot my ball to direction that i picked from spinbox. I aim with arrow and hit ball with the button Kresli. I will be very grateful if someone can help me with it because there is nothing on internet in this tkinter python graphic.

import tkinter
import math
from tkinter import ttk
class Simulacia(tkinter.Tk):
    def __init__(self):
     '''Nastavenie počiatočných vlastnosti a spúšťanie ďalších metód triedy'''
     super().__init__()
     self.title('Rovnomerný priamočiary pohyb')
     self.vytvorGUI()
     self.mainloop()
     self.posun_lopty()
     self.rotate()
    def vytvorGUI(self):
     '''Vytvorenie grafického rozhrania programu (GUI)'''
     self.uhol = tkinter.IntVar()
     tkinter.Button(self, text='Kresli', command=self.posun_lopty).grid(row=1, column=0)
     tkinter.Button(self, text='Ukonči', command=self.destroy).grid(row=1, column=3)
     tkinter.Button(self, text='Vymaž pozadie', command=self.vytvorGUI).grid(row=2, column=3)
     tkinter.Label(self, text = "Uhol=", width = 10).grid(row =2, column =0)
     self.platno = tkinter.Canvas(self, width=800, height=600, background='green')
     self.x = 150
     self.y= 300
     self.lopta = self.platno.create_oval(self.x, self.y, self.x+ 20, self.y + 20, fill="white")
     self.sipka = self.platno.create_line(160, 310, 280, 310, tags=("minute",), arrow="last")
     self.radius = 30
     self.platno.create_rectangle(3, 3, 800, 600, outline="brown", width=10)
     self.platno.create_oval(-self.radius, -self.radius, +self.radius, +self.radius, fill="black")  # VLAVO HORe
     self.platno.create_oval(-self.radius, 600 - self.radius, +self.radius, 600 + self.radius, fill="black")  # VLAVO DOLE
     self.platno.create_oval(800 - self.radius, 0 - self.radius, 800 + self.radius, 0 + self.radius, fill="black")  # Vpravo HORE
     self.platno.create_oval(800 - self.radius, 600 - self.radius, 800 + self.radius, 600 + self.radius, fill="black")  # Vpravo Dole
     self.platno.create_oval(400 - self.radius, -self.radius, 400 + self.radius, self.radius, fill="black")  # Hore
     self.platno.create_oval(400 - self.radius, 600 - self.radius, 400 + self.radius, 600 + self.radius, fill="black")  # Dole
     self.sb_minute = ttk.Spinbox(self, from_=0, to=60, increment=1, width=4, command=self.rotate)
     self.sb_minute.grid(row=2, column=1)
     self.sb_minute.set(15)
     self.platno.grid(row=3, column=0, columnspan=4)
     self.dx = 5
     self.dy = 0
    def rotate(self):
        self.size = 300
        # get the spinbox value
        self.m = int(self.sb_minute.get())
        # set the line coordinates for minute hand
        # moves 6 degrees per minute (360/60)
        self.degrees = self.m * 6
        # angle needs to be in radians
        self.angle = self.degrees * math.pi * 2 / 360
        # center coordinates cx, cy
        self.cx = 160
        self.cy = 310
        # find proper endpoint coordinates for the line
        self.x0 = self.cx + self.size * math.sin(self.angle) * 0.40
        self.y1 = self.cy - self.size * math.cos(self.angle) * 0.40
        self.platno.coords('minute', (self.cx, self.cy, self.x0, self.y1))


    def posun_lopty(self):
        self.x += self.dx
        self.y += self.dy
        self.platno.move(self.lopta, self.dx, self.dy)
        if self.y == 595:
            self.dx = +self.dx
            self.dy = -self.dy
        if self.y == 0:
            self.dx = self.dx
            self.dy = -self.dy
        if self.x == 0:
            self.dx = -self.dx
            self.dy = self.dy
        if self.x == 795:
            self.dx = -self.dx
            self.dy = +self.dy
        text_lopticka = "Loptička spadla do diery. Hra skončila"
        if ((self.x <= self.radius) and (self.y <= self.radius)):  # Vlavo HOre
            self.platno.create_text(400, 300, text=text_lopticka, fill="black", font=('Helvetica 15 bold'))
        if ((self.x <= self.radius) and (self.y <= 600 - self.radius)):  # VLAVO DOLE
            self.platno.create_text(400, 300, text=text_lopticka, fill="black", font=('Helvetica 15 bold'))
        if ((self.x >= 800 - self.radius) and (self.y <= self.radius)):  # Vpravo HORE
            self.platno.create_text(400, 300, text=text_lopticka, fill="black", font=('Helvetica 15 bold'))
        if ((self.x >= 800 -self.radius) and (self.y >= 600 - self.radius)):  # Vpravo Dole
            self.platno.create_text(400, 300, text=text_lopticka, fill="black", font=('Helvetica 15 bold'))
        if ((self.x <= 400 + self.radius) and (self.y <= self.radius)):  # Hore
            self.platno.create_text(400, 300, text=text_lopticka, fill="black", font=('Helvetica 15 bold'))
        if ((self.x >= 400 - self.radius) and (self.y >= 600 - self.radius)):  # Dole
            self.platno.create_text(400, 300, text=text_lopticka, fill="black", font=('Helvetica 15 bold'))
            self.platno.delete(self.lopta)
            self.platno.create_oval(400 - self.radius + 10, 600 - self.radius + 10, 400 + self.radius - 10, 600 + self.radius - 10, fill="white")
            return
        self.platno.update()
        self.platno.after(10, self.posun_lopty)
        print(self.x, self.y)

Simulacia()

I tried some get() methods from spinbox but it didnt work at all

0 Answers0