2017-05-12 9 views
1

私はPythonの初心者です。私は、イントロto Pythonクラスの最終プロジェクトに取り組んでいます。私はパドルボールゲームの大部分を行っていますが、ボールオブジェクトをパドルオブジェクトから跳ね返させる方法を理解することはできません。ボールオブジェクトをパドルから跳ね返ろうとする

私はしばらくの間、Stackoverflowを見てきましたが、何の成功もなく自分でそれを理解しようと数時間を費やしました。誰かがアイデアを持っているなら、私は本当に助けを使うことができます。

私はあなたがより良い理解を得るために、より良く説明する必要がある場合は、ただコメントしてください。

GUIのFILE:

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

パドルクラスファイル:

# 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 
+0

このボールは、パドルの上部または下部に跳ね返ることはできますか?そのデータから、パドルは長方形の形状を持ち、ボールとパドルがあり、2Dのx-y平面内をちょうど回っているようです。 – wandadars

+0

これはあなたの質問には関係ありませんが、セッターと一緒にpythonicでないゲッターを使用しているようです。 –

答えて

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)` 
関連する問題