Question

Q: Python ball in billiards board (Bouncing ball) Ball class contains two essential components: position (ball.x. ball.v. bal

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import math


class Ball: # class ball
x = 0#data members
y = 0#data members
pos = 0#data members
xvel = 0#data members
yvel = 0#data members
vel = 0#data members
d = 0#data members

def __init__(self, x, y, d):#constructor
self.x = x
self.y = y
self.pos = math.sqrt(self.x**2+self.y**2)#pythagoras
self.d = d
self.xvel = math.cos(self.d)
self.yvel = math.sin(self.d)
self.vel = math.sqrt(self.x**2+self.y**2) # pythagoras


def update_ball(ball, boxWidth, boxHeight):#update_ball function
temp = ball
xvel = 1
yvel = temp.yvel/temp.xvel
while temp.x != boxWidth and temp.y != boxHeight:
temp.x += xvel
temp.y += yvel
temp.d -= math.pi
temp.d %= 2*math.pi
temp.xvel = math.cos(ball.d)
temp.yvel = math.sin(ball.d)
ball.pos = math.sqrt(ball.x**2+ball.y**2)
vel = math.sqrt(ball.x**2+ball.y**2)
return temp


def plot_bounces(init_ball, numbounces, boxWidth, boxHeight):#plot_bounces function
temp = init_ball
for i in range(numbounces):
temp = update_ball(temp, boxWidth, boxHeight)


ball = Ball(1, 2, 0)
print(int(ball.x))
print(int(ball.y))
print(int(ball.xvel))
print(int(ball.yvel))
ball = update_ball(ball, 2, 4)
print(int(ball.x))
print(int(ball.y))
print(int(ball.xvel))
print(int(ball.yvel))
print(ball.d)

: Ball init 1.py import math 1 2 3 4 class Ball1: # class ball X = 0#data members 5 y = 0#data members 6 pos 0#data members x

1.py Ball init 27 while temp.x != boxWidth and temp.y- boxHeight: 28 temp.x += xvel temp.y += yvel temp.d math.pi temp.d%- 2*

Ball(1, 2, 0) ball 45 46 print(int(ball.x)) print(int(ball.y)) 47 print(int(ball.xvel)) print(int(ball.yvel) |ball- update_ba

OUTPUT

PS E:\fixer> python .\1.py 1 2 1 0 $2 -1 0 3.141592653589793 HNHONN I

COMMENT DOWN FOR ANY QUERY

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
Q: Python ball in billiards board (Bouncing ball) Ball class contains two essential components: position (ball.x. ball....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT