Level 1: Challenge 1 - IncrediCoders/Python1 GitHub Wiki

GitHubAvatar G_Look down2

Grafika Turtle added this page on March 14, 2023


Let's begin your first challenge for Level 1!

You are going to map a different route to the school, which will cross the bridge over the lake.

To find the Level 1 Challenge 1 code template, open the Level 1 folder, the Challenges folder, and then the Challenge 1 folder. You should already have the files downloaded onto your computer (see Load the IncrediCoders Files). Open the TurtleMapChallenge1.py file in Visual Studio Code to build the program by following along with my instructions below!

I broke these instructions down into a few sections:

Explaining the Code

On Line 2, the code imports the init.py file (which grabs the files from your folder):

from init import *

On Line 3, import turtle brings in the turtle module, which includes a lot of code you can use (called methods). For example, you can tell the turtle which direction to go in (and how far to go), you can set the color and shape of the turtle, etc. I already told you about methods in the book. Let me explain what a module is...

Module

A module is a Python file that holds useful code in it, just like how a toolbox is full of useful tools. A module has a specific purpose, like complex math functions, or allowing you to control a turtle. For example, in turtle.forward(125), the module is turtle.

On Line 6, SCREEN = turtle.Screen() creates a new window for your turtle program.

On Line 7, SCREEN.title("Mapster Spacebook") sets the title name ("Mapster Spacebook") for the window that we created in the last line of code.

(ED TO DO: to add an image of what the window title ("Mapster Spacebook") looks like.)

On Line 8, SCREEN.setup(1280, 800) sets the size of the window that we made in the code from Line 6, so that it is 1280 pixels wide and 800 pixels tall.

On Line 9, SCREEN.bgpic(get_file("Assets/Background.gif")) sets up our background image on the window. This image shows the map of the roads that you'll use, to move the turtle from my house to the school.

To review, here are Lines 6-9, which set up the window, set the title of the window, set the size of the window, and put the background image in the window:

SCREEN = turtle.Screen()

SCREEN.title("Mapster Spacebook")

SCREEN.setup(1280, 800)

SCREEN.bgpic(get_file("Assets/Background.gif"))

On Line 6, where it says turtle.Screen(), turtle is the module and .Screen() is the method.

Method

A module (like turtle), Python file, or class can hold methods, which are lines of code that we can reuse to tell our program what to do. For example, on Line 12, in the code turtle.shape("turtle"), .shape() is the method we are calling from the turtle module.

Moving the Turtle

On Line 12, turtle.shape("turtle") creates a new turtle shape on the screen, by using the code from the imported turtle class. That turtle shape is going to represent Monty Python's car, showing the path that he needs to drive, in order to get to the IncrediCoders Academy.

(If we didn't set the turtle.shape() parameter to "turtle", then the car would appear as an arrow instead. We could have set the shape as a car, which might make more sense, but because we're teaching turtle graphics, and I'm a turtle, we set the shape as a turtle instead!)

Parameter

A value that is passed into your method. For example, when we run turtle.shape("turtle"), we send the "turtle" parameter to the shape method. This is what tells the turtle object/module to change the appearance of the turtle.

On Line 13, turtle.color("green") changes the color of the turtle (that we created in the code from Line 12) to green, by using the .color() method from the imported turtle module.

On Line 16, turtle.penup() lifts the pen off the window so that while it is moved to its starting position, it does not draw any lines on the window.

The way that these lines of codes are being written is called the syntax. Let me go into more detail about what syntax is...

Syntax

Syntax is a set and order of words in code, to give the computer instructions it understands. If you break the rules of the syntax, then your code shows you errors. For example, if we wrote "turtle.PENup(up);" instead of turtle.penup(), then Python would produce errors, to let us know to use the right casing (lowercase), to not write anything in the parentheses, and to not use punctuation (the semicolon) at the end of the line of code.

On Line 17, turtle.setx(460) sets the x position of the turtle character (that you created in the code, back on Line 12). The turtle is placed at the x location of 460 pixels. Let me describe what a pixel is so you can understand more about what moving the turtle does:

Pixel

A pixel, also known as a picture element, is the smallest controllable element of a picture on a screen. When we say an image is the resolution of 460 by 500, we are counting the width and length of the image in pixels. So, turtle.setx(460), sets the turtle 460 of these pixels to the right of the turtle's starting x position (which is 0).

On Line 18, turtle.sety(-275) sets the y position of the turtle to -275 pixels. That means that the turtle is lowered 275 pixels on your screen (from the 0 y position), before we set it down.

On Line 19, turtle.pendown() puts the pen back down on the window, so that when it is moved, the turtle will create a line from its current location to its next location.

To review, here are Lines 12-13, which set the turtle shape and color:

turtle.shape("turtle")

turtle.color("green")

And here are Lines 16-19, which move the turtle sprite into its starting position:

turtle.penup()

turtle.setx(460)

turtle.sety(-275)

turtle.pendown()

Let me go into more detail about what a sprite is:

Sprite

Your turtle character is a sprite. A sprite is a stand-alone on-screen object in a computer game. It can be manipulated in different ways. One way is by changing the sprite's location (its x and y position), like you did on Lines 17 and 18: turtle.setx(460) and turtle.sety(-275). However, there are other ways to make changes to a sprite, such as changing its color or adding other animation to it.

On Line 21, #TODO: Uncomment these lines by removing the hashtags explains how to uncomment a line... you remove the hashtag in front of the code. By removing the hashtag, you include that line of code to be read when the program runs. When a line of code is commented out (a hashtag is in the front of the line), the program will not read that line of code. Let me go into more detail about what a comment is:

Comment

A comment is a note that you put in your code. If you add a hashtag symbol (#), then anything you write after that (on the line of code) becomes a comment. You can have a whole line act as a comment, or you can put a comment at the end of a line of code, to explain what that code does. In our template file, we also use comments to describe what code you should write in the line below it. A comment will not run with the program, but it will help you organize your code. Or it will help someone else, who did not work with you on the program, understand what your program does.

On Line 22, remove the hashtag to uncomment the code, turtle.left(180). When the program runs, this line of code will now rotate the turtle character 180 degrees to the left from its current position. The turtle is now facing left.

On Line 23, uncomment the code, turtle.forward(125). When the program runs, this line of code will move the turtle character forward 125 pixels. Since the turtle is facing left, this will change the current x position. The x direction on a graph is the direction that goes from left to right (horizontal). Here is the image again that I sent to you in my text message:

As you can see in the above image, the x coordinate determines where your turtle is from the left to right axis. If the x coordinate is a positive number, then the turtle is to the right of the middle of the window. If it is a negative number, the turtle is on the left side. Likewise, if y is set to a positive number, the turtle is higher on the screen, and if y is set to a negative number, the turtle is lower.

Copy the Code

The directions on Line 25 states, #TODO: Turn right 90 degrees and move forward 105 steps. We will code two separate movements, with one line of code for each movement.

The first movement, Turn right 90 degrees, can be represented by the code turtle.right(90). Write this code on Line 26, uncommented.

The second movement, Move forward 105 steps, can be represented by the code turtle.forward(105). Write this code on Line 27, uncommented.

The directions on Line 27 state, #TODO: Turn right 90 degrees and move forward 185 steps. This time, we will again write two lines of code. Use the turtle.right method to have the turtle turn 90 degrees to the right, and then on the next line, use the turtle.forward() method to move forward.

turtle.left(90) turtle.forward(435) turtle.left(90) turtle.forward(105)

Leave Line 28 blank with no code.

On Line 29, #TODO: Turn right 90 degrees and move forward 185 steps, follow the same process as you did for the code on Line 25. For each movement that you have, create a new line. For example, to turn left 45 degrees, you would write the code, turtle.left(45) how can you apply this to the code that you need to write for this statement? Write the answer on Line 30. After that, create a new line (Line 31) to write the second part of the statement. If turtle.forward(-50) moves the turtle character back 50 steps, what would the code be for the second statement?

[Under the comment #TODO: Turn left and move to the end of the road, tell the turtle to do the following (use even more vague language... "Turn right. Go forward 195 steps. Turn right again. Go forward 80 steps.."): turtle.right(90) turtle.forward(195) turtle.right(90) turtle.forward(80) turtle.left(90) turtle.forward(170) ]

Leave Line 32 blank with no code.

Write Your Own Code

On Line 33, #TODO: Turn left and move to the end of the road, your first step is to locate the end of the road. Then, in order to get there, you have to turn left 90 degrees, then move forward 385 pixels from the current position of your turtle character. Write each movement on a new line.

Leave Line 36 blank with no code.

On Line 37, #TODO: Turn left and move to the intersection. your first step is to locate the intersection. Then, in order to get there, you have to turn left 90 degrees, then move forward 65 pixels from the current position of your turtle character. Write each movement on a new line.

Leave Line 40 blank with no code.

On Line 41, #TODO: Write more lines here to navigate the alternate route to school , it tells you the goal of the code you are about to write which is to use methods to create an alternate route to the school.

On Line 42, #Cross the bridge and stay on the road!, it is giving you a hint to go over the bridge, and also setting the rule that you must stay on the road at all times.

On Line 43, you should start with the first movement to accomplish the goal of this challenge. A movement is turning a certain direction than going forward a certain number of pixels. Create a blank line in between each movement.

For the first movement, you want to turn the turtle character to the right 90 degrees then move forward 105 pixels.

For the second movement, you want to turn the turtle character to the left 90 degrees, then move it forward 435 pixels.

For the third movement, you want to turn the turtle character to the left 90 degrees, then move it forward 105 pixels.

For the fourth movement, you want to turn the turtle character to the right 90 degrees, then move it forward 195 pixels.

For the fifth movement, you want to turn the turtle character to the right 90 degrees, then move it forward 80 pixels.

For the sixth movement, you want to turn the turtle character to the left 90 degrees, then move it forward 170 pixels.

On Line 62, #This line stops the window from closing once we make it to the end, is an example of a good use of a comment to describe what the next line of code does.

On Line 63, turtle.done(), as described in the comment on Line 62, stops the window from closing once we make it to the end.

You did it! You planned the alternate route to school!"

The Final Code

I included a solution file for Level 1, Challenge 1. This file has all the code filled in, so if you run into any issues with your code (for example, if it doesn't compile/run, or if something isn't working correctly in your program), then you can take a look at the final code file to see what you did differently:

IMPORTANT: Please don't cheat yourself! Finish the game first!

Next Steps

Take the Next Challenge!

Next, you can take on Level 1, Challenge 2! Use loops to go back and forth between my house and the school, five times. (Paul's father, Monty, has to keep going back because Paul keeps forgetting his school supplies... at my house, apparently.)

More Level 1 Resources

In addition to this page, we also have Level 1 Help (for the main game, in the book), Online Articles, a Learning Quiz, an Unplugged Activity, and a Rewards article:

  • Level 1: Help - This page helps you complete the instructions in the book, in case you get stuck.

  • Level 1: Online Articles - I made you a list of different web pages I found, which will help you learn more about creating Turtle Graphics.

  • Level 1: Learning Quiz - I wrote some questions in case you want to quiz yourself about what you learned. Or you can teach others and quiz them!

  • Level 1: Unplugged Activity - I wrote this page with more details than what you saw in the book. In this game, you'll have one person act as the Turtle, and one person is the Programmer!

  • Level 1: Rewards - If you completed the Turtle Map project that we talked about in the car, then I set up this page to act as a reward. You can see some illustrations of me and learn more about who I am! You'll also find the Turtle Award digital download, to show off your accomplishment!

Level 2

After you're completely done with Level 1 (did you do Challenge 2?), then it's time to move on to Level 2! While you read through Level 2 in your book, you can check out the resources from Mrs. Scratcher, as she teaches you how to build the Class Discussion program:

I hope you had fun making this Turtle Maps alternate route, to get to IncrediCoders Academy! I'll see you at the academy!

-- Grafika Turtle

⚠️ **GitHub.com Fallback** ⚠️