如果画布对象到达某个坐标,如何设置if语句? tkinter
我正在使用TKINTER制作蛇游戏,如果蛇碰到窗口,我正在尝试打印出“ Over Over”消息。我该怎么做?
这是我到目前为止得到的:
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()
请帮忙
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为现在的问题还为时过早。您之前需要回答不同的问题。您的动作永远不会让您认为蛇咬伤本身。您的蛇也不会在角落里移动。因此,您很可能必须更改该功能。无论如何,向您展示了我在评论部分中提到的内容的概念。
我已经实现了一个称为
after_tick()
的函数。在函数的内部,我通过询问画布的属性来定义(0,0)=左上角的边界和宽度和高度的最大坐标。之后,我向画布询问此 box 的项目,如果snakebody
不在内部的ID,请通过打印 game。其他建议:
tick
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 ofsnakebody
is not inside, print game over.Additional suggestions:
tick
, a tick is usually the next step in a automation