Игрок должен столкнуться с границей холста, но я не знаю, как это сделать.
from tkinter import *
import time
окно
tk = Tk()
tk.title("Game")
tk.geometry("500x500")
tk.wm_attributes("-topmost", 1)
tk.resizable(0, 0)
холст
canvas = Canvas(tk, width=500, height=500, bg="#FFD700")
canvas.pack()
canvasx = canvas.winfo_width()
canvasy = canvas.winfo_height()
player = canvas.create_rectangle(240, 240, 260, 260, fill="blue",
outline="dark blue")
Игрок класса
class Player:
def __init__(self, event, canvas):
def move(event):
if event.keysym == "Up" or event.keysym == "w":
canvas.move(1, 0, -5)
elif event.keysym == "Down" or event.keysym == "s":
canvas.move(1, 0, 5)
elif event.keysym == "Right" or event.keysym == "d":
canvas.move(1, 5, 0)
else:
canvas.move(1, -5, 0)
что мне написать в этой функции?
def hit_border():
pass
canvas.bind_all("<KeyPress-Up>", move)
canvas.bind_all("<KeyPress-w>", move)
canvas.bind_all("<KeyPress-Down>", move)
canvas.bind_all("<KeyPress-s>", move)
canvas.bind_all("<KeyPress-Right>", move)
canvas.bind_all("<KeyPress-d>", move)
canvas.bind_all("<KeyPress-Left>", move)
canvas.bind_all("<KeyPress-a>", move)
вот цикл, который заставляет все работать
while True:
player = Player(canvas, canvas)
tk.update()
time.sleep(0.001)