如果画布对象到达某个坐标,如何设置if语句? tkinter

发布于 2025-01-21 22:33:43 字数 1044 浏览 0 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲世九天 2025-01-28 22:33:43

我认为现在的问题还为时过早。您之前需要回答不同的问题。您的动作永远不会让您认为蛇咬伤本身。您的蛇也不会在角落里移动。因此,您很可能必须更改该功能。无论如何,向您展示了我在评论部分中提到的内容的概念。

我已经实现了一个称为after_tick()的函数。在函数的内部,我通过询问画布的属性来定义(0,0)=左上角的边界和宽度和高度的最大坐标。之后,我向画布询问此 box 的项目,如果snakebody不在内部的ID,请通过打印 game。

其他建议:

  • ,tick通常是自动化的下一步
  • 实现
  • 功能tick

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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文