Below is the code for a Ball class, which illustrate the bouncing of the balls i
ID: 3857946 • Letter: B
Question
Below is the code for a Ball class, which illustrate the bouncing of the balls in X and Y coordinates.
class Ball:
def __init__ (self, x, y, vx, vy):
self.xPos = x
self.yPos = y
self.xVec = vx
self.yVec = vy
def getXPos(self):
return self.xPos
def getYPos(self):
return self.yPos
def getXVel(self):
return self.xVel
def getYVel(self):
return self.yVel
def advance(self, timestep=1):
self.xPos += self.xVel *timestep
self.yPos += self.yVel *timestep
Write a bounce method for the class that multiplies both the y-coordinate
and y-velocity by -1.
Explanation / Answer
The answer is as follows:
def bounce(self):
self.yPos = self.yPos* (-1)
self.yVel = self.yVel * (-1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.