私はPythonに新しく、オブジェクトクラスに少し問題があります。ボールのオブジェクトが壁の側から飛び跳ねるというコードを作成しました。私はそれがクリックされた後にボールを削除したいと思います。私はこれを行ういくつかの異なる方法を試したが、すべてがエラーに終わった。以下は、ボールが壁から跳ね返る私のコードです。クリックすればボールを削除するにはどうすればこのコードを編集できますか?ありがとう!Python:クラスからオブジェクトを削除するには?
from Tkinter import *
import random
import time
canvasWidth=480
canvasHeight=320
root= Tk()
canvas=Canvas(root,width=canvasWidth,height=canvasHeight, bg='white')
root.title('RaspPi')
canvas.pack()
class Ball:
def __init__(self):
self.ballSize = 30
self.xposition = random.randint(0 + self.ballSize, canvasWidth - self.ballSize)
self.yposition = random.randint(0 + self.ballSize, canvasHeight - self.ballSize)
self.shape=canvas.create_oval(self.xposition,self.yposition,self.xposition+self.ballSize,self.yposition+self.ballSize, fill='black',activefill="grey",width=0)
self.xspeed = random.randrange(-3,3)
self.yspeed = random.randrange(-3,3)
def move(self):
canvas.move(self.shape, self.xspeed, self.yspeed)
pos = canvas.coords(self.shape)
if pos[2] >= canvasWidth or pos[0] <= 0:
self.xspeed = -self.xspeed
if pos[3] >= canvasHeight or pos[1] <= 0:
self.yspeed = -self.yspeed
balls=[]
for i in range(20):
balls.append(Ball())
while True:
for ball in balls:
ball.move()
root.update()
time.sleep(0.01)
root.mainloop()
これは、Python側のtkinter側で問題が多いようです。 –
errr、 'balls'リストから対応するインスタンスを削除するだけで、削除されたオブジェクトは反復されません。 –
これは「[a]クラスからオブジェクトを削除する」とは関係ありません。ゲームからオブジェクトを削除する方法については、Tkinter APIを読む必要があります。 – Carcigenicate