In: Computer Science
(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.
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)