You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter开发Pong游戏:球与Paddle碰撞失效,Paddle坐标为空

问题:Tkinter开发Pong游戏时碰撞检测失效

我在使用Tkinter开发Pong游戏时遇到了球与Paddle的碰撞检测问题。我的实现逻辑是通过比对球与Paddle的坐标,当坐标匹配时让球沿X轴改变运动方向,但调试时发现Paddle的坐标列表为空,推测这是碰撞功能失效的原因。

对象定义代码

class Ball:
    def __init__(self, canvas, x, y, diameter, xVelocity, yVelocity, color):
        self.canvas = canvas
        self.image = canvas.create_oval(x, y, diameter,diameter, fill=color)
        self.xVelocity = xVelocity
        self.yVelocity = yVelocity

    def ball_at_centre(self):
        self.canvas.move(self.image, 365, 360)
        self.canvas.update()

    def move_ball(self):

            coordinates = self.canvas.coords(self.image)
            self.canvas.move(self.image, self.xVelocity, self.yVelocity)
            print(coordinates)
            if (coordinates[2]>=(self.canvas.winfo_width())):
                self.canvas.move(self.image, -365, -300)
                self.canvas.update()
                self.canvas.move(self.image, self.xVelocity, self.yVelocity)

            if (coordinates[3]>=(self.canvas.winfo_height())):
                self.yVelocity = -3
            if coordinates[0] < 0:
                self.canvas.move(self.image, 365, 300)
                self.canvas.update()
                self.canvas.move(self.image, self.xVelocity, self.yVelocity)

            if coordinates[1] < 0:
                self.yVelocity = 3

    def move_collisions_left(self, image_paddle_one):
        coordinates = self.canvas.coords(self.image)
        coordinates1 = self.canvas.coords(image_paddle_one)
        if coordinates[0] <= 5 and coordinates1[1] < coordinates[1] < coordinates1[3]:
            self.xVelocity = 3

    def move_collisions_right(self, image_paddle_two):
        coordinates2 = self.canvas.coords(image_paddle_two)
        coordinates = self.canvas.coords(self.image)
        if coordinates[2] <= 750 and coordinates2[1] < coordinates[3] < coordinates2[3]:
            self.xVelocity = -3

class PaddleOne:
    def __init__(self, canvas, x1, x2, y1, y2, color):
        self.canvas = canvas
        self.image_paddle_one = canvas.create_rectangle(x1, x2, y1, y2, fill=color)

    def move_paddle(self, direction):
        if direction == "w":
            self.canvas.move(self.image_paddle_one, 0, -20)
        if direction == "s":
            self.canvas.move(self.image_paddle_one, 0, 20)
        print(self.canvas.coords(self.image_paddle_one))

主程序代码

from tkinter import *
from objects import *
import time


def play_game():
    global paddle_left
    global paddle_right
    global canvas

    new_window = Tk()
    window.destroy()
    canvas = Canvas(new_window, height=800, width=800,bg='black')
    canvas.pack()
    centre_ball = Ball(canvas, 10, 10, 50, 2, 3, 'white')
    paddle_left = PaddleOne(canvas, 5, 5, 50, 100, 'white')
    paddle_right = PaddleTwo(canvas, 795, 795, 750, 700, 'white')
    new_window.geometry("800x800")
    centre_ball.ball_at_centre()
    while True:
        centre_ball.move_ball()
        new_window.update()
        time.sleep(0.01)

        new_window.bind("<w>", lambda event: paddle_left.move_paddle("w"))
        new_window.bind("<s>", lambda event: paddle_left.move_paddle("s"))
        new_window.bind("<Up>", lambda event: paddle_right.move_paddle("up"))
        new_window.bind("<Down>", lambda event: paddle_right.move_paddle("down"))

        centre_ball.move_collisions_left(paddle_left)
        centre_ball.move_collisions_right(paddle_right)


    new_window.mainloop()
    canvas.update()



    new_window.geometry("800x800")


    new_window.mainloop()

def controls():
    canvas = Canvas(window, height=window.winfo_height(), width=window.winfo_width(), bg='black')
    canvas.pack()
    controls_label = Label(canvas, text="left is wd right is arrowkeys", relief='flat', bg='black', fg='white', font="Times 22 bold")
    controls_label.pack()

window = Tk()
window.geometry("800x800")
window.configure(bg='black')

play = Button(window, text="play", font="Times 22 bold", fg='white', activeforeground='white', bg='black', relief='flat', height = 5, width=25, command=play_game)
play.place(x=190, y=200)


controls = Button(window, text="controls", font="Times 22 bold", fg='white', activeforeground='white', bg='black', relief='flat', height = 5, width=25, command=controls)
controls.place(x=190, y=400)


window.mainloop()

内容的提问来源于stack exchange,提问作者bigvideostudios

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 01:55:19