Code

Light the First Pixel

Every picture on a screen is made of tiny coloured squares called pixels. PyQuest has a pixel canvas you can paint with Python!

from pyquest import Canvas

c = Canvas(8, 8, background="night")
c.set(3, 1, "gold")
c.rect(2, 2, 4, 3, "orange")
c.show()
  • Canvas(8, 8, ...) makes a grid 8 pixels wide and 8 tall.
  • Every pixel has an address (x, y). x counts columns from the left. y counts rows from the top. Both start at 0, so the top-left pixel is (0, 0).
  • c.set(x, y, color) paints one pixel.
  • c.rect(x, y, width, height, color) paints a filled rectangle whose top-left corner is (x, y).
  • c.show() puts the picture on screen.

Colours can be names like "gold", "orange", "brown", "sky" and "grass", or hex codes like "#ff8800".

Pip tip: Painting the same pixel twice is fine. The last colour wins, just like real paint.

Assignment

Paint the Guild's first lantern on the 8 × 8 night canvas c:

  1. Paint two "gold" flame pixels at (3, 1) and (4, 1) with c.set.
  2. Paint the "orange" body: c.rect(2, 2, 4, 4, "orange").
  3. Paint a "brown" base with c.rect: top-left at (1, 6), 6 wide and 1 tall.
  4. Keep the c.show() at the end.