MAZE RUNNER - No Starch Press

3y ago
67 Views
8 Downloads
4.00 MB
24 Pages
Last View : 13d ago
Last Download : 3m ago
Upload by : Albert Barnett
Transcription

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al Sweigart3MAZE RUNNERou’ve probably played a maze gamebefore, but have you ever tried makingone? 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 acat through a maze to reach its goal—a deliciousapple! You’ll learn how to move the cat with thekeyboard and how to block its progress with walls.

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartBefore you start coding, take a look at the final program.Go to https://www.nostarch.com/scratchplayground2/ andplay the game.Your goal is to get the apple!You control the cat.Sketch Out the DesignFirst, draw what you want the game to look like on paper. Withsome planning, you can make your maze game a-maze-ing. (Inever apologize for my puns.) My sketch for the maze gamelooks like the following figure:34CHAPTER 3

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartDCMake a goal atthe end of the maze.Keep the cat fromwalking through walls.BMake the maze levels.AMake the catwalk around.If you want to save time, you can start from the skeletonproject file, named maze-skeleton.sb3, in the resources ZIPfile. This file has parts of the project already set up and onlyneeds the code blocks added to it. Go to https://www.nostarch.com/scratchplayground2/ and download the ZIP file to yourcomputer by right-clicking the link and selecting Save linkas or Save target as. Extract all the files from the ZIP file.The skeleton project file has all the sprites already loaded, soyou’ll only need to drag the code blocks into each sprite. ClickFile u Load from your computer in the Scratch editor toload the maze-skeleton .sb3 file.Even if you don’t use the skeleton project, you shoulddownload the ZIP file from the website. This file contains themaze images you’ll use in this chapter.If you want to create everything on your own, clickFile u New to start a new Scratch project. In the text fieldin the upper left, rename the project from Untitled to MazeRunner.Maze Runner35

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartA Make the Cat Walk AroundIn the Maze Runner game, the player will control the catsprite. In Part A, you’ll set up the code to control thecat with the arrow keys on the keyboard.EXPLORE: X- AND Y-COORDINATESTo 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 representshow far left or right a sprite is on the Stage. In other words, x is thesprite’s horizontal position. The y-coordinate (also called y position)is a number that represents how far up or down a sprite is on theStage. The y-coordinate is a sprite’s vertical position.Used together, x- and y- coordinates indicate a sprite’s preciselocation on the Stage. When writing coordinates, the x-coordinatealways comes first, and the coordinates are separated by a comma.For example, an x- coordinate of 42 and a y- coordinate of 100 wouldlook like this: (42, 100).In the center of the Stage is a point marked (0, 0), which is called theorigin. In the following figure, I’m using the xy-grid backdrop fromthe Scratch Backdrop Library. (To load the xy-grid backdrop, click36CHAPTER 3

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al Sweigartthe Choose a backdrop button in the lower right and select thebackdrop.) I’ve added several cat sprites who are all saying their xand y-coordinates.The rightmost side of the Stage has an x-coordinate of 240. Thex-coordinates get smaller as you go left. In the center, the x-coordinateis 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. They-coordinates work the same way: the top of the Stage has a y-coordinateof 180, the center is 0, and the bottom is 180.Scratch displays the x- and y-coordinates of the currently selectedsprite in the upper-right corner of the Sprite List. Sprites move aroundthe Stage when you change their x- and y-coordinates, as shown here:To make a sprite go . . .change its . . .by a . . .Rightx-coordinatepositive numberLeftx-coordinatenegative numberUpy-coordinatepositive numberDowny-coordinatenegative number(continued)Maze Runner37

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartMany of the blocks in the dark blue Motion category will changea sprite’s x and y position, such as the change x by and change y byblocks. Note that changing a coordinate by a negative number is thesame as subtracting a positive number from it.1. Add Movement Code to the Player SpriteThe first bit of code you’ll add will make the arrow keys movethe cat sprite, which is named Sprite1. But first, click andrename this sprite Orange Cat. Then add the following code.You’ll find these blocks in the Events, Control, Sensing, andMotion categories.This code repeatedly checks whether the key is beingpressed. The code literally reads “for forever, check whether38CHAPTER 3

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al Sweigartthe up arrow key is pressed, and if it is, thenchange y by 4.” If the up arrow key is not beingpressed, Scratch skips the code inside the if thenblock.Pressing the up arrow key makes the catsprite move up. The forever loop block meansScratch will check over and over again whetherthe up arrow key is being pressed. This continuesuntil you click the red stop sign.The forever block is needed for this program to work.Without it, Scratch would check only once if the up arrowkey was pressed. Then the program would end. But youwant Scratch to keep checking forever if the up arrow key ispressed. If your program doesn’t seem to be doing anything,make sure you didn’t forget to add the forever block.When you code this on your own, be sure you use thechange y by code block instead of the change x by or set yto code block. If your program isn’t working correctly, checkthat your code is the same as the code in this book.Save PointClick the green flag, and try moving the cat by pressing theup arrow key. Then click the red stop sign, and save yourprogram.2. Duplicate the Movement Code for the Cat SpriteNow you’ll add code for the other three arrow keys: down, left,and right. This code is similar to the code to move the catsprite up. To save time, you can right-click or long press theorange if then block and select Duplicate to create a copyof the blocks. These blocks will be identical, so all you’ll needto change are the dark blue Motion blocks for the other directions. Duplicating blocks can often be faster than draggingnew ones from the Block Palette.Maze Runner39

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartScratch will now check if the four arrow keys are helddown, one after another. After checking the right arrow key,Scratch starts back at the top of the loop and checks the uparrow key again. The computer checks them so fast thatto human eyes it looks like all of the arrow keys are beingchecked at the same time!Save PointClick the green flag to test the code so far. The cat shouldwalk up, down, left, and right when you press the arrow keys.Notice that Orange Cat is small enough to fit in the maze you’llcreate next. Click the red stop sign, and save your program.If your program doesn’t work and you don’t know howto fix it, you can start over by using the maze-part-a.sb3Scratch project file, which is in the resources ZIP file. ClickFile u Load from your computer in the Scratch editor toload the maze-part-a.sb3 file, and then move on to Part B.40CHAPTER 3

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartBMake the Maze LevelsNext, we’ll create the maze sprite and set the backdrop. Themaze game would quickly get boring if it had only one maze,so we’ll also add multiple levels to the game.3. Download the Maze ImagesYou could draw the maze sprite yourself, but let’s use imagesfrom the ZIP file instead. One of the maze images is the Maze .sprite3 file.In the Scratch editor, click the Upload Sprite button, whichappears after you tap or hover over the Choose a Sprite button(which looks like a face) and select Maze.sprite3 to upload thefile. This creates a new sprite named Maze with several mazecostumes. Every sprite in Scratch can have multiple costumes tochange the way it looks. These costumes, which you can see byclicking the Costumes tab, are often used to animate the sprite.Your Sprite List should look like this:4. Change the BackdropLet’s add a little flair to the maze by placing some artworkin the background. You can use whichever backdrop youlike. Change the Stage’s backdrop by clicking the Choosea Backdrop button in the lower right to open the ScratchBackdrop Library window. Choose a backdrop (I chose light).5. Start at the First MazeAdd the following code to the Maze sprite. You can find theseblocks in the Events, Looks, and Motion categories.Maze Runner41

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartEach of the Maze sprite’s costumes will be a new level.When the player clicks the green flag to start the program, thegame should begin with the first costume and make sure themaze is in the center of the Stage. We will add code to switchto the next level in steps 8 and 9.Note that the Code Area shows code blocks for the selectedsprite. Be sure the Maze sprite is selected in the Sprite List;otherwise, you’ll add the maze’s code to a different sprite. Eachsprite needs its own code to work correctly. If you don’t seemaze1 in the switch costume to block, the Orange Cat spriteis most likely selected.If your Scratch program doesn’t work and you don’t knowhow to fix it, you can start over by using the maze-part-b.sb3Scratch project file, which is in the resources ZIP file. ClickFile u Load from your computer in the Scratch editor toload the maze-part-b.sb3 file, and then move on to Part C.C Keep the Cat from WalkingThrough WallsWhen you click the green flag now, you’ll be able to move thecat through the maze. But you’ll also be able to move the catthrough the walls of the maze, because nothing in the program prevents this from happening. The code only states,“when the right arrow key is pressed, move the cat right.”The cat moves, whether or not a wall is there.6. Check Whether the Cat Is Touching the WallsLet’s add code that checks whether the cat is touching theblue walls. If it is, the cat should move backward. So ifthe cat moves to the right and is touching a wall, it should42CHAPTER 3

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al Sweigartautomatically move left. This will undo the player’s move andprevent the cat from moving through walls. Click the OrangeCat sprite in the Sprite List, and modify the code to look likethe following. Notice that we’re using the touching? block,not the touching color? block.Maze Runner43

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartAlso, you might have noticed that the Orange Cat spriteis Godzilla-sized compared to the maze, making the cat lookunrealistic. Add a set size block from the Looks category tomake the Orange Cat sprite smaller. You also want the OrangeCat sprite to always be shown on top of the maze, so you’ll addthe go to front layer block. These two code blocks are at thetop of the script.Save PointClick the green flag to test the code so far. Make sure theOrange Cat sprite cannot walk through the maze walls. Testthis for all four directions. Then click the red stop sign, andsave your program.If your Scratch program doesn’t work and you don’t knowhow to fix it, you can start over by using the maze-part-c.sb3Scratch project file, which is in the resources ZIP file. ClickFile u Load from your computer in the Scratch editor toload the maze-part-c.sb3 file, and then move on to Part D.D Make a Goal at the End ofthe MazeRight now, it isn’t clear where the player is supposed to end upin the maze. Let’s add an apple at the other end of the maze tomake the player’s goal more obvious.44CHAPTER 3

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al Sweigart7. Create the Apple SpriteClick the Choose a Sprite button. When the Sprite Librarywindow appears, select Apple to add a new sprite namedApple to the Sprite List.When the game starts, you want the Apple sprite to moveto the end of the maze at the top of the Stage. The Apple spritealso has to be small enough to fit in the maze. Add the following code to the Apple sprite:8. Detect When the Player Reaches the AppleThe Maze Runner game has a sprite for the player, the maze,and the apple at the end of the maze. Now it just needs thecode to detect when the player reachesthe end. When that happens, you’llhave Scratch play a sound and thenswap costumes to the next level. Butbefore you add the code, you need toload the Cheer sound. Select Orange Catin the Sprite List. Click the Soundstab at the top of the Block Palette, andthen click the Choose a Sound buttonin the lower left. This button looks likea speaker.In the Sound Library window thatappears, select Cheer to load the Cheersound. Now click the Code tab.Maze Runner45

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartThe broadcast block causes a script under a matchingwhen I receive block to run. Add this script to the Orange Catsprite’s code:To make the broadcast block broadcast the next mazemessage, click the white triangle in the broadcast block andselect new message.In the window that appears, type next maze as the message name and click OK.46CHAPTER 3

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al Sweigart9. A dd the Broadcast Handling Code to the MazeSpriteClick the Maze sprite in the Sprite List, and add the followingcode to it:This code changes the maze level when it receives the nextmaze broadcast.Save PointClick the green flag to test the code so far. Try playing thecompleted game. Make sure the level changes to the next mazewhen the cat touches the apple. Then click the red stop sign,and save your program.The Complete ProgramThe following code shows theentire program. If your programisn’t working right, check yourcode against this complete code.The complete program is alsoin the resources ZIP file as themaze .sb3 file.I hope the code for thismaze program isn’t toolabyrinthine.Maze Runner47

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al Sweigart48CHAPTER 3

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartVersion 2.0: Two-Player ModeNow that the basic maze game is working, you can do someiterative development and add small improvements one at atime. Iterative development helps you avoid trying to make agame that is too large for you to finish.In version 2.0 of Maze Runner, you’ll add a second player.The two players will race against one another. The first playerstarts at the bottom and races to the top; the second playerraces from the top to the bottom. Because they both musttravel the same path, the distance for each is the same.Duplicate the Apple SpriteThe second player needs a goal too. Right-click or long press theApple sprite and select Duplicate to make a copy of the Applesprite and its code. The new sprite is automatically namedApple2. Select the Apple2 sprite, and click the Costumes tab.Select a green fill color, and then select the Fill tool to theright (it looks like a tipped cup). Then click the red part of theapple to change it to green. When you’re done, Apple2 will looklike the following figure:Maze Runner49

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al Sweigart1Select the green color.2Click the Fill tool.3Click the center of the apple.Modif y the Apple2 Sprite’s CodeYou need to modify the Apple2 sprite’s code to look like the following code so that the green apple starts at the bottom of themaze rather than the top:Duplicate the Orange Cat SpriteNow let’s add a second cat sprite. Right-click or long pressthe Orange Cat sprite, and select Duplicate from the menuto make a copy of Orange Cat and its code. The new sprite is50CHAPTER 3

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al Sweigartautomatically named Orange Cat2. The Orange Cat and OrangeCat2 sprites need to look different enough that the players cantell them apart. Similar to what you did for Apple2, click theCostumes tab, and change Orange Cat2 from orange to blue.Click all orange partsto make them blue.In the Sprite Pane, rename Orange Cat2 to Blue Cat.Modif y the Code for the Blue Cat SpriteRight now the Blue Cat sprite has the same code as the OrangeCat sprite. You’ll need to change the code; otherwise, the arrowkeys will control both player 1 and player 2. The second playerwill control the Blue Cat sprite using the WASD keys (pronounced whaz-dee). The W, A, S, and D keys are often usedas a left-handed version of the up, left, down, and rightarrow keys.Change the two go to x y blocks and the key pressed?blocks for Blue Cat to look like the following code. Also, remember to change if touching Apple to if touching Apple2.Maze Runner51

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartChanged x and yChanged the keyChanged the keyChanged the keyChanged the keyChanged spriteChanged x and y52CHAPTER 3

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartGo Back to the Starting PositionThe sprites go back to their starting positions when they touchtheir apples, but they also need to go there when the other catwins. Add the following script to the Orange Cat sprite:Then add the following script to the Blue Cat sprite:This way, when the other cat wins and broadcasts thenext maze message, both cats will go back to their startingpositions.Save PointClick the green flag to test the code so far. Try moving bothplayers using the arrow keys and the WASD keys. Make sureeach of the eight keys moves the correct cat and only that cat.Try playing the full game. Make sure the level changes to thenext maze when the second player touches the green apple.Make sure both players are sent to their starting points whenthe next level begins. Click the red stop sign and save yourprogram.You’ve just upgraded your maze game to support two players. Find a friend to race against. Player 1 uses the arrowkeys, and player 2 uses the WASD keys.Maze Runner53

Scratch Programming Playground, 2nd Edition (Sample Chapter) 9/14/20 by Al SweigartCheat Mode:Walk Through WallsTeleporting is a cool cheat, butplayers can’t control where theyteleport to. Also, it’s too obviousthat a player is cheating when theysuddenly move across the Stage andthrough many walls. However, youcan add a subtler cheat that lets thecats move through walls when aspecial key is held down.Add the Walk-Through-WallsCode to Orange CatFor the Orange Cat sprite, modifythe walking code so the touching Maze? blocks are replaced with the touching Maze? andnot key l pressed? blocks. Only the code for the up arrow isshown here, but you’ll want to replace the touching Maze?blocks in all four of the if key pressed cases.This enables the wall-blocking code only if the L key is notbeing pressed. If the L key is pressed, this wall-blocking codeis skipped, and the player walks through the walls.Add the Walk-Through-Walls Code to Blue CatMake the same walk-through-walls code changes to the BlueCat sprite, except instead of key l pressed, make it key qpressed. The second player can walk through walls whenthe Q key is held down.54CHAPTER 3

Scratch Programming Playground, 2nd

MAZE RUNNER. ou’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

Related Documents:

The Maze Runner Series The Maze Runner The Scorch Trials The Death Cure The Kill Order The 13th Reality Series The Journal of Curious Letters The Hunt for Dark Infinity The Blade of Shattered Hope The Void of Mist and Thunder. This is a work of fiction. Names, characters, places, and incidents either are the product of the author's .

THE MAZE RUNNER In this package you will find the questions you must complete as you read the novel The Maze Runner. You are responsible for bringing these things to class every day. Please do not write on the question sheets. Instead, record your answers neatly on loose leaf and keep them in your novel duotang.

Reading (R-CBM and Maze) Grade 1 Grade 2 R-CBM Maze R-CBM Maze Tier 2 Tier 1 Tier 2 Tier 1 Tier 2 Tier 1 Tier 2 Tier 1 Fall 0 1 21 55 1 4 Winter 14 30 1 3 47 80 4 9 Spring 24 53 3 7 61 92 8 14 Grade 3 Grade 4 R-CBM Maze R-CBM Maze Tier 2 Tier 1 Tier 2 Tier 1 Tier 2 Tier 1 Tier 2 Tier 1 Fa

upon time all students will begin the maze and upon completion they will record how long it took them to make it through the maze to the nearest second. Students record their maze completion time value on a post-it note to be collected by the teacher. Class maze completion times are written in an appropriate column on the board (male or female).

1: Like the Very First Time The Maze Runner begins with Thomas waking up in the ‘Box’, just before he meets the Gladers for the first time. What is perhaps most remarkable about this opening chapter is that our learning about world of the Maze is simultaneous with Thomas’s learning. Thomas, like the reader, has no previous experience of the

MAZE RUNNER BY JAMES DASCHNER ! If you ain’t scared!, you ain’t human. Remember. Survive. RUN.!!! Use this Survival Guide to help you navigate your way through the Maze of assignments. Remember to bring your Survival Guide and adhere to the Number One Rule—Always Finish the Questions and Tasks, unless you want to be

These pages from the Maze Reading Passages for 2nd Grade manual are provided as a . Maze Reading Passages is defined as a literary work and as such the reproduction, distribution, and display of Maze Reading Pass

ANIMAL NUTRITION Tele-webconference, 27 November, 10 and 11 December 2020 (Agreed on 17 December 2020) Participants Working Group Members:1 Vasileios Bampidis (Chair), Noël Dierick, Jürgen Gropp, Maryline Kouba, Marta López-Alonso, Secundino López Puente, Giovanna Martelli, Alena Pechová, Mariana Petkova and Guido Rychen Hearing Experts: Not Applicable European Commission and/or Member .