Categories
BLOG

clicks in ** seoncds scratch

Clicks in ** seoncds scratch

You’ve probably played a maze game before, but have you ever tried making one? Mazes can be tricky to complete, but they’re easy to program. In this chapter, you’ll create a game that lets the player guide a cat through a maze to reach its goal—a delicious apple! You’ll learn how to move the cat with the keyboard and how to block its progress with walls.

Before you start coding, take a look at the final program. Go to https://www.nostarch.com/scratchplayground/ and play the game.

SKETCH OUT THE DESIGN

First, draw what you want the game to look like on paper. With some planning, you can make your maze game a-mazeing. (I never apologize for my puns.) My sketch for the maze game looks like the following figure.

If you want to save time, you can start from the skeleton project file, named maze-skeleton.sb2, in the resources ZIP file. Go to https://www.nostarch.com/scratchplayground/ and download the ZIP file to your computer by right-clicking the link and selecting Save link as or Save target as. Extract all the files from the ZIP file. The skeleton project file has all the sprites already loaded, so you’ll only need to drag the code blocks into each sprite. Click File Upload from your computer in the Scratch editor to load the maze-skeleton.sb2 file.

Even if you don’t use the skeleton project, you should download the ZIP file from the website. This file contains the maze images you’ll use in this chapter.

If you want to create everything on your own, click File New to start a new Scratch project. In the text field in the upper left, rename the project from Untitled to Maze Runner.

MAKE THE CAT WALK AROUND

In the Maze Runner game, the player will control the cat sprite. In Part A, you’ll set up the code to control the cat with the arrow keys on the keyboard.

EXPLORE: X- AND Y-COORDINATES

To make the cat move around the Stage, you need to use coordinates. Coordinates are numbers that represent an exact location. The x-coordinate (also called x position) is a number that represents how far left or right a sprite is on the Stage. In other words, x is the sprite’s horizontal position. The y-coordinate (also called y position) is a number that represents how far up or down a sprite is on the Stage. The y-coordinate is a sprite’s vertical position.

Used together, x- and y-coordinates indicate a sprite’s precise location on the Stage. When writing coordinates, the x-coordinate always comes first, and the coordinates are separated by a comma. For example, an x-coordinate of 42 and a y-coordinate of 100 would look like this: (42, 100).

In the very center of the Stage is a point marked (0, 0), which is called the origin. In the following figure, I’m using the xy-grid backdrop from the Scratch Backdrop Library. (To load the xy-grid backdrop, click the Choose backdrop from library button next to the New backdrop label and select the backdrop.) I’ve added several cat sprites who are all saying their x- and y-coordinates.

The rightmost side of the Stage has an x-coordinate of 240. The x-coordinates get smaller as you go left. In the center, the x-coordinate is 0. To the left of the center, the x-coordinates become negative numbers. The leftmost side of the Stage has an x-coordinate of –240. The y-coordinates work the same way: the top of the Stage has a y-coordinate of 180, the center is 0, and the bottom is –180.

The mouse cursor’s x- and y-coordinates are also given in the bottom-right corner of the Stage. In the previous figure, the mouse cursor is at the coordinates (–182, –27), which means the x-coordinate is -182 and the y-coordinate is –27.

Scratch displays the x- and y-coordinates of the currently selected sprite in the upper-right corner of the Scripts Area. Sprites move around the Stage when you change their x- and y-coordinates as shown here:

Clicks in ** seoncds scratch You’ve probably played a maze game before, but have you ever tried making one? Mazes can be tricky to complete, but they’re easy to program. In this chapter, you’ll

Clicks in ** seoncds scratch

You’ve probably played a maze game before, but have you ever tried making one? Mazes can be tricky to complete, but they’re easy to program. In this chapter, you’ll create a game that lets the player guide a cat through a maze to reach its goal—a delicious apple! You’ll learn how to move the cat with the keyboard and how to block its progress with walls.

Before you start coding, take a look at the final program. Go to https://www.nostarch.com/scratchplayground/ and play the game.

SKETCH OUT THE DESIGN

First, draw what you want the game to look like on paper. With some planning, you can make your maze game a-mazeing. (I never apologize for my puns.) My sketch for the maze game looks like the following figure.

If you want to save time, you can start from the skeleton project file, named maze-skeleton.sb2, in the resources ZIP file. Go to https://www.nostarch.com/scratchplayground/ and download the ZIP file to your computer by right-clicking the link and selecting Save link as or Save target as. Extract all the files from the ZIP file. The skeleton project file has all the sprites already loaded, so you’ll only need to drag the code blocks into each sprite. Click File Upload from your computer in the Scratch editor to load the maze-skeleton.sb2 file.

Even if you don’t use the skeleton project, you should download the ZIP file from the website. This file contains the maze images you’ll use in this chapter.

If you want to create everything on your own, click File New to start a new Scratch project. In the text field in the upper left, rename the project from Untitled to Maze Runner.

MAKE THE CAT WALK AROUND

In the Maze Runner game, the player will control the cat sprite. In Part A, you’ll set up the code to control the cat with the arrow keys on the keyboard.

EXPLORE: X- AND Y-COORDINATES

To make the cat move around the Stage, you need to use coordinates. Coordinates are numbers that represent an exact location. The x-coordinate (also called x position) is a number that represents how far left or right a sprite is on the Stage. In other words, x is the sprite’s horizontal position. The y-coordinate (also called y position) is a number that represents how far up or down a sprite is on the Stage. The y-coordinate is a sprite’s vertical position.

Used together, x- and y-coordinates indicate a sprite’s precise location on the Stage. When writing coordinates, the x-coordinate always comes first, and the coordinates are separated by a comma. For example, an x-coordinate of 42 and a y-coordinate of 100 would look like this: (42, 100).

In the very center of the Stage is a point marked (0, 0), which is called the origin. In the following figure, I’m using the xy-grid backdrop from the Scratch Backdrop Library. (To load the xy-grid backdrop, click the Choose backdrop from library button next to the New backdrop label and select the backdrop.) I’ve added several cat sprites who are all saying their x- and y-coordinates.

The rightmost side of the Stage has an x-coordinate of 240. The x-coordinates get smaller as you go left. In the center, the x-coordinate is 0. To the left of the center, the x-coordinates become negative numbers. The leftmost side of the Stage has an x-coordinate of –240. The y-coordinates work the same way: the top of the Stage has a y-coordinate of 180, the center is 0, and the bottom is –180.

The mouse cursor’s x- and y-coordinates are also given in the bottom-right corner of the Stage. In the previous figure, the mouse cursor is at the coordinates (–182, –27), which means the x-coordinate is -182 and the y-coordinate is –27.

Scratch displays the x- and y-coordinates of the currently selected sprite in the upper-right corner of the Scripts Area. Sprites move around the Stage when you change their x- and y-coordinates as shown here:

Clicks in ** seoncds scratch You’ve probably played a maze game before, but have you ever tried making one? Mazes can be tricky to complete, but they’re easy to program. In this chapter, you’ll ]]>