Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python ( Ball Velocity Problem ) ( Need two files: ball.py and pool.py ) The objective in this assignment is to use objects to simulate
Python Ball Velocity ProblemNeed two files: ball.py and pool.py
The objective in this assignment is to use objects to simulate a twodimensional ball moving in a twodimensional cage imagine a circle in a box We will create a ball class and use it to play a simplified game of pool.
Summary of the Ball Class.
Create a new py file called ball.py and define a new class called ball The ball class will have four attributes:
x the xcoordinate of the balls position
y the ycoordinate of the balls position
xvel, the xvelocity
yvel, the yvelocity
status, the status of the ball a string that will contain one of three values
The ball will also have four methods:
Required init the initialization method
locate, a method that will print the position of the ball
update, a method that will update the balls position
setstatus, a method that will set the status of the ball
The Initialization Method
The initialization method should take two arguments in addition to the required self Call these arguments a and b
The method should set the x and y coordinate to and respectively, set xvel to a yvel to b and status to moving
The locate Method
The locate method should print the following to the terminal:
The ball is located at XYwhere X and Y are replaced by the balls coordinates
The setstatus Method
The status method will set the value of the status attribute. It should be set to one of the following:
If the ball is located within one unit of the point set status to hit
If the ball is located within two units of the point set status to miss
Otherwise, set status to moving
The ball is located within one unit of the point if the following is true:
SQRT selfx selfy
The ball is located within two units of the point if the following is true:
SQRT selfx selfy
The update Method
The ball will move in discrete time steps frames The update method will simulate one frame of motion.
The first thing update needs to do is add the balls velocity coordinates to its position coordinates. In order to simulate a ball that moves at a speed of units per frame, we will:
Add times the balls xvel to x
Add times the balls yvel to y
The next thing that update needs to do is check if the ball strikes one of the walls of the cage it is in Our ball will lie in a times unit box. To make it bounce if it hits a wall code the following:
If the balls x is less than and its xvel is negative, multiply xvel by
If the balls x is greater than and its xvel is positive, multiply xvel by
If the balls y is less than and its yvel is negative, multiply yvel by
If the balls y is greater than and its yvel is positive, multiply yvel by
Finally, the update method should call the balls own setstatus method.
Putting the Ball in Motion
Create a new py file called pool.py This program will need to import the ball.py file.
This program will conduct the simulation. First, prompt the user to enter two numbers, and store them in the variables u and v You can use your numinput function from the previous homework to ensure the user enters a number. Furthermore, the user should not be allowed to enter for either number.
Once the user enters their numbers, create a new instance of the ball class. The two numbers the user entered will be the x and y velocity of the ball that the initialization method requires.
Next, set up a loop that implements the following logic:
while the status of the ball is moving
call the balls own update method
call the balls own locate method
The loop will repeat until the ball either falls into the hit hole or miss hole.
The last line of code not part of the body of the loop should print the status of the ball
Execute your program and enter the two velocity coordinates. See if you can get the ball into the correct hole.
Note. Its possible to put the ball onto a route that will never lead to one of the holes. If you need to interrupt execution of your program, hold down the ctrl and c buttons on the keyboard.
Note. My own code for the ball class is about lines of code including blank lines used to make the code easier to read My code to put the ball in motion is about also including blank lines.
If you want to test your results, entering for both velocity coordinates should result in a hit and for both velocity coordinates should result in a miss
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started