Color Control
Introduction:
- Welcome to BlockPyArt, a fun and interactive way to learn programming by creating colorful shapes and patterns on a screen. This tutorial will focus on controlling the color of your drawings. Remember: Always use the "start" block to activate the BlockPyArt console. If you do not start with the start block, you will encounter an error and the commands will not work properly.
Color Control Commands
- pencolor(color):
- Purpose: Sets the color of the pencil used for drawing lines.
Example:
pythonturtle.pencolor("red") # string turtle.forward(100) turtle.pencolor((0, 255, 0)) # RGB tuple turtle.forward(100) turtle.pencolor("#0000FF") # hexadecimal color code turtle.forward(100)- Exercise: Set the pencil color to blue, (255, 165, 0), and "#FF5733" respectively.
- fillcolor(color):
- Purpose: Sets the fill color for shapes.
Example:
pythonturtle.fillcolor("yellow") # string turtle.forward(100) turtle.fillcolor((255, 105, 180)) # RGB tuple turtle.forward(100) turtle.fillcolor("#4B0082") # hexadecimal color code turtle.forward(100)- Exercise: Set the fill color to green, (0, 128, 128), and "#FF1493" respectively.
- color(pencolor, fillcolor):
- Purpose: Sets both the pencil color and the fill color at the same time.
Example:
pythonturtle.color("purple", "pink") # strings turtle.forward(100) turtle.color((135, 206, 235), (255, 20, 147)) # RGB tuples turtle.forward(100) turtle.color("#FFD700", "#8A2BE2") # hexadecimal color codes turtle.forward(100)- Exercise: Set the pencil color to red and the fill color to yellow; set both to (0, 0, 0); and set them to "#FFFFFF" and "#000000" respectively.
Examples
- Drawing a Filled Circle:
Example:
pythonturtle.fillcolor("cyan") turtle.begin_fill() turtle.circle(50) turtle.end_fill()
- Drawing a Colored Square:
Example:
pythonturtle.pencolor("blue") turtle.fillcolor("lightblue") turtle.begin_fill() for _ in range(4): turtle.forward(100) turtle.right(90) turtle.end_fill()
Exercises
- Draw a Rainbow Circle:
Write a program to draw a circle with a gradient of colors.
pythoncolors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"] for color in colors: turtle.pencolor(color) turtle.circle(50) turtle.penup() turtle.goto(turtle.xcor() + 10, turtle.ycor() - 10) turtle.pendown()
- Draw a Pattern with Different Fill Colors:
Write a program to draw squares with different fill colors in a grid.
pythoncolors = ["red", "green", "blue", "yellow", "purple", "orange", "pink", "cyan"] for i in range(2): for j in range(4): turtle.pencolor("black") turtle.fillcolor(colors[i*4 + j]) turtle.begin_fill() turtle.goto(j*110, i*110) for _ in range(4): turtle.forward(100) turtle.right(90) turtle.end_fill() turtle.penup() turtle.goto(0, 0) turtle.pendown()
Summary:
- In this tutorial, we learned how to control the color of your drawings in BlockPyArt using the commands
pencolor(),fillcolor(), andcolor(). Remember to always use the start block to activate the BlockPyArt console. If you cannot start using the BlockPyArt blocks, there will be an error. Practice these commands and exercises to get comfortable with adding color to your drawings in BlockPyArt!