In: Computer Science
IN PYTHON- Problem: Roll a ball and make it bounce around the sides of a window 4 times
Details: You need to design, write, and test a program that rolls a ball as demonstrated at the beginning of the lab. The ball goes down and right, bounces against the right edge of the window, then goes down and left, bounces against the bottom edge, then goes up and left, bounces against the left edge, then goes up and right, and stops when it hits the top edge.
Program Requirements:
at least 1 function: it returns a value input by the user after checking that that value is between 2 values (a min and a max); you can assume that the user will enter integer values only: it accepts 3 parameters representing the min, max, and a message to pass to the user when asking the user to input a value Your program should use that function to get user input for the starting x coordinate of the ball (the starting y coordinate is 20). Note that you can do that function at the very end (i..e you can start by hard coding a value for the starting x coordinate) Important note: Point ( 0, 0 ) is at the top left of the window. Your program should make sure that the starting x coordinate is between 50 and 450 included (considering that the height and width of the window are set at 500 - see skeleton code provided); this will make sure that the ball hits the right edge first, then the bottom edge, then the left edge, and finally the top edge. Steps: Understand the problem Write the first loop, moving the ball to the right and down; do not worry about the ball going through the wall of the window Make your ball stop at the edge of the window Write the 2nd, then 3rd, then 4th loop.
USE at least 1 or more of the Following Function in your Algorithm:
#1
def make_window( title, width, height ): #2
def make_circle( x, y, radius ): return Circle( Point( x, y ), radius )
#3
def move( circle, moveX, moveY ): circle.move( moveX, moveY )
#4
def change_title( window,title ): window.master.title( title )
#5
def sleep( ms): time.sleep( ms / 1000 )
Ans)
I created a demo program to move circle around the window.program was difficult to complete in given time frame but i created it with move() and make_window, create_circle() function and try to give you a demo , how to move circle inside window.
Now you can play with this more and can make it as per your need.
Attaching program and output window.
from graphics import *
def change_title( window,title ):
window.master.title( title )
# it will create circle ay x,y points and with given radious
def make_circle( x, y, radius ):
return Circle( Point( x, y ), radius )
#moving cirle around the window
def move(circle, dx, dy):
x=10
y=10
for i in range (100000):
# move the circle
circle.move(dx, dy)
x = x + dx
y = y + dy
# when x reaches 190, reverse the direction of movement in
# the x direction
if x >490:
dx = -1*dx
# when y reaches 190, reverse the direction of movement in
# the y direction
if y > 490:
dy = -1*dy
# when x reaches 10, reverse the direction of movement in
# the x direction
if x == 10:
dx = -1*dx
# when y reaches 10, reverse the direction of movement in
# the y direction
if y == 10:
dy = -1*dy
#once circle stoped click muose over window
def make_window( title, width, height ):
win = GraphWin("Move the Circle", 500, 500)
c = make_circle( 0, 0, 100 )
c.draw(win)
win.getMouse()
moveX = .01
moveY = .01
move(c, moveX, moveY)
win.getMouse()
win.close()
# it will create a window , draw circle and calling other methods so it primary main call
make_window("Move the Circle", 500, 500)
if your satisfy above answer please give positive rating or?
please don't downvote
Thankyou!