2017-06-12 63 views
0

我創建了一個程序,使球圍繞畫布移動,並使畫布邊緣反彈。這個效果非常好,所以我試圖添加另一個球,但不是創建了2個球,而是創建了4個球,其中2個球移動,另外兩個球保持不動。 這是代碼:Tkinter移動球程序創建4個球而不是2個

#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

的問題是,你除了改變Ball類還加入了第二球。面向對象編程的思想(即用python中的簡單詞語)就是創建一個類,這裏​​是Ball,它定義了一個通用球如何工作。你可以從這個類創建儘可能多的對象(這裏是ball1,ball2等)。

在你的代碼只需要

  1. 刪除行

    self.ball2 = canvas.create_oval(self.x1,self.y1,self.x2,self.y2,填寫= 「紅」)

  2. 取出完整move_ball2功能

  3. 變化

ball2.move_ball2(X0,Y0)

ball2.move_ball(x0,y0) 

,並預期它會奏效。

2

您正在初始化您的__init()__方法中的兩個球。

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") 

當爲對象創建類爲兩個球而不是一個。所以改變類中的init方法應該解決它。您需要爲兩個不同的球創建兩個不同的對象,而不是類中的兩個不同的變量。

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.一種方法應該適用於兩個球。

ball1 = Ball(canvas, 10, 10, 30, 30) 
ball2 = Ball(canvas, 60, 60, 80, 80) 

當兩個語句執行時,它們將類Ball的對象返回到變量ball1和ball2。這些對象是相互獨立的,它們將分別在它們之間調用而不會相互影響。爲兩個不同的變量創建兩個函數會破壞創建類的目的。

您可能想要詳細瞭解herehere的課程和對象。有關於類和對象的video tutorials以及它們如何在tkinter上實現。

+0

另一個很好的資源是[this](http://programarcadegames.com/index.php?chapter=introduction_to_classes&lang=de#section_12)。提供一個很好的介紹python的面向對象編程和遊戲開發 – scotty3785

+0

添加了答案的鏈接。它確實爲課程提供了很好且簡單的解釋。 –