繁体   English   中英

为什么我会在我的代码中找到我的移动球命令的跟踪,因为它正在阻止我的其余代码运行

[英]why am i getting a trace back in my code for my move ball command as it is stopping the rest of my code from running

我正在做一个乒乓球游戏

我设法将球编码为在窗口周围反弹,但似乎用于使用用户输入使矩形移动的代码不起作用。 当我运行代码时,我一直在回溯到与使球反弹有关的 move.ball 命令,并且我相信这可能会阻止其余代码运行。 我已经包含了完整的代码,以防在其中发现任何错误。

from tkinter import *
import tkinter as tkr
import time
tk = tkr.Tk()
Canvas = tkr.Canvas(tk, width=300, height=400)
Canvas.grid()
ball = Canvas.create_oval(3,3,40,40,fill="light blue")
player1 = Canvas.create_rectangle(20,5,90,30,fill="black")
Canvas.moveto(player1, 120, 380)
player2 = Canvas.create_rectangle(20,5,90,30,fill="black")
Canvas.moveto(player2, 120, -5)
x = 1
y = 3
while True:
    Canvas.move(ball,x,y)
    pos = Canvas.coords(ball)
if pos[3] >= 400 or pos[1] <= 0:
    y = -y
if pos[2] >= 300 or pos[0] <= 0:
    x = -x
tk.update()
time.sleep(0.025)
pass

tk.mainloop()

def left(event):
    x == -10
    y == 0
Canvas.move(player1, x, y)

def right(event):
    x == 10
    y == 0
Canvas.move(player1, x, y)

def up(event):
    x == 0
    y == -10
Canvas.move(player1, x, y)

def down(event):
    x == -10
    y == 0
Canvas.move(player1, x, y)

root.bind("<Left>", left)
root.bind("<Right>", right)
root.bind("<Up>", up)
root.bind("<Down>", down)



tk.mainloop()

我得到了这个跟踪 -

Traceback (most recent call last):
  File "C:/Users/amarb/Desktop/code/pong.py", line 15, in <module>
    Canvas.move(ball,x,y)
  File "C:\Users\amarb\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2949, in move
    self.tk.call((self._w, 'move') + args)
_tkinter.TclError: invalid command name ".!canvas"

.!canvas是您创建的画布小部件的内部标识符。 发生此错误是因为您尝试在mainloop()返回之后运行代码,也就是在窗口被销毁之后。 小部件被销毁后,您将无法与它们进行交互。

编辑:新。 如何让桨移动并击中桨。 我一次只使用一个播放器。 你会做剩下的。 我不得不修改一些并添加一些新功能。 我正在使用新的 python 3.11.0b3。

from tkinter import *
import tkinter as tkr
import time
import random


tk = tkr.Tk()
width=300
height=400
Canvas = tkr.Canvas(tk, width=width, height=height)
Canvas.pack()
tk.update()

ball = Canvas.create_oval(3,3,25,25,fill="red")
Canvas.move(ball, 245, 100)

starts = [-3, -2, -1, 1, 2, 3]
random.shuffle(starts)

ball_x = starts[0]
ball_y = 3

player1 = Canvas.create_rectangle(20,5,90,30,fill="black")
Canvas.moveto(player1, 180, 380)

player2 = Canvas.create_rectangle(20,5,90,30,fill="blue")
Canvas.move(player2, 120, -5)

paddle_x = 0


def hit_bat(pos):
    bat_pos = Canvas.coords(player1) #retrieve the coordinates of the bat position - note the ball coordinates are being passed
    if pos[2] >= bat_pos[0] and pos[0] <= bat_pos[2]: #if the right side of the ball (that is the x right hand coordinate) is greater than the left side of the bat, AND the left side of the ball is less than the right side of the bat ....move etc
        if pos[3]>= bat_pos[1] and pos[3] <= bat_pos[3]: #if the bottom of the ball (pos[3]) is between the paddle's top [bat pos[1]) and bottom (pos[3])
            return True
    return False

    
def draw_ball():
    global ball_x, ball_y
    Canvas.move(ball, ball_x, ball_y)
    pos = Canvas.coords(ball)
    if pos[1] <= 0: 
            ball_y = 6
    if pos[3] >= height:
        ball_y = -6
    #Call the hit_bat function
    if hit_bat(pos) == True: #if the hit_bat function returns a value of True, the direction of the ball is changed
        ball_y = -6 #if the ball hits the bat, then send the ball flying up at a rate of -6 (higher the number the faster the fly!)
    if pos[0] <= 0:
        ball_x = 6
    if pos[2]>= width:
        ball_x = -6         
    tk.update()
     
    
def hit_paddle(pos):
    paddle_pos = Canvas.coords(ball)
    if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
        if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
            return True
    return False


def draw_paddle():
    global paddle_x
    Canvas.move(player1, paddle_x, 0)
    pos = Canvas.coords(player1)
    if pos[0] <= 0:
        paddle_x = 0
    elif pos[2] >= width:
        paddle_x = 0
    time.sleep(0.02)
    tk.update()


def move_left(event):
    global paddle_x
    paddle_x = -2

    
def move_right(event):
    global paddle_x
    paddle_x = 2
 

Canvas.bind_all("<KeyPress-Left>", move_left)
Canvas.bind_all("<KeyPress-Right>", move_right) 

while True:
 
    draw_ball()    
    draw_paddle()
 
 

新图片:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM