2017-06-12 3 views
0

ボールをキャンバスの周りを動かし、キャンバスのエッジをバウンスさせるプログラムを作成しました。これは非常にうまくいったので、別のボールを追加しようとしましたが、2つのボールを作成する代わりに、4つのボールが移動し、残りの2つは静止していました。 これはコードです:2つではなく4つのボールを作成するTkinter moving ballsプログラム

#imports 
from tkinter import * 
import random 
from random import randint 



#Creating random x and y for the balls to move along 
x = random.randint(1,10) 
y = random.randint(1,10) 
print(x, " Ball 1y") 
print(y, " Ball 1x") 

x0 = random.randint(1,10) 
y0 = random.randint(1,10) 
print(y0," Ball2y") 
print(x0, " Ball2x") 


class Ball: 

    #creates balls 
    def __init__(self, canvas, x1,y1,x2,y2): 
     self.x1 = x1 
     self.y1 = y1 
     self.x2 = x2 
     self.y2 = y2 
     self.canvas = canvas 
     self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2,   fill="red") 
     self.ball2 = canvas.create_oval(self.x1, self.y1, self.x2, self.y2,  fill="red") 


    def move_ball(self, x, y): 
     #function to move ball 

     coords = canvas.coords(self.ball) 
     #Because coord is stored in a list I need to call which coord is bigger than the edge of the canvas 
     if coords[0] >= 280: 
      #multiplying by a negative number makes the ball "bounce" 
      x *= -1 
     if coords[0] <= 0: 
      x *= -1 
     if coords[3] <= 20: 
      y *= -1 
     if coords[3] >= 300: 
      y *= -1 
     print(coords, " Ball1") 
     #makes ball move 
     self.canvas.move(self.ball, x, y) 
     self.canvas.after(50, self.move_ball, x, y) 


    def move_ball2(self, x0, y0): 

     #same as previous different variables 
     coords2 = canvas.coords(self.ball2) 

     if coords2[0] >= 280: 
      x0 *= -1 
     if coords2[0] <= 0: 
      x0 *= -1 
     if coords2[3] <= 20: 
      y0 *= -1 
     if coords2[3] >= 300: 
      y0 *= -1 
     print(coords2, " Ball2") 
     self.canvas.move(self.ball2, x0, y0) 
     self.canvas.after(50, self.move_ball2, x0, y0) 



#changes window titles etc. 
root = Tk() 
root.title("Balls") 
root.resizable(False, False) 
canvas = Canvas(root, width = 300, height = 300) 
canvas.pack() 

#creates ball with dimensions of the ball 
ball1 = Ball(canvas, 10, 10, 30, 30) 
ball2 = Ball(canvas, 60, 60, 80, 80) 
#calls move ball function 
ball1.move_ball(x, y) 
ball2.move_ball2(x0,y0) 

root.mainloop() 

答えて

2

問題は、あなたが第2のボールを追加することに加えて、クラスのボールを変更したことです。オブジェクト指向プログラミング(すなわち、単純な言葉では、Pythonのクラス)のアイデアは、ここではBallというクラスを作成して、どのように一般的なボールが動作するかを定義することです。このクラスから、必要な数のオブジェクト(ここではball1、ball2など)を作成できます。

あなただけ記入し、

self.ball2 = canvas.create_oval(self.x1、self.y1、self.x2、self.y2行を削除

    1. にを持っているあなたのコードで

      変更

      完全move_ball2機能を削除

    2. )= "赤"

      ball2.move_ball(x0,y0) 
      

      から

    ball2.move_ball2(X0、Y0)

    にし、期待どおりに動作します。

  • 2

    __init()__メソッドで2つのボールを初期化しています。

    self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill="red") 
    self.ball2 = canvas.create_oval(self.x1, self.y1, self.x2, self.y2,fill="red") 
    

    オブジェクトが1つではなく2つのボールに対して作成されている場合。したがって、クラス内のinitメソッドを変更すると修正されるはずです。クラス自体に2つの異なる変数ではなく、2つの異なるボールに2つの異なるオブジェクトを作成する必要があります。

    class Ball: 
    
        #creates balls 
        def __init__(self, canvas, x1,y1,x2,y2): 
         self.x1 = x1 
         self.y1 = y1 
         self.x2 = x2 
         self.y2 = y2 
         self.canvas = canvas 
         self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill="red") 
    

    また、ボール1とボール2を移動する方法は2種類あります。どちらの方法でもどちらの方法でも動作します。

    2つのステートメントが実行されると、Ballクラスのオブジェクトが変数ball1とball2に返されます。これらのオブジェクトはお互いに独立しており、それぞれに影響を与えずに個別に呼び出されます(move_ball)。 2つの異なる変数に対して2つの関数を作成すると、クラスを作成する目的が無効になります。

    クラスやオブジェクトについては、herehereから少しお読みください。クラスとオブジェクトにはvideo tutorialsがあり、それらはtkinterにどのように実装されていますか?

    +0

    もう一つの素晴らしいリソースは[this](http://programarcadegames.com/index.php?chapter=introduction_to_classes&lang=de#section_12)です。 Pythonのオブジェクト指向プログラミングとゲーム開発の良い紹介を提供する – scotty3785

    +0

    答えにリンクを追加しました。それはクラスに良いと簡単な説明を提供しています。 –

    関連する問題