簡體   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