X
Create a Python program that defines the function draw_x(center_x: float, center_y: float, size: float) -> None
. The function should use turtle graphics to draw an X centered on the point (center_x, center_y). The X should be size wide and size tall.
Example
import turtle
def main() -> None:
0.0, 0.0, 100.0)
draw_x(return None
main()
import turtle
def main() -> None:
0.0, 0.0, 50.0)
draw_x(25.0, 0.0, 50.0)
draw_x(-25.0, 0.0, 50.0)
draw_x(return None
main()
Hint
- If you use the
turtle.forward
function, you will need to use trigonometry to compute the length of the X’s lines. The function is a bit easier to create if you instead use theturtle.setposition
function repeatedly. - To use the
turtle.setposition
function, you will need to compute the coordinates of the four endpoints of the X. Do this by adding or subtracting half of the X’s size to its center x and y. - To draw a line, pick up the pen, set the position to a corner, put down the pen, and set the position to the opposite corner.
Pixel Art
Early computers had such a low resolution that you could see the display’s colored light squares called pixels. Modern computers are capable of displaying smooth, detailed graphics. But the blocky pixel art style remains popular. Creating retro pixelated visuals requires drawing filled-in squares. This is an excellent use of functions.
Details
Create a Python program that defines the function fill_square(center_x: float, center_y: float, size: float, color: str) -> None
. The function should use turtle graphics to draw a square centered on the point (center_x, center_y). The square should be size wide and size tall. And finally, it should be filled with color.
Example
import turtle
-> None:
main() 0.0, 0.0, 100.0, 'red')
fill_square(return None
main()
import turtle
-> None:
main() -25.0, 25.0, 50.0, 'red')
fill_square(25.0, 25.0, 50.0, 'green')
fill_square(-25.0, -25.0, 50.0, 'blue')
fill_square(25.0, -25.0, 50.0, 'yellow')
fill_square(return None
main()
import turtle
-> None:
main() float
size: = 10.0
size 0)
turtle.speed(
* -4.5, size * 4.5, size, '#1EA049')
fill_square(size * -3.5, size * 4.5, size, '#97CF8A')
fill_square(size * -2.5, size * 4.5, size, '#1EA049')
fill_square(size * -1.5, size * 4.5, size, '#47AF48')
fill_square(size * -0.5, size * 4.5, size, '#8FCB8D')
fill_square(size * 0.5, size * 4.5, size, '#C2DCBF')
fill_square(size * 1.5, size * 4.5, size, '#88C86D')
fill_square(size * 2.5, size * 4.5, size, '#78C364')
fill_square(size * 3.5, size * 4.5, size, '#459243')
fill_square(size * 4.5, size * 4.5, size, '#98CF90')
fill_square(size * -4.5, size * 3.5, size, '#60BD56')
fill_square(size * -3.5, size * 3.5, size, '#259044')
fill_square(size * -2.5, size * 3.5, size, '#60BD56')
fill_square(size * -1.5, size * 3.5, size, '#42AE49')
fill_square(size * -0.5, size * 3.5, size, '#6FC160')
fill_square(size * 0.5, size * 3.5, size, '#459243')
fill_square(size * 1.5, size * 3.5, size, '#78C35D')
fill_square(size * 2.5, size * 3.5, size, '#5CBB4F')
fill_square(size * 3.5, size * 3.5, size, '#BEDDBB')
fill_square(size * 4.5, size * 3.5, size, '#259044')
fill_square(size * -4.5, size * 2.5, size, '#8FCB7F')
fill_square(size * -3.5, size * 2.5, size, '#46B749')
fill_square(size * -2.5, size * 2.5, size, '#040707')
fill_square(size * -1.5, size * 2.5, size, '#040707')
fill_square(size * -0.5, size * 2.5, size, '#8FCB7F')
fill_square(size * 0.5, size * 2.5, size, '#4D9C49')
fill_square(size * 1.5, size * 2.5, size, '#040707')
fill_square(size * 2.5, size * 2.5, size, '#040707')
fill_square(size * 3.5, size * 2.5, size, '#C8E3C4')
fill_square(size * 4.5, size * 2.5, size, '#46B749')
fill_square(size * -4.5, size * 1.5, size, '#6ABE55')
fill_square(size * -3.5, size * 1.5, size, '#5ABA47')
fill_square(size * -2.5, size * 1.5, size, '#040707')
fill_square(size * -1.5, size * 1.5, size, '#040707')
fill_square(size * -0.5, size * 1.5, size, '#6ABE55')
fill_square(size * 0.5, size * 1.5, size, '#43AF48')
fill_square(size * 1.5, size * 1.5, size, '#040707')
fill_square(size * 2.5, size * 1.5, size, '#040707')
fill_square(size * 3.5, size * 1.5, size, '#77C363')
fill_square(size * 4.5, size * 1.5, size, '#5ABA47')
fill_square(size * -4.5, size * 0.5, size, '#5BB34F')
fill_square(size * -3.5, size * 0.5, size, '#86C877')
fill_square(size * -2.5, size * 0.5, size, '#5BB34F')
fill_square(size * -1.5, size * 0.5, size, '#8ECB80')
fill_square(size * -0.5, size * 0.5, size, '#040707')
fill_square(size * 0.5, size * 0.5, size, '#040707')
fill_square(size * 1.5, size * 0.5, size, '#B1D8AB')
fill_square(size * 2.5, size * 0.5, size, '#3E8D42')
fill_square(size * 3.5, size * 0.5, size, '#68BD4E')
fill_square(size * 4.5, size * 0.5, size, '#86C877')
fill_square(size * -4.5, size * -0.5, size, '#479443')
fill_square(size * -3.5, size * -0.5, size, '#97CF8C')
fill_square(size * -2.5, size * -0.5, size, '#479443')
fill_square(size * -1.5, size * -0.5, size, '#040707')
fill_square(size * -0.5, size * -0.5, size, '#040707')
fill_square(size * 0.5, size * -0.5, size, '#040707')
fill_square(size * 1.5, size * -0.5, size, '#040707')
fill_square(size * 2.5, size * -0.5, size, '#6FC053')
fill_square(size * 3.5, size * -0.5, size, '#ACD7A5')
fill_square(size * 4.5, size * -0.5, size, '#B7DBA8')
fill_square(size * -4.5, size * -1.5, size, '#5EA459')
fill_square(size * -3.5, size * -1.5, size, '#B7DBA8')
fill_square(size * -2.5, size * -1.5, size, '#5EA459')
fill_square(size * -1.5, size * -1.5, size, '#040707')
fill_square(size * -0.5, size * -1.5, size, '#040707')
fill_square(size * 0.5, size * -1.5, size, '#040707')
fill_square(size * 1.5, size * -1.5, size, '#040707')
fill_square(size * 2.5, size * -1.5, size, '#58B94E')
fill_square(size * 3.5, size * -1.5, size, '#4A9A45')
fill_square(size * 4.5, size * -1.5, size, '#B7DBA8')
fill_square(size * -4.5, size * -2.5, size, '#84C76D')
fill_square(size * -3.5, size * -2.5, size, '#6FC053')
fill_square(size * -2.5, size * -2.5, size, '#86C76C')
fill_square(size * -1.5, size * -2.5, size, '#040707')
fill_square(size * -0.5, size * -2.5, size, '#8DCB7C')
fill_square(size * 0.5, size * -2.5, size, '#A2D398')
fill_square(size * 1.5, size * -2.5, size, '#040707')
fill_square(size * 2.5, size * -2.5, size, '#2E8B42')
fill_square(size * 3.5, size * -2.5, size, '#6BBF56')
fill_square(size * 4.5, size * -2.5, size, '#6DBF53')
fill_square(size * -4.5, size * -3.5, size, '#AFD8A0')
fill_square(size * -3.5, size * -3.5, size, '#58B247')
fill_square(size * -2.5, size * -3.5, size, '#AED8A0')
fill_square(size * -1.5, size * -3.5, size, '#6DC05C')
fill_square(size * -0.5, size * -3.5, size, '#159B48')
fill_square(size * 0.5, size * -3.5, size, '#3CAF49')
fill_square(size * 1.5, size * -3.5, size, '#68BD4E')
fill_square(size * 2.5, size * -3.5, size, '#58BA47')
fill_square(size * 3.5, size * -3.5, size, '#81C673')
fill_square(size * 4.5, size * -3.5, size, '#56B247')
fill_square(size * -4.5, size * -4.5, size, '#91CC84')
fill_square(size * -3.5, size * -4.5, size, '#60BC51')
fill_square(size * -2.5, size * -4.5, size, '#91CC84')
fill_square(size * -1.5, size * -4.5, size, '#389143')
fill_square(size * -0.5, size * -4.5, size, '#159B48')
fill_square(size * 0.5, size * -4.5, size, '#1EA149')
fill_square(size * 1.5, size * -4.5, size, '#63BD50')
fill_square(size * 2.5, size * -4.5, size, '#40AF49')
fill_square(size * 3.5, size * -4.5, size, '#9ED18E')
fill_square(size * 4.5, size * -4.5, size, '#60BC51')
fill_square(size
turtle.penup()
turtle.forward(turtle.window_width())return None
main()
Hint
Use the turtle.setposition function to move the turtle to one of the corners of the square to draw.
But don’t move to the point (center_x, center_y), that’s the center of the square.
The turtle should move to a corner. Compute the coordinates of a corner by adding half the square size to the center x and y.
Drawing the square is straightforward. You just need to go forward and turn 4 times. A loop makes this easier.
Use the
fillcolor
function to set the fill color, and thebegin_fill
andend_fill
functions to actually fill the square.
Challenge
Use the function to create your own pixel art. Use an Image Search for some suggestions.