From the python code below, change the color of the circle each time it hits a b
ID: 3778350 • Letter: F
Question
From the python code below, change the color of the circle each time it hits a border. If it hits the x border, change it green. If it hits the y border, change it blue. from graphics import * from random import * from time import * def main(): win = GraphWin("Assignment 8", 800, 800) p1 = Point(400,400) x = randint(-5,5) y = randint(-5,5) for i in range(10000): c1 = Circle(p1,50) c1.setFill('blue') c1.draw(win) sleep(.01) c1.undraw() if (p1.getX() < 0 or p1.getX() > 800) and (p1.getY() < 0 or p1.getY() > 800): # hit the corner x = x * -1 y = y * -1 p1 = Point(p1.getX() + x, p1.getY() + y) if p1.getX() < 0 or p1.getX() > 800: # hit the x border x = x * -1 p1 = Point(p1.getX() + x, p1.getY() + y) elif p1.getY() < 0 or p1.getY() > 800: # hit the y border y = y * -1 p1 = Point(p1.getX() + x, p1.getY() + y) else: p1 = Point(p1.getX() + x, p1.getY() + y) main()
Explanation / Answer
from graphics import *
from random import *
from time import *
def main():
win = GraphWin("Assignment 8", 800, 800)
p1 = Point(400,400)
x = randint(-5,5)
y = randint(-5,5)
color='blue' #keep the variable color outside the loop
for i in range(10000): #update the color everytime it hits the x or y border
c1 = Circle(p1,50) #i have highlighted the added code
c1.setFill(color)
c1.draw(win)
sleep(.01)
c1.undraw()
if (p1.getX() < 0 or p1.getX() > 800) and (p1.getY() < 0 or p1.getY() > 800):
x = x * -1
y = y * -1
p1 = Point(p1.getX() + x, p1.getY() + y)
if p1.getX() < 0 or p1.getX() > 800:
# hit the x border
x = x * -1
p1 = Point(p1.getX() + x, p1.getY() + y)
color='blue'
elif p1.getY() < 0 or p1.getY() > 800:
# hit the y border
y = y * -1
p1 = Point(p1.getX() + x, p1.getY() + y)
color='green'
else:
p1 = Point(p1.getX() + x, p1.getY() + y)
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.