2017-09-23 68 views
0

我有以下代碼,它設法使球在屏幕的頂端和底端反彈。類和tkinter球運動

工作代碼爲反彈頂部和底部的

from tkinter import * 
import random 
import time 

class Ball: 
    def __init__(self,canvas,color): 
     self.canvas=canvas 
     self.id=canvas.create_oval(30,30,50,50,fill=color) 
     self.canvas.move(self.id,100,200) 

     #ADD THESE LINES TO OUR __INIT__ METHOD 
     self.x=0 
     self.y=-1 
     self.canvas_height=self.canvas.winfo_height() 

    def draw(self): 
     self.canvas.move(self.id,self.x,self.y) 
     pos=self.canvas.coords(self.id) 

     if pos[1] <=0: 
      self.y=1 
     if pos[3] >=self.canvas_height: 
      self.y=-1 


def main(): 
    tk=Tk() 
    tk.title("My 21st Century Pong Game") 
    tk.resizable(0,0) 
    tk.wm_attributes("-topmost",1) 
    canvas=Canvas(tk,bg="white",width=500,height=400,bd=0,highlightthickness=0) 
    canvas.pack() 
    tk.update() 

    ball1=Ball(canvas,'green') 
    while 1: 
     tk.update() 
     ball1.draw() #call the ball draw method here 
     time.sleep(0.01) 
main() 

當試圖做到這一點的左到右(左,右牆反彈),我不能完全弄清楚邏輯或解決我的錯誤如下所示。

我已經試過了彈跳左右

self.x=1 #set the object variable x to 0 (don't move the ball horizontally) 
     self.y=-0 #set the object variable y to -1 (this means keep moving the ball UP on initilisation) 
     self.canvas_height=self.canvas.winfo_height() #set the canvas height by calling the canvas function winfo_height (it gives us the current canvas height) 

    def draw(self): 
     self.canvas.move(self.id,self.x,self.y) 
     pos=self.canvas.coords(self.id) 

     if pos[2] <=0: #if you hit the top of the screen then stop subtracting 1 as defined in the __init__ method and therefore stop moving up -reverse directions 
      self.x=-1 
     if pos[3] >=self.canvas_height: #if the bottom coordinates are greater or equal to canvas height, then reverse again, and set y back to -1 (go up) 
      self.x=1 

一個答案

可能有人提供簡單的解釋爲所需要的邏輯解決問題,座標來自哪裏以及pos [0],pos [1]等指的是什麼。我有一個想法,但它不是很清楚,並且會從某種清晰度中受益(就像我想象的那樣)。

所以,我在解釋+編碼解決方案修復(使用我的原始代碼)來解決問題。

回答

0

代碼幾乎是正確的。事實上,一時的進一步思考可能會給你答案是非常正確的。您的'移動x'代碼已經從'移動y'代碼複製粘貼,但沒有足夠改變。謹防這樣做。

迴應'pos pos [0]等於什麼意思',我邀請您閱讀Get coords of an oval in Tkinter。 通常,矩形的座標爲(左,上,右,下)。

你會發現,我已經改變了以下一些事情,除了糾正你的錯誤:

  1. 我已經改變了x & y變量vxvy。數值是球的速度,而不是位置。
  2. 函數名現在是move,而不是draw,因爲該函數移動並且不繪製。
  3. alive變量與tk.protocol行爲程序提供了一種在用戶關閉窗口後清理的整潔方式。

from tkinter import * 
import random 
import time 

class Ball: 
    def __init__(self,canvas,color): 
     self.alive = True 
     self.canvas = canvas 
     self.id = canvas.create_oval(30, 30, 50, 50, fill=color) 
     self.canvas.move(self.id, 100, 200) 

     #ADD THESE LINES TO OUR __INIT__ METHOD 
     self.vx = 1 
     self.vy = -1 
     self.canvas_width = self.canvas.winfo_width() 
     self.canvas_height = self.canvas.winfo_height() 

    def move(self): 
     self.canvas.move(self.id, self.vx, self.vy) 
     pos = self.canvas.coords(self.id) 
     if pos[0] <= 0: 
      self.vx = 1 
     if pos[2] >= self.canvas_width: 
      self.vx = -1 
     if pos[1] <= 0: 
      self.vy = 1 
     if pos[3] >= self.canvas_height: 
      self.vy = -1 

    def kill(self): 
     self.alive = False 

def main(): 
    tk = Tk() 
    tk.title("My 21st Century Pong Game") 
    tk.resizable(0, 0) 
    tk.wm_attributes("-topmost", 1) 
    canvas = Canvas(tk, bg="white", width=500, height=400, bd=0, highlightthickness=0) 
    canvas.pack() 
    tk.update() 

    tk.protocol("WM_DELETE_WINDOW", lambda: ball1.kill()) 

    ball1 = Ball(canvas, 'green') 
    while ball1.alive: 
     tk.update() 
     ball1.move() #call the ball move method here 
     time.sleep(0.01) 
    tk.destroy() 

if __name__ == "__main__": 
    main()