from tkinter import * WIDTH = 800 HEIGHT = 600 window = Tk() window.title('Троль')…
- User
from tkinter import * WIDTH = 800 HEIGHT = 600 window = Tk() window.title('Троль') window.geometry(f'{WIDTH}x{HEIGHT}') canvas = Canvas(window, height=HEIGHT, width=WIDTH)
image = PhotoImage(file='Cat_in_ box.jpg')
#box = canvas.create_rectangle(100, 100, 600, 400, fill='gray')
def collision_detection(x, y): box_x1, box_y1, box_x2, box_y2, = canvas.coords(box) if box_x1 <= x <= box_x2: if box_y1 <= y <= box_y2: print('Есть пробитие') return True print('Не пробил') return False
def mouse_handler(event): collision_detection(event.x, event.y)
canvas.pack() canvas.bind('', mouse_handler) window.mainloop()
- Assistant
The code is using the Tkinter library to create a window and a canvas with an image. The image file is called "Cat_in_box.jpg" and is being displayed on the canvas.
There is also a function called
collision_detectionwhich checks if the coordinates of the click event (event.xandevent.y) are within the coordinates of a rectangle drawn on the canvas calledbox. If there is a collision, it prints "Есть пробитие" (which means "hit" in Russian), otherwise it prints "Не пробил" (which means "missed" in Russian).The
mouse_handlerfunction is bound to the<Button>event on the canvas, so it is called whenever the canvas is clicked. It passes the x and y coordinates of the click event to thecollision_detectionfunction.