Jed Rembold & Fred Agbo
February 21, 2024
Which of the below images is the mostly likely output of the code to the right?
def make_circle(x,y,r):
c = GOval(x-r, y-r, 2*r, 2*r)
c.set_filled(True)
if randint(1, 100) > 75:
c.set_color("#F92672") #pink
else:
c.set_color("#66D9EF") #blue
return c
gw = GWindow(500, 500)
for i in range(50):
gw.add(make_circle(
randint(50,450),
randint(50,450),
randint(5,50)
)
)
GWindow
object (mostly commonly
named gw
)!gw
objectgw.my_attribute_name = some_cool_value
Just refer to the object and attribute name:
print(gw.my_attribute_name)
from pgl import GWindow, GLine
WIDTH = 500
HEIGHT = 500
def draw_lines():
def mousedown_event(e):
x = e.get_x()
y = e.get_y()
gw.line = GLine(x,y,x,y)
gw.add(gw.line)
def drag_action(e):
gw.line.set_end_point(e.get_x(), e.get_y())
gw = GWindow(WIDTH, HEIGHT)
gw.line = None
gw.add_event_listener("mousedown", mousedown_event)
gw.add_event_listener("drag", drag_action)
if __name__ == '__main__':
draw_lines()
Created with
gw.set_timeout(function, delay)
where function
is the callback function
and delay
is the time interval in
milliseconds
Created with
gw.set_interval(function, delay)
GTimer
object that
identifies the timer, and can be stopped by invoking the
.stop()
method on that timerdef moving_square():
def step():
square.move(dx, dy)
if square.get_x() > 500:
timer.stop()
gw = GWindow(500, 200)
dx = 1
dy = 0
square = create_filled_rect(12, 100, 24, 24, "red")
gw.add(square)
timer = gw.set_interval(step, 20)