I am currently trying to make the canvas the borders for example something like:
def coords(): if pos1 <= canvas.height: tri.hit_left=true
if event.keysym == 'w' and tri.hit_left ==False: canvas.move(tri, 0, -2)
this would make it so if the triangle collides with the edge of the canvas it would not move past the edge. However I know this does not work because i have tested it out.
from tkinter import * import random import time tk = Tk() tk.title("Game") tk.resizable(0, 1) tk.wm_attributes("-topmost", 1) canvas = Canvas(tk, width=500, height=500) x3 = 5 y3 = 5 x4 = 5 y4 = 15 x5 = 13 y5 = 10 pos1 = [x3, y3] pos2 = [x4, y4] pos3 = [x5, y5] tri=canvas.create_polygon(x3, y3, x4, y4, x5, y5) tk.update() canvas.pack() def coords(): pass def move_tri(event): if event.keysym == 'w' and tri.hit_top ==False: canvas.move(tri, 0, -2) elif event.keysym == 's' and tri.hit_bottom == False: canvas.move(tri, 0, 2) elif event.keysym == 'a' and tri.hit_left == False: canvas.move(tri, -2, 0) elif event.keysym == 'd' and tri.hit_right == False: canvas.move(tri, 2, 0) canvas.bind_all('<KeyPress-w>', move_tri) canvas.bind_all('<KeyPress-a>', move_tri) canvas.bind_all('<KeyPress-s>', move_tri) canvas.bind_all('<KeyPress-d>', move_tri)
if you know how to do what i am trying to do please write suggestions, and feel free to ask for clarification.