Click

Calvin (Deutschbein)

W7Wed: 09 Oct

Announcements

  • Finally got to look at midterms, working on getting out some feedback. I can do grade checks by request until I get every thing out.
  • Problem Set 4: Graphics for Monday, 14 October, 11:59 PM.
  • Sections today and tomorrow.
  • EXTRA CREDIT: Post a selfie in the Discord/in a Discord DM to me while at the "SCIS Open House" for
    • +5% on your Midterm score.
    • Thursday 11:30-1:00 in the Ford 101 in the Discord
    • 'SCIS Open House primarily directed at first year students who are interested in learning more about computer science/data science as majors.'
    • Let me know if you consent to have the selfie used in promotion materials or not.

Today

  • Graphics Window
    • Events
    • State/attributes/fields

Boxy Click

GRect(x,y,w,h) ;
    x = y =
    w = h =

Boxy Click

  • What does each part do? from pgl import * w = 400 # width def do_graphics(): gw = GWindow(w,w) rect = GRect(150,50,200,100) rect.set_filled(True) rect.set_color("Blue") gw.add(rect) do_graphics()

Boxy Click

  • Why don't we just make a "rectangle" function... def do_graphics(): def my_rect(x,y,w,h,color): rect = GRect(x,y,w,h) rect.set_filled(True) rect.set_color(color) gw.add(rect) gw = GWindow(400,400) my_rect(000,000,200,200, "Blue") my_rect(100,100,200,200, "Red") do_graphics()

Factoring

  • We term the use a function like "my_rect" to be "factoring".
    • In math class, 2x2 + 4x is equal to x(2x + 4).
    • We factor out what happens to x.
    • In this class, my_rect factors out what happens to 'rect' def my_rect(x,y,w,h,color): rect = GRect(x,y,w,h) rect.set_filled(True) rect.set_color(color) gw.add(rect)

Today

  • Graphics Window
    • Events
    • State/attributes/fields

Events

Events

  • To my knowledge, there is nowhere in the pgl documentation that shows how to make an event listener for a click event.
  • To my knowledge, this is the minimal possible example. from pgl import * def click_func(e): # 'e' is a "MouseEvent" print(e) # let's look at 'e' gw = GWindow(400,400) gw.add_event_listener("click",click_func)
  • It doesn't print anything particularly helpful: <pgl.GMouseEvent object at 0x000001FA27BDCBC0>

Events

  • The "MouseEvent" thing has methods, like string.replace() or gw.add()
  • They are "get_x" and "get_y".
  • Let's use them. from pgl import * def click_func(e): # 'e' is a "MouseEvent" print('x = ', e.get_x()) print('y = ', e.get_y()) gw = GWindow(400,400) gw.add_event_listener("click",click_func)
  • This prints the x,y location where the click occurred: x = 143 y = 148

Boxy Click


Events

  • Instead of printing the location, let's add a small pink rectangle.
  • Remember to import and to use "my_rect" from earlier slides! def click_func(e): x = e.get_x() y = e.get_y() my_rect(x,y,50,50,'pink') gw = GWindow(400,400) gw.add_event_listener("click",click_func)

Events

  • We can 'center' the rectangle on the cursor by changing the x and y values by half the size.
  • Remember to import and to use "my_rect" from earlier slides! def click_func(e): x = e.get_x() y = e.get_y() my_rect(x-25,y-25,50,50,'pink') gw = GWindow(400,400) gw.add_event_listener("click",click_func)

Today

  • Graphics Window
    • ✓ Events
    • State/attributes/fields

State

  • 'State' is a technical term for "information that stays around after a function returns".
  • In computing, it is usually maintained by placing values, like a number or a GRect, inside of objects, like a GWindow.
    • This is an "attribute" or "field" of the 'gw', an 'object'
    • Don't worry, we'll practice.
  • Things like string.replace() are a special type of attribute, called a method.
    • A method is a function and an attribute - a function inside of some object.
  • Everything in Python is an object, strings/bools/integers/graphics/rectangles/clicks.

State

  • Say we wish to only display the most recent box:
    • We create something called "box" inside the GWindow - "gw.box"
    • We use "gw.box", rather than "rect", to store the return value of GRect.
    • We change the location of the box rather than making a new box.
      • We use the new ".set_location(x,y)" to do this.
      • Overall, will look like: gw.box.set_location(x,y)
    • To my knowledge, this is the minimal possible example.

Events

    from pgl import * # same click func def click_func(e): x = e.get_x() y = e.get_y() gw.box.set_location(x,y) gw = GWindow(400,400) # same as my rect, but on "gw.box" gw.box = GRect(400,400,50,50) gw.box.set_filled(True) gw.box.set_color('pink') gw.add(gw.box) gw.add_event_listener("click",click_func)

Announcements

  • Grade checks by request until I get every thing out.
  • Problem Set 4: Graphics for Monday, 14 October, 11:59 PM.
  • Sections today and tomorrow.
  • EXTRA CREDIT: Post a selfie in the Discord/in a Discord DM to me while at the "SCIS Open House" for
    • +5% on your Midterm score.
    • Thursday 11:30-1:00 in the Ford 101 in the Discord
    • 'SCIS Open House primarily directed at first year students who are interested in learning more about computer science/data science as majors.'
    • Let me know if you consent to have the selfie used in promotion materials or not.