Question

In: Computer Science

(please use zelle's Python Graphics library, I've already asked this question and have had to post...

(please use zelle's Python Graphics library, I've
already asked this question and have had to post
this multiple times. I am working with the Python Graphics library and nothing else. thank you! note that I will downvote anything other than python graphics. this is the 4th time I've had to post this same question)
im trying to create a function that has a circle
bouncing left to right on a window and when the
circle is clicked on, it stops moving horizontally
and begins moving up and down on the window.

Solutions

Expert Solution

if you look into the docs there are two functions getMouse and checkMouse

getMouse returns the co-ordinates whenver it is clicked , infact whole function then starts waiting for that click then only it will interpret later code

checkMouse returns whenver the last click happens it does not wait for click to happen , it returns true as soon as click happens .

So here , once you will click to show that click has happened and then you can click on the shape .
if it finds the click is inside the shape then it will iterate vertically
otherwise, it will continue its horizontal bounce.

Further , you can remove getMouse function so that it would start iterating as soon as you click anywhere on the window just by working with checkMouse function.

import time
import random
from graphics import *
import math


winWidth, winHeight = 400, 500
ballRadius = 10
ballColor = 'red'
numBalls = 1
delay = .03
runFor = 30 # in seconds

# After mouse click it will move vertically straight line
def upwards(shape,yLow, yHigh):
dy = 5;
  
for i in range(1000):
center = shape.getCenter()
y = center.getY()
if y < yLow or y > yHigh:
dy = -dy
shape.move(0,dy)
  
time.sleep(.05)
  


# create 1 balls, randomly located
def makeBalls(xLow, xHigh, yLow, yHigh):

balls = []

for _ in range(numBalls):
center = getRandomPoint(xLow, xHigh, yLow, yHigh)

aBall = Circle(center, ballRadius)
aBall.setFill(ballColor)
aBall.draw(win)
balls.append(aBall)

return balls


def inCircle(pt1, circ):

# get the distance between pt1 and circ using the
# distance formula
  
dx = pt1.getX() - circ.getCenter().getX()
dy = pt1.getY() - circ.getCenter().getY()
dist = math.sqrt(dx*dx + dy*dy)

# check whether the distance is less than the radius
return dist <= circ.getRadius()


# animate ball bouncing off edges of window
def bounceInWin(shapes, dx, dy, xLow, xHigh, yLow, yHigh):
  
movedShapes = [(getRandomDirection(dx, dy), shape) for shape in shapes]

start_time = time.time()

while time.time() < start_time + runFor:
shapes = movedShapes
movedShapes = []

for (dx, dy), shape in shapes:
if(win.checkMouse()):# if mouse click occurs
mouse = win.getMouse() #click on the shape to move it vertically
if(inCircle(mouse,shape)):   
upwards(shape, yLow, yHigh);
break;
shape.move(dx, dy)
center = shape.getCenter()

x = center.getX()
if x < xLow or x > xHigh:
dx = -dx

y = center.getY()
if y < yLow or y > yHigh:
dy = -dy

# Could be so much simpler if Point had setX() and setY() methods
movedShapes.append(((dx, dy), shape))

time.sleep(delay)

# get a random direction
def getRandomDirection(dx, dy):
x = random.randrange(-dx, dx)
y = random.randrange(-dy, dy)

return x, y

# get a random Point
def getRandomPoint(xLow, xHigh, yLow, yHigh):
x = random.randrange(xLow, xHigh + 1)
y = random.randrange(yLow, yHigh + 1)

return Point(x, y)

# make balls bounce
def bounceBalls(dx, dy):

xLow = ballRadius * 2
xHigh = winWidth - ballRadius * 2
yLow = ballRadius * 2
yHigh = winHeight - ballRadius * 2

balls = makeBalls(xLow, xHigh, yLow, yHigh)

bounceInWin(balls, dx, dy, xLow, xHigh, yLow, yHigh)

# create screen
win = GraphWin('Ball Bounce', winWidth, winHeight)

bounceBalls(10, 2)


Related Solutions

(please use zelle's Python Graphics library, I've already asked this question and have had to post...
(please use zelle's Python Graphics library, I've already asked this question and have had to post this multiple times. I am working with the Python Graphics library and nothing else. thank you! note that I will downvote anything other than python graphics. this is the 4th time I've had to post this same question, just a simple code with 1 ball is needed ) im trying to create a function that has a circle bouncing left to right on a...
Write the code in python only. You will need the graphics library for this assignment. Please...
Write the code in python only. You will need the graphics library for this assignment. Please download the library and place it in the same file as your solution. Draw a 12" ruler on the screen. A ruler is basically a rectangular outline with tick marks extending from the top edge. The tick marks should be drawn at each quarter-inch mark. Below the tick marks, your ruler should show large integers at each full-inch position.
Please answer this question and do steps 1-7. I've already posted this question and they did...
Please answer this question and do steps 1-7. I've already posted this question and they did not complete the answer. PB2-3    Recording Transactions (in a Journal and T-Accounts); Preparing and Interpreting the Balance Sheet (LO 2-2, 2-3, 2-4, 2-5 and a little Chapter 1) Starbuck$ is a coffee company – a big coffee company. During a 10-year period, the number of Starbucks locations grew from 165 to over 8,500 stores in 50 countries. The following is adapted from Starbucks’s annual...
#2 I post this question twice, please don't answer this post if you already answer the...
#2 I post this question twice, please don't answer this post if you already answer the other one, if you can answer different answer that's fine. REASON WHY I POST IT TWICE, I NEED TWO DIFFERENT VIEW. Discussion: Suicidal Crickets, Zombies Roaches and Other Parasites Tales Please watch the TED Talk ‘Suicidal crickets, zombies roaches and other parasite tales' and post something that you learn as well as whether you believe there could be parasites living inside of humans possibly...
I post this question twice, please don't answer this post if you already answer the other...
I post this question twice, please don't answer this post if you already answer the other one, if you can answer different answer that's fine. REASON WHY I POST IT TWICE, I NEED TWO DIFFERENT VIEW. Discussion: Suicidal Crickets, Zombies Roaches and Other Parasites Tales Please watch the TED Talk ‘Suicidal crickets, zombies roaches and other parasite tales' and post something that you learn as well as whether you believe there could be parasites living inside of humans possibly controlling...
Question #4 DO NOT USE ANY NON-STANDARD LIBRARY. o All the required libraries have already been...
Question #4 DO NOT USE ANY NON-STANDARD LIBRARY. o All the required libraries have already been included. O DO NOT INCLUDE ANY OTHER LIBRARY INTO THE CODE. o DO NOT ALTER THE NAMES OF THE C FILES PROVIDED. o DO NOT ALTER THE NAMES AND PROTOTYPES OF THE FUNCTIONS. DO NOT ALTER THE CODE, ONLY ADD WHATS NEEDED TO MAKE IT WORK. This is the code: #include <stdio.h> /* * This function counts and returns the number of adjacent Up...
Windows PowerShell Scripting Please answer using a switch statement. I have asked this question already and...
Windows PowerShell Scripting Please answer using a switch statement. I have asked this question already and it was wrong. Thank you and I will upvote. 1) Write a PowerShell script that displays the total count of each type (dll, txt, log) of files in c:\windows using switch statement.
***Please do not re post already posted answer to this question that was previously posted and...
***Please do not re post already posted answer to this question that was previously posted and answered. I need to see a new income statement and retained earnings statement with the breakdown of how certain totals are being reached. Thank you! Please help with creating a corrected 2021 multi-step income statement with EPS disclosure and a 2021 retained earnings statement and in good form with the information below. Please provide explanations to how you came up with totals for each...
I've already worked on isEmpty, so please check that and solve the other parts to the...
I've already worked on isEmpty, so please check that and solve the other parts to the question as well. Will rate as soon an I see that it is answered. In this section, you will implement a generic Stack class implemented using linked list. Assume the linked list node class is already defined as below: public class LLNode<T> { public LLNode<T> link; public T info; public LLNode(T in) { info = in; link = null; } } Note that both...
Please Use the STL library for this question and Should be done in C++ and Provide...
Please Use the STL library for this question and Should be done in C++ and Provide screenshots of the OUTPUT Topic: Stack Infix to postfix conversion         Take Input mathematical expression in infix notation, and convert it into a postfix notation. The infix notation may or may not have round parenthesis. Postfix expression evaluation         Take Input mathematical expression in postfix form, evaluate it, and display the result The mathematical expression can contain Numbers as operands (numbers can be of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT