试图让球对象从桨叶上弹回

时间:2017-05-12 17:38:36

标签: python tkinter

我是python的新手,正在为我的python类简介做最后的项目。我完成了大部分的桨球比赛,但无法弄清楚如何让球对象从我的球拍物体上反弹。

我已经看了Stackoverflow一段时间,并花了几个小时试图自己解决这个问题而没有任何成功。如果有人有任何想法我真的可以使用帮助。

如果我需要更好地解释,为了让您更好地理解,请发表评论。

GUI文件:

Import tkinter, random, particle, and helpers
from tkinter import *
from ball import *
from paddle import *
from time import *

class PaddleBall:    
    def __init__(self, window):
        ''' Construct the paddle ball GUI '''
        self.window = window
        self.window.protocol('WM_DELETE_WINDOW', self.safe_exit)
        self.width = 700
        self.height = 900
        self.canvas = Canvas(self.window, bg='black', width=self.width, height=self.height, highlightthickness=0)
        self.canvas.bind_all("<KeyPress-Left>", self.move_left)
        self.canvas.bind_all("<KeyPress-Right>", self.move_right)

        self.canvas.pack()

        # Create a label to indicate instructions
        instructions = Label(window, text="Controls: Left & Right Arrow Keys")
        instructions.pack(side=BOTTOM, expand=YES) 

        # Create a button to clear Ball  
        restart_button = Button(window, text="Play", command=self.reset)
        restart_button.pack(side=BOTTOM, expand=YES)

        self.ball = Ball(350, 350)
        self.paddle = Paddle(300, 850, 400, 860, 0, 0)
        self.terminated = False
        self.render()

    def ballobject(self):
        self.ball = Ball(350, 350)
        self.paddle = Paddle(300, 850, 400, 860, 0, 0)
        self.render()

    def reset(self):
        self.terminated = True

    def safe_exit(self):
        ''' Turn off the event loop before closing the GUI '''
        self.terminated = True
        self.window.destroy()

    # Render everything    
    def render(self):
        # While program is not terminated
        if not self.terminated:
            # Erase Canvas
            self.canvas.delete(ALL)

            # Move ball
            self.ball.move(self.canvas, self.paddle)   

            # Render ball
            self.ball.render(self.canvas)    

            # Render paddle
            self.paddle.render(self.canvas)  

            # use distance() to detect collision between ball and paddle.
            '''Ball.bounce(self)'''

            # Animate the particles movement
            self.canvas.after(10, self.render)

        else: 
            # Erase Canvas
            self.canvas.delete(ALL)

            self.terminated = False

            self.canvas.after(50, self.ballobject)


    def move_left(self, event):
        self.paddle.move_left(event)

    def move_right(self, event):
        self.paddle.move_right(event)


if __name__ == '__main__':
root = Tk()
root.option_add('*font', ('Verdana', 12, 'bold italic')) # Found at http://effbot.org/tkinterbook/tkinter-widget-styling.htm
root.resizable(0,0) # Found at https://mail.python.org/pipermail/tutor/2001-September/008504.html
root.title('Paddle Ball')  
root.wm_attributes("-topmost", -1)
app = PaddleBall(root)
root.mainloop()

BALL CLASS FILE:

class Ball:
    '''
    Ball models a single ball that may be rendered to a canvas
    '''

    def __init__(self, x, y, radius = 15,):
        '''
        Constructor
        '''
        self._x = x
        self._y = y
        self._velX = randint(-10,10)
        self._velY = randint(-10,-5)
        self._radius = radius
        self._color = 'white'        
        self._tx = 350
        self._ty = 400
        self._t = ""
        self._tfill = "red"
        self._tfont = ("Arial", 35, "bold italic")

    # This method renders the ball    
    def render(self, canvas):
        canvas.create_oval(self._x - self._radius, self._y - self._radius, self._x + self._radius, self._y + self._radius, fill = self._color)
        canvas.create_text(self._tx, self._ty, text = self._t, fill = self._tfill, font = self._tfont)


    # This method moves the ball    
    def move(self, canvas, Paddle):
        # Update Position

        self._x += self._velX
        self._y += self._velY

        # If the ball hits any of the wall negate the velocity
        if (self._x + self._radius > canvas.winfo_reqwidth() and self._velX > 0) or (self._x - self._radius < 0 and self._velX < 0):
            self._velX = -self._velX
        if  (self._y + self._radius < 0 and self._velY < 0):
            self._velY = -self._velY
        if (self._y + self._radius > canvas.winfo_reqheight() and self._velY > 0):
            self._velY = 0
        self._velX = 0
            self._t = "                 GAME OVER! \n Click the play button to play again."


#*****THIS IS WHAT I'M HAVING TROUBLE WITH******
        # Determine if the ball hits the paddle
        if ((self._x + self._radius > Paddle._x(self) and self._velX > 0) or (self._x + self._radius < Paddle._x2(self))) and (self._y < Paddle._y(self)):
            self._velX = -self._velX

PADDLE CLASS FILE:

# Import math and helpers
from tkinter import *
import math
from gui import *


class Paddle:

    def __init__(self, x, y, x2, y2, velX, velY):
        '''
        Constructor
        '''
        self._x = x
        self._y = y
        self._x2 = x2
        self._y2 = y2
        self._velX = velX
        self._velY = velY
        self._color = 'white'

    def getpadx(self):
        return self._x

    def getpady(self):
        return self._y

    def getpadx1(self):
        return self._x2

    def getpady2(self):
        return self._y2

    # This method renders the paddle    
    def render(self, canvas):
        canvas.create_rectangle(self._x, self._y, self._x2, self._y2, fill = self._color)

    # This method moves the paddle    
    def move(self, canvas):
        # Update Position

        # If the paddle hits any of the wall negate the velocity
        if (self._x + self._radius > canvas.winfo_reqwidth() and self._velX > 0) or (self._x - self._radius < 0 and self._velX < 0):
            self._velX = -self._velX

    def move_left(self, event):
        self._x -= 35
        self._x2 -= 35

    def move_right(self, event):
        self._x += 35
        self._x2 += 35

1 个答案:

答案 0 :(得分:1)

我在朋友的帮助下想出来了。我所要做的就是改变这段代码:

从此:

if ((self._x + self._radius > Paddle._x(self) and self._velX > 0) or (self._x + self._radius < Paddle._x2(self))) and (self._y < Paddle._y(self)): self._velX = -self._velX

对此:

`if (self._x > Paddle._x) and (self._x < Paddle._x2):
    if (self._y + self._radius > Paddle._y):
        self._velY = -self._velY
        self._velX = self._velX + randint(-2,2)`
相关问题