Lesson 18: Pixels taught by Parag Gupta

posted Mar 4, 2020, 1:49 PM by Attila Lengyel   [ updated Mar 4, 2020, 1:54 PM ]
#### create a drawing area filled with pixels whose colors can be changed by clicking
# initialize some stuff and create a function to draw a square
import turtle as t
goo = t.Turtle()
goo.speed(0)
def draw_square (side_len):
for i in range(4):
goo.forward(side_len)
goo.left(90)
### draw the boundary square starting from (-300, -300) with each side being 600
sq_x = -300 ## starting x coordinate of square
sq_y = -300 ## starting y coordinate of square
sq_sz = 600 ## length of each side of square
goo.penup()
goo.setposition(sq_x, sq_y)
goo.pendown()
draw_square(sq_sz)
### draw the grid of pixels
row_pixels = 8 #### adjust this to change resolution of pixels ####
pixel_width = sq_sz/row_pixels ## length of each side of the pixel
def draw_grid (row_pix):
for i in range (row_pix):
# draw a row of pixels
for j in range (row_pix):
draw_square(pixel_width)
goo.forward(pixel_width)
# go to the beginning of the next row of pixels
goo.penup()
goo.setposition(sq_x, goo.ycor()+pixel_width)
goo.pendown()
# comment the following out if you don't need the grid drawn since
# it can take some time to draw
draw_grid (row_pixels)
### create a list of colors, feel free to add to or remove from the list
colors = ['black', 'white', 'red', 'gray', 'orange', 'magenta', 'green', 'blue', 'yellow', 'brown']
num_colors = len(colors)
### Figure out the total number of pixels and create a list having an entry for
### each pixel which keeps track of the next color when that pixel is clicked. Initialize
### each entry to 0, which means the first color would be black. For example, if
### a particular entry had a value of 3, that pixel would be gray when clicked next. When
### a pixel gets clicked on, we would need to increment the value for that pixel in this list.
num_pixels = row_pixels * row_pixels
# initialize the list called pix_color to keep track of colors for each pixel
pix_color = []
# set each value in the list to initially be 0
for i in range(num_pixels) :
pix_color.append(0)
### Define a function to fill the clicked pixel with a color, where cl_x and cl_y
### are the x and y coordinates of the clicked location. The bottom left pixel is pixel 0.
### The one to the right is pixel 1 and so forth. If row_pixels was 4, the first pixel
### in the second row would be pixel 4 and the top right pixel would be pixel 15. We
### use the pixel numbers to keep track of what color each pixel will be when clicked next.
def fill_pixel(cl_x, cl_y):
# only fill the pixel if the click was within the big square
if ((cl_x > sq_x) and (cl_x < (sq_x + sq_sz)) and
(cl_y > sq_y) and (cl_y < (sq_y + sq_sz))) :
## figure out the pixel number
# calculate the x and y coordinates within our square and then divide by
# the pixel width to figure out which row and column the click was in
x = cl_x - sq_x
pix_col = int(x / pixel_width)
y = cl_y - sq_y
pix_row = int(y / pixel_width)
# calculate the actual pixel number from the row and column numbers
pix_num = pix_col + (pix_row * row_pixels)
print (pix_num)
# Calculate the start coordinates of the pixel to fill by using the column,
# row, pixel width, and square coordinates.
fill_pix_x = (pix_col * pixel_width) + sq_x
fill_pix_y = (pix_row * pixel_width) + sq_y
# Figure out the fill color by looking up the pixel number in the pix_color list
# and then using the value in that list to look up the actual color in the colors list.
fill_color = colors[pix_color[pix_num]]
goo.color(fill_color)
# Increment the value for that pixel in the pix_color list. Use the mod function so
# value goes back to 0 when we've reached the last color.
pix_color[pix_num] = (pix_color[pix_num] + 1) % num_colors
# fill the pixel with the fill color
goo.penup()
goo.setposition(fill_pix_x, fill_pix_y)
goo.begin_fill()
draw_square(pixel_width)
goo.end_fill()
## When the screen is clicked, fill the pixel, and wait for the next click.
screen = t.Screen()
screen.onscreenclick(fill_pixel)
screen.listen()


Comments