Screen and Coordinate System Understanding the coordinate system. Positioning the turtle using coordinates. Setting up the screen size and background color.
Understanding the Coordinate System
In turtle graphics, the coordinate system is based on a Cartesian plane with the following characteristics:
The center of the screen is the origin, which has coordinates (0, 0).
The x-axis extends horizontally, with positive values to the right and negative values to the left.
The y-axis extends vertically, with positive values going up and negative values going down.
Here's a visual representation of the coordinate system:
yaml
y
|
|
-x --------- +x
|
|
-y
Positioning the Turtle Using Coordinates
To position the turtle at a specific coordinate, you can use the goto(x, y) function. For example:
goto(0, 0) places the turtle at the center.
goto(100, 100) places the turtle 100 units to the right and 100 units up from the center.
goto(-100, -100) places the turtle 100 units to the left and 100 units down from the center.
Here's an example of how to position the turtle using coordinates:
Python Code
import turtle
# Set up the screen
screen = turtle.Screen()
# Create a turtle
t = turtle.Turtle()
# Move the turtle to a specific coordinate
t.goto(100, 100)
t.write("I'm here!", align="center")
t.goto(-100, -100)
t.write("Now I'm here!", align="center")
# Hide the turtle and display the result
t.hideturtle()
turtle.done()
Setting Up the Screen Size and Background Color
You can set up the screen size and background color using the setup(width, height) and bgcolor(color) methods, respectively.
Here's how you can do it:
Python Code
import turtle
# Set up the screen
screen = turtle.Screen()
screen.setup(width=800, height=600) # Set the screen size to 800x600 pixels
screen.bgcolor("lightblue") # Set the background color to light blue
# Create a turtle
t = turtle.Turtle()
# Move the turtle to a specific coordinate
t.goto(100, 100)
t.write("I'm here!", align="center")
t.goto(-100, -100)
t.write("Now I'm here!", align="center")
# Hide the turtle and display the result
t.hideturtle()
turtle.done()
In this example:
screen.setup(width=800, height=600) sets the screen size to 800 pixels wide and 600 pixels high.
screen.bgcolor("lightblue") sets the background color of the screen to light blue.
By understanding the coordinate system, positioning the turtle using coordinates, and setting up the screen size and background color, you can create various drawings and animations using turtle graphics in Python.
Top of Form