簡體   English   中英

Pong with Python(Tkinter) paddle 不工作

[英]Pong with Python(Tkinter) paddle not working

所以我是 GUI 和 Tkinter 的新手。 我正在使用 Tkinter 進行單槳乒乓球游戲,我需要槳像槳一樣工作,並在圓盤從右側或左側擊中槳時將圓盤彈開。 我的當前代碼確實在 x 軸上反彈了槳位置的磁盤,但它不會讓磁盤超過 line_x。 我的想法是否正確? 還是我走了? 如果有人可以修復我的代碼以使其正常工作,那就太棒了。 對於已經使用 GUI 工作一段時間的人來說,這可能很容易,但我被踩到了。 請幫忙。

from tkinter import *

import random

class ControlAnimation:

    def __init__(self):

        my_window = Tk() # create a window
        my_window.title("Control Animation Demo")

        self.width = 400
        self.height = 200
        self.line_x = 350
        self.line_top = 75
        self.line_bot = 125
        self.paddle_width = 10
        self.dy = 5
        self.sleep_time = 50
        self.is_stopped = False

        self.my_canvas = Canvas(my_window, bg = 'white', \
            width = self.width, height = self.height)
        self.my_canvas.pack()

        frm_control = Frame(my_window) # for comand buttons below canvas
        frm_control.pack()
        btn_stop = Button(frm_control, text = 'Stop', \
                     command = self.stop)
        btn_stop.pack(side = LEFT)  
        btn_resume = Button(frm_control, text = 'Resume', \
                       command = self.resume)
        btn_resume.pack(side = LEFT)  
        btn_faster = Button(frm_control, text = 'Faster', \
                       command = self.faster)
        btn_faster.pack(side = LEFT)  
        btn_slower = Button(frm_control, text = 'Slower', \
                       command = self.slower)
        btn_slower.pack(side = LEFT)  

        self.radius = 20
        self.x = self.radius # just to start; y is at canvas center
        self.y = self.height/2
        # (x, y) is center of disk for this program, but ...
        # recall: x1,y1 and x2,y2 form a bounding box for disk
        self.my_canvas.create_oval(\
            self.x - self.radius, self.height/2 + self.radius,\
            self.x + self.radius, self.height/2 - self.radius,\
                          fill = "red", tags = "disk")
        self.my_canvas.create_line(self.line_x, self.line_top, \
                               self.line_x, self.line_bot, \
          width = self.paddle_width, fill = "blue", tags = "paddle")
        self.my_canvas.bind("<KeyPress-Up>", self.move_paddle)
        self.my_canvas.bind("<KeyPress-Down>", self.move_paddle)
        self.my_canvas.bind("<B1-Motion>", self.left_click_paddle)
        self.my_canvas.bind("<B3-Motion>", self.right_click_paddle)

        self.animate()
        self.my_canvas.focus_set()
        my_window.mainloop()

    def stop(self):
        self.is_stopped = True

    def resume(self):
        self.is_stopped = False
        self.animate()

    def faster(self):
        if self.sleep_time > 5:
            self.sleep_time -= 15

    def slower(self):
        self.sleep_time += 15

    def animate(self):
        dx = 3
        dy = 2
        while not self.is_stopped :
            self.my_canvas.move("disk", dx, dy) # move right
            self.my_canvas.after(self.sleep_time) # sleep for a few ms            
            # redraw/update the canvas w/ new oval position
            self.my_canvas.update() 

            # increment x to set up for next re-draw
            r = random.randint(-1, 1)

            self.x += dx # moves the disk
            if self.x + self.radius > self.width: # hit right boundary
                dx = -dx + r # add randomness
            elif self.x - self.radius <= 0: # hit left boundary
                dx = -dx + r # add randomness
            elif self.x + self.radius > self.line_x and self.x + self.radius <= self.line_top:
                dx = -dx + r 
            #elif self.x - self.radius <= self.line_x:
                #dx = -dx + r
            # increment y to set up for next re-draw
            self.y += dy
            if self.y + self.radius > self.height: # hit bottom boundary
                dy = -dy
            elif self.y - self.radius <= 0: # hit top boundary
                dy = -dy
    def left_click_paddle(self, event):
        print(" clicked at =", event.x, event.y)
        print("-"*30)
        self.move_paddle( -self.dy)

    def right_click_paddle(self, event):
        print(" clicked at =", event.x, event.y)
        print("-"*30)
        self.move_paddle( self.dy)        

    def move_paddle(self, increment):
        self.line_top += increment
        self.line_bot += increment
        self.my_canvas.delete("paddle")
        self.my_canvas.create_line(self.line_x, self.line_top, \
                               self.line_x, self.line_bot, \
              width = 10, fill = "blue", tags = "paddle") 
ControlAnimation() # create instance of the GUI

在您的運動循環中,檢查是否觸發 x 方向變化的邏輯不完整。 它應該是這樣的:

# new disk x, y positions
self.x += dx
self.y += dy

# Change "dx" sign if the ball hit something horizontally
if self.x + self.radius > self.width-1:
    # ball hit right frame boundary
    dx = -dx + r
elif self.x - self.radius <= 1:
    # ball hit left frame boundary
    dx = -dx + r
elif ( self.line_x <= self.x+self.radius <= self.line_x + 2*dx
       and self.line_top <= self.y <= self.line_bot ):
    # ball hit paddle from the left
    dx = -dx + r 
elif ( self.line_x + 2*dx <= self.x-self.radius <= self.line_x
       and self.line_top <= self.y <= self.line_bot ):
    # ball hit paddle from the right
    dx = -dx + r

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM