Question

In: Computer Science

Using Python 3 graphics, create a bouncing ball within a box. Allow the user to control...

Using Python 3 graphics, create a bouncing ball within a box. Allow the user to control the ball using their keyboard.

Solutions

Expert Solution


from graphics import *
import time, random 

def bounceInBox(shape, dx, dy, xLow, xHigh,yLow,yHigh):
          delay = .005 
          for i in range(600): 
               shape.move(dx, dy) 
               center = shape.getCenter()
               x = center.getX() 
               y = center.getY() 
               if x < xLow: 
                  dx = -dx 
               elif x > xHigh: 
                  dx = -dx 
               if y < yLow: 
                  dy = -dy 
               elif y > yHigh: 
                  dy = -dy 
                  time.sleep(delay)
  
def getRandomPoint(xLow, xHigh, yLow, yHigh):
     x = random.randrange(xLow, xHigh+1) 
     y = random.randrange(yLow, yHigh+1) 
     return Point(x, y)

def makeDisk(center, radius, win):
    disk = Circle(center, radius)
    disk.setOutline("red")
    disk.setFill("red") 
    disk.draw(win) 
    return disk
def bounceBall(dx, dy):
    winWidth = 290 
    winHeight = 290 
    win = GraphWin('Ball Bounce', winWidth, winHeight) 
    win.setCoords(0,0,winWidth, winHeight) 
    radius = 10 
    xLow = radius  
    xHigh = winWidth - radius 
    yLow = radius 
    yHigh = winHeight - radius 
    center = getRandomPoint(xLow, xHigh, yLow, yHigh) 
    ball = makeDisk(center, radius, win)
    bounceInBox(ball, dx, dy, xLow, xHigh, yLow, yHigh) 
    win.close()
    
bounceBall(3,5)


Related Solutions

with python, create a fake logo for a company using turtle graphics
with python, create a fake logo for a company using turtle graphics
*Create a python function that uses a while list to allow a user to enter values...
*Create a python function that uses a while list to allow a user to enter values for sales and to add each value into a list in order to track all values entered. Set the gathering of values to stop when the value of zero is entered. Then take the created list and calculate the values within the list to return the total for all the values. Next, take the previously created list and display all the values from smallest...
create a program that asks user math questions and keeps track of answers... Python Allow the...
create a program that asks user math questions and keeps track of answers... Python Allow the user to decide whether or not to keep playing after each math challenge. Ensure the user’s answer to each math problem is greater than or equal to zero. Keep track of how many math problems have been asked and how many have been answered correctly. When finished, inform the user how they did by displaying the total number of math problems, the number they...
Using Tkinter for GUI, A Python program that will allow the user to perform (i) generate...
Using Tkinter for GUI, A Python program that will allow the user to perform (i) generate RSA keys, (ii) encrypt a given message and (iii) decrypt the message without using any cryptographic library. Your program will generate the values of p unless the user provides them manually. Then the program will generate the keys (private and public). Then the program will allow the user to enter his/her message to be encrypted. Finally, the program will perform the decryption operation as...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Write a python program that will allow a user to draw by inputting commands. The program...
Write a python program that will allow a user to draw by inputting commands. The program will load all of the commands first (until it reaches command "exit" or "done"), and then create the drawing. Must include the following: change attributes: color [red | green | blue] width [value] heading [value] position [xval] [yval] drawing: draw_axes draw_tri [x1] [y1] [x2] [y2] [x3] [y3 draw_rect [x] [y] [b] [h] draw_poly [x] [y] [n] [s] draw_path [path] random random [color | width...
Create a python graphics of a smiley face of a dog that includes ears. must start...
Create a python graphics of a smiley face of a dog that includes ears. must start with : from graphics import * or import graphics
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that can sort a singly-linked-list with Merge-sort. The program should read integers from file (hw-extra.txt) and create an unsorted singly-linked list. Then, the list should be sorted using merge sort algorithm. The merge-sort function should take the head of a linked list, and the size of the linked list as parameters. hw-extra.txt provided: 37 32 96 2 25 71 432 132 76 243 6 32...
Create a simple python app that allows the user to create a roster of students and...
Create a simple python app that allows the user to create a roster of students and their grade on CUS-1166. Moreover the app needs to calculate the average grade of students added to the roster. 1-To begin with, create a new file n the same working folder as part A (i.e. cus1166_lab1) and name it app.py. Moreover, create a subfolder and name it mymodules. 2-Within mymodules create the files __init__.py , models.py , math_utils.py . 3-In the models.py file define...
CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT