Bounceゲームというプログラムを書いて、バウンドするボールと静止した長方形のラケットを表示させることができましたが、しばらくするとエラーが表示をされます。エラーを解消するにはどうしたらいいのか分かりません。
python3
1コードfrom tkinter import* 2import random 3import time 4 5class Ball: 6 def __init__(self,canvas,color): 7 self.canvas=canvas 8 self.id=canvas.create_oval(10,10,25,25,fill=color) 9 self.canvas.move(self.id,245,100) 10 starts=[-3,-2,-1,1,2,3] 11 random.shuffle(starts) 12 self.x=starts[0] 13 self.y=-3 14 self.canvas_height=self.canvas.winfo_height() 15 self.canvas_width=self.canvas.winfo_width() 16 17 18 19 def draw(self): 20 self.canvas.move(self.id, self.x, self.y) 21 pos=self.canvas.coords(self.id) 22 if pos[1]<=0: 23 self.y=3 24 if pos[3]>=self.canvas_height: 25 self.y=-3 26 if pos[0]<=0: 27 self.x=3 28 if pos[2]>=self.canvas_width: 29 self.x=-3 30 31class Paddle: 32 def __init__(self,canvas,color): 33 self.canvas=canvas 34 self.id=canvas.create_rectangle(0,0,100,10,fill=color) 35 self.canvas.move(self.id,200,300) 36 def draw(self): 37 pass 38 39 40tk=Tk() 41tk.title("Game") 42tk.resizable(0,0) 43tk.wm_attributes("-topmost",1) 44canvas=Canvas(tk,width=500,height=400,bd=0,highlightthickness=0) 45canvas.pack() 46tk.update() 47 48paddle=Paddle(canvas,'blue') 49ball=Ball(canvas, 'red') 50 51while True: 52 ball.draw() 53 paddle.draw() 54 tk.update_idletasks() 55 tk.update() 56 time.sleep(0.01)
エラー文
Traceback (most recent call last):
File "C:\Users\t\Desktop\paddleball.py", line 52, in <module>
ball.draw()
File "C:\Users\t\Desktop\paddleball.py", line 20, in draw
self.canvas.move(self.id, self.x, self.y)
File "C:\Users\t\AppData\Local\Programs\Python\Python37-32\lib\tkinter_init_.py", line 2591, in move
self.tk.call((self._w, 'move') + args)
_tkinter.TclError: invalid command name ".!canvas"
このままではコードが読めないので、質門を編集し、<code>ボタンで、出てくる’’’の枠の中にコードを貼り付けてください
あなたの回答
tips
プレビュー