In: Computer Science
IN PYTHON
Write a graphics program that creates a graphics window that is 500 pixels wide and 300 pixels high. Then draw a blue circle with radius 25 at (x,y) = (100,150). Then have the ‘ball’ (ie the circle) move horizontally to the right by 5 pixel steps, until it reaches x = 200. Then have it change color to red and move back to the starting point.
Here is the solutioin,
i providing two program one with for loop and other with while loop
program 1
# import of graphics module
# please install the module with the command
# pip install graphics
from graphics import *
from time import sleep
# window with width 500 and height 300
win = GraphWin('Radar', width=500, height=300)
# circle at position 100, 150 and radius 25
circle = Circle(Point(100, 150), 25)
circle.setFill("blue")
circle.draw(win)
# while loop works on condition
# continues the loop till the condition is true
while circle.getCenter().getX() <= 200:
# to move the circle by 5 points horizontally
circle.move(5, 0)
# for delay
sleep(0.05)
# change of colour to red
circle.setFill("red")
while circle.getCenter().getX() >= 100:
# to move the circle by 5 points horizontally
circle.move(-5, 0)
# for delay
sleep(0.05)
win.getMouse()
win.close()
program 2:
# import of graphics module
# please install the module with the command
# pip install graphics
from graphics import *
from time import sleep
# window with width 500 and height 300
win = GraphWin('Radar', width=500, height=300)
# circle at position 100, 150 and radius 25
circle = Circle(Point(100, 150), 25)
circle.setFill("blue")
circle.draw(win)
# basically i have divided 100/5 = 20
# this will take the circle till 200 position
for i in range(20):
# to move the circle by 5 points horizontally
circle.move(5, 0)
# for delay
sleep(0.05)
# change of colour to red
circle.setFill("red")
# basically i have divided 100/5 = 20
# this will take the circle till 200 position
for i in range(20):
# to move the circle by 5 points horizontally
circle.move(-5, 0)
# for delay
sleep(0.05)
win.getMouse()
win.close()
here is the sample output(final stage):-
