How do I add collision detection to the sides of the window in Tkinter? [Python 3]
<p>I'm making an (admittedly, my first) game in Python, and I've only really made the base of the game (like, moving around), and everything works <em>pretty</em> well, except for the fact that you can move right off the screen.
How would I add collision detection to the sides of the window?
Here's the code:</p>

<pre><code>from tkinter import *
import time
tk = Tk()
tk.title("Squarey")
c = colorchooser.askcolor(title='Choose Squarey\'s Color')
canvas = Canvas(tk, width=700, height=700)
canvas.pack()
class Squarey:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(10, 10, 50, 50, fill=color)
    def draw(self, pos):
        def move(event):
            if event.keysym == 'Up':
                self.canvas.move(self.id, 0, -5)
            elif event.keysym == 'Down':
                self.canvas.move(self.id, 0, 5)
            elif event.keysym == 'Left':
                self.canvas.move(self.id, -5, 0)
            else:
                self.canvas.move(self.id, 5, 0)
        canvas.bind_all('&lt;KeyPress-Up&gt;', move)
        canvas.bind_all('&lt;KeyPress-Down&gt;', move)
        canvas.bind_all('&lt;KeyPress-Left&gt;', move)
        canvas.bind_all('&lt;KeyPress-Right&gt;', move)
class Tele:
    def __init__(self, canvas, pos):
        self.canvas = canvas
        self.id = canvas.create_text(10, 10, text=pos)
    def draw(self, pos):
        canvas.delete(self.id)
        self.id = canvas.create_text(350, 350, text=pos)
squarey = Squarey(canvas, c[1])
pos = squarey.canvas.coords(squarey.id)
tele = Tele(canvas, pos)
while 1:
    pos = squarey.canvas.coords(squarey.id)
    squarey.draw(pos)
    tele.draw(pos)
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)
</code></pre>

<p>Hope everyone can help me out; thanks!</p>

<p>You can get the size of your canvas like this:</p>

<pre><code>size, _ = self.canvas.winfo_geometry().split('+', maxsplit=1)
w, h = (int(_) for _ in size.split('x'))
</code></pre>

<p>And the position of your <code>Squarey</code> like this:</p>

<pre><code>x, y, _, __ = self.canvas.coords(self.id)
</code></pre>

<p>(There may be better ways to do this, of course)</p>

<p>Then just adapt your movement function like this:</p>

<pre><code>if event.keysym == 'Up':
    if y &gt; 0:
        self.canvas.move(self.id, 0, -5)
elif event.keysym == 'Down':
    if y+50 &lt; h:
        self.canvas.move(self.id, 0, 5)
elif event.keysym == 'Left':
    if x &gt; 0:
        self.canvas.move(self.id, -5, 0)
else:
    if x+50 &lt; w:
        self.canvas.move(self.id, 5, 0)
</code></pre>

<p>That should work for you (at least it does for me). But you shouldn't stop here, there are some improvements that you can make.</p>

<p>The first one that I would do is something like this:</p>

<pre><code>def __init__(self, canvas, color, width=50, height=50):
    self.canvas = canvas
    self.width = width
    self.height = height
    self.id = canvas.create_rectangle(10, 10, width, height, fill=color)
</code></pre>

<p>Then you could change your move:</p>

<pre><code>left_edge = x
right_edge = left_edge + self.width
top_edge = y
bottom_edge = top_edge + self.height

if event.keysym == 'Up' and top_edge &gt; 0:
    ...
elif event.keysym == 'Down' and bottom_edge &lt; h:
    ...
elif event.keysym == 'Left' and left_edge &gt; 0:
    ...
elif event.keysym == 'Right' and right_edge &lt; w:
    ...
</code></pre>

python tkinter collision-detection python-3.4 tkinter-canvas