Question: I need help with my python code I only use the graphics.py library because those were the instructions ... The code is about the Pong
I need help with my python code I only use the graphics.py library because those were the instructions ... The code is about the Pong game so far I have created the objects and I have made the ball move and collide at the limits. ... my question is in the last part of the code where I put it as a comment .. I can't make the ball collide with the paddle. I don't know if they could help me in that part ... The problem is in the part that says #Paddle and ball collision ..... Here is my code:
from graphics import *
def paddleA_up(paddleA):
paddleA.move(0, -20)
def paddleA_down(paddleA):
paddleA.move(0, 20)
def paddleB_up(paddleB):
paddleB.move(0, -20)
def paddleB_down(paddleB):
paddleB.move(0, 20)
def main():
#Window
win = GraphWin("Pong Game", 600, 400)
win.setBackground("black")
#Score
#PaddleA
paddleA = Rectangle(Point(5,2), Point(10, 70))
paddleA.setOutline("white")
paddleA.setFill("white")
paddleA.draw(win)
paddleA.move(5, 170)
paddleA.setWidth(20)
#PaddleB
paddleB = Rectangle(Point(5,2), Point(10, 70))
paddleB.setOutline("white")
paddleB.setFill("white")
paddleB.draw(win)
paddleB.move(580, 170)
paddleB.setWidth(20)
#Ball
radius = 8
ball = Circle(Point(20, 20), radius)
ball.setOutline("white")
ball.setFill("white")
ball.draw(win)
ball.move(290,170)
dx = 1
dy = 1
#Move Paddle
while True:
key = win.checkKey()
yFloor = radius
yCieling = win.getHeight() - radius
xFloor = radius
xCieling = win.getHeight() - radius
ball.move(dx, dy)
if key == "o":
break
elif key == "Up":
paddleA_up(paddleA)
elif key == "Down":
paddleA_down(paddleA)
elif key == "w":
paddleB_up(paddleB)
elif key == "s":
paddleB_down(paddleB)
#Move Ball
elif ball.getCenter().getY() <= yFloor or ball.getCenter().getY() >= yCieling:
dy = -dy
elif ball.getCenter().getY() <= xFloor or ball.getCenter().getY() >= xCieling:
dx = -dx
if ball.getCenter().getX() > 630:
ball.move(290,170)
dx *= -dx
if ball.getCenter().getX() < -620:
ball.getCenter()
dx *= dx
#Paddle and ball collision
#if ((ball.getCenter().getX() > 580 and ball.getCenter().getX() < 580)
#and (ball.getCenter().getY() < paddleB.getCenter().getY() + 20
#and ball.getCenter().getY() > paddleB.getCenter().getY() - 20)):
#ball.getCenter()
#dx *= -dx
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
