Answered step by step
Verified Expert Solution
Link Copied!

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 Problem)(Need two files: ball.py and pool.py)
The objective in this assignment is to use objects to simulate a two-dimensional ball moving in a two-dimensional 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 x-coordinate of the balls position
- y, the y-coordinate of the balls position
- xvel, the x-velocity
- yvel, the y-velocity
- 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 5 and 5 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 X,Y.(where 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 (10,10), set status to hit
- If the ball is located within two units of the point (0,0), set status to miss
- Otherwise, set status to moving
The ball is located within one unit of the point (10,10) if the following is true:
SQRT [(self.x -10)2+(self.y -10)2]<=1
The ball is located within two units of the point (0,0) if the following is true:
SQRT [(self.x -0)2+(self.y -0)2]<=1
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 0.01 units per frame, we will:
- Add 0.01 times the balls xvel to x 3
- Add 0.01 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 10\times 10 unit box. To make it bounce if it hits a wall, code the following:
- If the balls x is less than 0 and its xvel is negative, multiply xvel by -1
- If the balls x is greater than 10 and its xvel is positive, multiply xvel by -1
- If the balls y is less than 0 and its yvel is negative, multiply yvel by -1
- If the balls y is greater than 10 and its yvel is positive, multiply yvel by -1
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 0 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 40 lines of code (including blank lines used to make the code easier to read). My code to put the ball in motion is about 20, also including blank lines.
If you want to test your results, entering 1 for both velocity coordinates should result in a hit, and -1 for both velocity coordinates should result in a miss.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

4-44. Management (inaugurated) the recycling policy six months ago.

Answered: 1 week ago