pgl

Calvin (Deutschbein)

W5Fri: 27 Sep


Announcements

  • By next Monday 11:59 PM: "Project 1: Wordle"
    • By Fri: Random word
    • By Sat: Change rows
    • By Sun: Color keys.
  • Midterm next Friday.
    • Practice Exams Posted

Today

  • Aside on Wordle
  • More graphics
  • More on functions

redact

    def redact(word:str, secret:str) -> str: for i in range(len(word)): if word[i] == secret[i]: word = word[:i] + '*' + word[i+1:] # or ' ' etc DEBUG and print('Redact '+secret[i]+' at '+str(i)) return word
  • Imagine, e.g. "sassy" and "glass" >>> DEBUG = True >>> redact('glass','sassy') Redact s at 3 'gla*s'

censor

  • May also want to keep secret whether letters are in the secret at all! def censor_letter(letter:str, word:str) -> str: # remove ONLY THE FIRST case of letter in word for index in range(len(word)): if word[index] == letter: return word[:index] + '-' + word[index+1:] return word # if letter isn't present
  • Imagine, e.g. "s" and "sas*y" >>> censor_letter('s', 'sas*y') '-as*y'

censor

  • Perhaps we want to censor every letter in some secret word. def censor(word:str, secret:str) -> str: for letter in secret: word = censor_letter(letter, word) return word
  • Imagine, e.g. "sas*y" and "claps" >>> censor("sas*y", "claps") '--s*y'

censor

  • If we want to debug, we need to use local variables... def censor(word:str, secret:str) -> str: for letter in secret: censored = censor_letter(letter, word) if censored != word: DEBUG and print("Censored " + letter) word = censored return word
  • Imagine, e.g. "sas*y" and "claps" Censored a Censored s '--s*y'

Blue Rectangle!

from pgl import *
gw = GWindow(500, 200) # like WordleGWindow
rect = GRect(150,50,0,0) #  answer = "blimp"
rect.set_color("Blue") # answer.upper()
rect.set_filled(True) 
gw.add(rect) # like word_to_row(answer)

The Coordinate System

image/svg+xml blue_rectangle (0,0) (150,50) 200 px 100 px
  • Positions and distances on the screen are measured in terms of pixels
    • Origin is in the upper left instead of lower left
    • Vertical values increase as you move downwards

Other Simple Objects

Functions to create simple geometric objects:

  • Rectangles/Squares + Circles/Ovals!
    • GRect( x, y, width, height )
    • GOval( x, y, width, height )
    • Creates a rectangle whose upper left corner is at (x,y) of the specified size
  • Lines!
    • GLine( x1, y1, x2, y2 )
    • Creates a line extending from (x1,y1) to (x2,y2)

For Wordle

  • Lower numbered rows are above higher numbered rows.
  • Lower numbered columns are leftward of higher numbered columns.

    (0,0)(0,1)(0,2)(0,3)(0,4)
    (1,0)(1,1)(1,2)(1,3)(1,4)
    (2,0)(2,1)(2,2)(2,3)(2,4)
    (3,0)(3,1)(3,2)(3,3)(3,4)
    (4,0)(4,1)(4,2)(4,3)(4,4)
    (5,0)(5,1)(5,2)(5,3)(5,4)
  • gw.set_square_letter(1, 3, '$') # example
    $

GWindow

  • Akin to Wordle: gw = GWindow(width, height)
  • You have several more operations:
    gw.add(object) Adds an object to the window
    gw.add(object, x, y) Adds an object to the window after moving it to (x,y)
    gw.remove(object) Removes an object from the window
    gw.get_width() Returns the width of the graphics window in pixels
    gw.get_height() Returns the height of the graphics window in pixels

GObject

  • The following are used on things like rectangles, imagine:' object = GRect(150,50,0,0) # like saying answer = "blimp"
  • object.get_x() Returns the x coordinate of this object
    object.get_y() Returns the y coordinate of this object
    object.get_width() Returns the width of this object
    object.get_height() Returns the height of this object
    object.set_color(color) Sets the color of the object to the specified color
  • All coordinates and distances are measured in pixels

GFillableObject

  • Some things, like Wordle letterboxes, can be filled in with a color. gw.set_square_color(row, col, color)
  • Wordle squares are a lot like GRects... object = GRect(150,50,0,0)
  • object.set_filled(bool) Sets the fill state of the object
    object.set_fill_color(color) Sets the color to be used to fill the interior, otherwise same as the outer line
    object.get_fill_color() Gets the current color used to display the object interior
    object.is_filled() Returns True or False depending on whether the object is currently filled


gw = GWindow(400, 400) head = GOval(20, 20, 360, 360) head.set_fill_color("yellow") head.set_filled(True) gw.add(head) reye = GOval(110, 100, 40, 40) reye.set_filled(True) gw.add(reye) leye = GOval(250, 100, 40, 40) leye.set_filled(True) gw.add(leye) mouth = GLine(150,250,250,250) mouth.set_line_width(5) gw.add(mouth)

Announcements

  • By next Monday 11:59 PM: "Project 1: Wordle"
    • By Tue: Display a word
    • By Wed: Check a guess
    • By Thr: Color letters
    • By Fri: Random word
    • By Sat: Change rows
    • By Sun: Color keys.