MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / drawFromData

Function drawFromData

src/gamesbyexample/monalisa.py:32–57  ·  view source on GitHub ↗
(image_data, image_width, image_height, left, top, pixel_size)

Source from the content-addressed store, hash-verified

30
31
32def drawFromData(image_data, image_width, image_height, left, top, pixel_size):
33 turtle.penup()
34
35 inBinary = bin(int(image_data, 16))
36
37 # Remove '0b' from the start of the hex number:
38 inBinary = inBinary[2:]
39
40 # Add leading zeros to the binary number, if needed:
41 inBinary = inBinary.rjust(image_width * image_height, '0')
42
43 for y in range(image_height):
44 for x in range(image_width):
45 turtle.goto(left + (x * pixel_size), top - (y * pixel_size))
46
47 if inBinary[y * image_width + x] == '1': # (!) Try switching this to '0'.
48 turtle.begin_fill()
49 turtle.setheading(0)
50 turtle.forward(pixel_size) # draw top of the box
51 turtle.right(90)
52 turtle.forward(pixel_size) # draw right edge of the box
53 turtle.right(90)
54 turtle.forward(pixel_size) # draw bottom of the box
55 turtle.right(90)
56 turtle.forward(pixel_size) # draw left edge of the box
57 turtle.end_fill() # fill in the box
58
59
60try:

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected