我的播放器如何与画布边框碰撞?

时间:2019-04-12 08:12:08

标签: python tkinter

播放器应与画布的边框碰撞,但我不知道该怎么做。

from tkinter import *
import time

窗口

tk = Tk()
tk.title("Game")
tk.geometry("500x500")
tk.wm_attributes("-topmost", 1)
tk.resizable(0, 0)

画布

canvas = Canvas(tk, width=500, height=500, bg="#FFD700")
canvas.pack()
canvasx = canvas.winfo_width()
canvasy = canvas.winfo_height()

player = canvas.create_rectangle(240, 240, 260, 260, fill="blue", 
outline="dark blue")

播放器类

class Player:
    def __init__(self, event, canvas):
        def move(event):
            if event.keysym == "Up" or event.keysym == "w":
                canvas.move(1, 0, -5)
            elif event.keysym == "Down" or event.keysym == "s":
                canvas.move(1, 0, 5)
            elif event.keysym == "Right" or event.keysym == "d":
                canvas.move(1, 5, 0)
            else:
                canvas.move(1, -5, 0)

该功能我应该写些什么?

        def hit_border():
            pass            

        canvas.bind_all("<KeyPress-Up>", move)
        canvas.bind_all("<KeyPress-w>", move)
        canvas.bind_all("<KeyPress-Down>", move)
        canvas.bind_all("<KeyPress-s>", move)
        canvas.bind_all("<KeyPress-Right>", move)
        canvas.bind_all("<KeyPress-d>", move)
        canvas.bind_all("<KeyPress-Left>", move)
        canvas.bind_all("<KeyPress-a>", move)

这是使一切正常的循环

while True:
    player = Player(canvas, canvas)
    tk.update()
    time.sleep(0.001)

0 个答案:

没有答案