2016-06-26 7 views
2

私は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() 
+0

これは、Python側のtkinter側で問題が多いようです。 –

+1

errr、 'balls'リストから対応するインスタンスを削除するだけで、削除されたオブジェクトは反復されません。 –

+0

これは「[a]クラスからオブジェクトを削除する」とは関係ありません。ゲームからオブジェクトを削除する方法については、Tkinter APIを読む必要があります。 – Carcigenicate

答えて

0

クラスキャンバスのバインドメソッドを使用し、クリックされた楕円を削除します。あなたのforループは、削除されたオブジェクトがcoordiantesまたは速度を持つことができないため、例外処理が必要です。 del()関数は通常、オブジェクトの削除に使用されます。

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',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()) 


def click(event): 
    if canvas.find_withtag(CURRENT): 
     canvas.delete(CURRENT) 

canvas.bind("<Button-1>", click) 

while True: 
    for ball in balls: 
     try: 
      ball.move() 
     except: 
      del(ball) 

    root.update() 
    time.sleep(0.01) 

root.mainloop() 

UPDATE

リストposがゼロでない場合はちょうどあなたの方法moveで確認してください。それがゼロの場合は、オブジェクト自体を削除してください。キャンバスを削除しても大丈夫ですが、メモリ使用量を気にしていれば、これは私が考えることのできる最良のオプションです。

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',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 len(pos) != 0: 
      if pos[2] >= canvasWidth or pos[0] <= 0: 
       self.xspeed = -self.xspeed 
      if pos[3] >= canvasHeight or pos[1] <= 0: 
       self.yspeed = -self.yspeed 
     else: 
      del(self) 




balls=[] 
for i in range(20): 
    balls.append(Ball()) 


def click(event): 
    if canvas.find_withtag(CURRENT): 
     canvas.delete(CURRENT) 

canvas.bind("<Button-1>", click) 

while True: 
    for ball in balls: 
     ball.move() 

    root.update() 
    time.sleep(0.01) 

root.mainloop() 
+0

ありがとうございます! tryとexceptを使わずにそれを行う方法はありますか? – user6515293

+0

ええ、確かに更新を参照してください:P – Blind0ne

+0

ありがとう!あなたは最高です! – user6515293

関連する問題