Home > Net >  How to set an if statement if a canvas object reaches a certain coordinate? tkinter
How to set an if statement if a canvas object reaches a certain coordinate? tkinter

Time:04-19

I'm making a snake game with tkinter and I'm trying to print out a 'game over' message if the snake touches the window. How do I do this?

Here's what I've gotten so far:

from tkinter import *
# import random, disregard random module, unused

def up(event):
    canvas.move(snakebody, 0, -20)

def down(event):
    canvas.move(snakebody, 0, 20)

def left(event):
    canvas.move(snakebody, -20, 0)

def right(event):
    
    canvas.move(snakebody, 20, 0)

# root
window = Tk()

window.config(bg='darkgreen')
window.geometry('450x450')
window.resizable(0, 0)

# canvas
canvas = Canvas(window, bg='darkgreen', borderwidth=0)
canvas.pack(fill=BOTH, expand=1)

# snake body/head
snakebody = canvas.create_rectangle(100, 50, 50, 100, outline='lightgreen', fill='lightgreen')

# game over message
if canvas.coords(snakebody) == canvas.coords(snakebody, 50, 50, 100, 100):
    print('game over')

# control binds
window.bind('<Up>', up)
window.bind('<Down>', down)
window.bind('<Left>', left)
window.bind('<Right>', right)

window.mainloop()

please help

CodePudding user response:

I think it's too early for your question. You will need to answer different questions before. Your movement never allows you that the snake bites itself. Nor will your snake move around a corner. Because of that it's most likely that you have to change that function. Anyway to show you the concept of what I mentioned in the comment section.

I have implemented a function that is called after_tick(). Inside of the function I defined the boundery from (0,0) = upper left corner and the maximum coord in width and height by asking the canvas for its property. After that I ask the canvas for the items inside of this box and if the id of snakebody is not inside, print game over.

Additional suggestions:

  • implement a function tick, a tick is usually the next step in a automation
  • use multiple rectangles for your snake and use tags for the head and the body
  • dont use wildcard imports to avoid name conflicts

from tkinter import *

def after_tick():
    max_w = canvas.winfo_width()
    max_h = canvas.winfo_height()
    in_cnvs = canvas.find_enclosed(0,0,max_w,max_h)
    if snakebody not in in_cnvs:
        print('game over')    

def move_event(event):
    key = str(event.keysym)
    if key == 'Up':
        canvas.move(snakebody, 0, -20)
    elif key == 'Down':
        canvas.move(snakebody, 0, 20)
    elif key == 'Left':
        canvas.move(snakebody, -20, 0)
    elif key == 'Right':
        canvas.move(snakebody, 20, 0)
    after_tick()


window = Tk()
window.config(bg='darkgreen')
window.geometry('450x450')
window.resizable(0, 0)

canvas = Canvas(window, bg='darkgreen', borderwidth=0)
canvas.pack(fill=BOTH, expand=1)

snakebody = canvas.create_rectangle(100, 50, 50, 100,
                                    outline='lightgreen',
                                    fill='lightgreen')

window.bind('<Up>', move_event)
window.bind('<Down>', move_event)
window.bind('<Left>', move_event)
window.bind('<Right>', move_event)

window.mainloop()
  • Related