Unit 9:   User Interaction and Complex next-world in Your Game

imageUnit 9User Interaction and Complex next-world in Your Game
Unit Overview

Using the same techniques as their Ninja Cat games, students will return to piecewise functions to write a function to make their own games respond to keypresses, and identify ways in which their own world structure will change without user input.

Product Outcomes:
  • Students will use ask in their next-world functions

  • Students will identify circumstances in which the functions in their game should behave differently

  • Students will define these circumstances - and the desired behavior - in code, as different ask branches

Standards and Evidence Statements:

Standards with prefix BS are specific to Bootstrap; others are from the Common Core. Mouse over each standard to see its corresponding evidence statements. Our Standards Document shows which units cover each standard.

    Length: 90 minutes
    Glossary:
    • piecewise function: a function that computes different expressions based on its input

    Materials:
    • Pens/pencils for the students, fresh whiteboard markers for teachers

    • Class poster (List of rules, design recipe, course calendar)

    • Editing environment (Pyret Editor)

    • Student workbooks

    • Language Table

    Preparation:
    • Seating arrangements: ideally clusters of desks/tables

    Types

    Functions

    Number

    + - * / num-sqr num-sqrt num-expt

    String

    string-append string-length

    Image

    rectangle circle triangle ellipse radial-star scale rotate put-image

    Boolean

    = > < string-equal and or


    Introduction

    Overview

    Learning Objectives

      Evidence Statements

        Product Outcomes

          Materials

          • Pens/pencils for the students, fresh whiteboard markers for teachers

          • Class poster (List of rules, design recipe, course calendar)

          • Editing environment (Pyret Editor)

          • Student workbooks

          • Language Table

          Preparation

          • Seating arrangements: ideally clusters of desks/tables

          Introduction (Time 5 minutes)

          • Your games are really coming together! At this point you have the basic shell of your video game, along with basic animation. The next steps are things you’ve already programmed in your Ninja Cat game: keypresses, and a more complex version of next-world, like identifying the boundaries of your game screen, or adding randomization.

            Turn to Page 36 in your workbook. Choose which keys will control your game, and list them on the left-hand column of the table. Fill in the rest of the table by stating which part of the world will change in response to that key (be sure to use the dot-accessor!), and how that part of th world will change. Will the player move by 10, or 20 pixels? Will the direction of the dange change?

            Make sure students are specific in this activity: Have them identify the dot-accessor they’ll need to use to change the world structure, and exactly how their world will change. This will make the next step much easier.

          Keypresses in Your Game

          Overview

          Learning Objectives

          • Using conditionals, students will write a function to handle different keypresses in their game.

          Evidence Statements

            Product Outcomes

              Materials

                Preparation

                Keypresses in Your Game (Time 35 minutes)

                • Now it’s time to program YOUR game to respond to keypresses.

                  Turn to Page 37 in your workbook. Choose 3 keys will control your game, and go through the Design Recipe: Write test cases for what should happen to worldA depending on which key was pressed. Then define your function. Once you’ve completed the Design Recipe, type everything into your games.

                  Of course, keypresses can do a lot more in a videogame than just move a character up and down. By using what your learned about Boolean functions, you can add more advanced movement. Here are some ideas:

                  • Warping: instead of having the player’s y-coordinate change by adding or subtracting, replace it with a Number to have the player suddenly appear at that location. (For example, hitting the "c" key causes your player to warp back to the center of the screen, at 240.)

                  • Boundary-detection: Change the condition for moving up so that the player only moves up if key = "up" AND the player’s y-coordinate is less than the top of the screen. Likewise, change the condition for "down" to also check that the player’s y-coordinate is greater than 0. Hint: In Bootstrap:1, everyone’s game screen was 640x480. In Bootstrap:2, the size of your game screen is determined by the size of your background image. To find out exactly how large your image is, you can use the following functions to get the dimensions of your background:  

                  • Wrapping: Add a condition (before any of the keys) that checks to see if the player’s y-coordinate is above the screen. If it is, have the player warp to the bottom. Add another condition so that the player warps back up to the top of the screen if it moves below the bottom.

                  • Challenge: Have a character hide when the "h" key is pressed, only to re-appear at the same location when it is pressed again.

                  Hint for the challenge: What operation reverses itself when done twice?

                Tests and Results

                Overview

                Learning Objectives

                  Evidence Statements

                    Product Outcomes

                    • Students will use ask in their next-world functions

                    • Students will identify circumstances in which the functions in their game should behave differently

                    • Students will define these circumstances - and the desired behavior - in code, as different ask branches

                    Materials

                      Preparation

                      Tests and Results (Time 15 minutes)

                      • Now to use what you know about boundary detection and apply it to your own game!
                        • Open your workbook to Page 41.

                        • Think about the things in your game that make the world change. Do characters fly off the left side of the screen? The right? The top or bottom? Do you need to write an off-top function or off-bottom? Maybe something happens when the player achieves a high score, or the player’s health reaches 0.

                        • In the lefthand column of Page 41, make a list of the questions (in Pyret!) you will need to ask the world. For example, with the dog we said is-off-right(current-world.dogX) to ask if the dog was off the right side of the screen. On the right, figure out which world you need to make, if your question returns true.

                        Some examples of game states students may want to test for:

                        • Gravity: the player constantly moves down, until her y-coordinate is 50, placing her at the bottom of the screen. (This coordinate will probably be adjusted based on the size of the PLAYER-IMG image.)

                        • The player shoots a projectile and destroys the alien/shark/bad guy: An element of the world cannot be taken out of the world struct, but its locaton can be changed so that it no longer appears on the screen. If the DOG-IMG has been set to a certain coordinate and has been "destroyed", should it continue to move normally?

                        • After reaching a certain score, the background image changes, and the player reaches "Level 2". (Note: changing the background image is handled by the draw-world function. This can be a piecewise function just like next-world! Students who want to change the apprearance of their game should turn to Page 43 and make a list of the questions to ask inside draw-world to change the image(s) in their game.

                      Branches in next-world

                      Overview

                      Learning Objectives

                        Evidence Statements

                          Product Outcomes

                            Materials

                              Preparation

                              Branches in next-world (Time 30 minutes)

                              • Think about the ask branches in Ninja World’s next-world function. Remember that for each ask, we needed a test and a result. This is exactly what you’ve written in your workbook for your game. All you need to do now is reformat your next-world function so that it uses ask, with your current code inside the otherwise clause.

                                Adapt next-world so it becomes a piecewise function, and use at least one helper function on your list from Page 41.

                                Work in pairs or small groups to assist students with their own next-world functions.

                              Closing

                              Overview

                              Learning Objectives

                                Evidence Statements

                                  Product Outcomes

                                    Materials

                                      Preparation

                                      Closing (Time 5 minutes)

                                      • You started from scratch, and now have a working videogame! You probably have at least a few more things to add, which we’ll work on in the next unit!

                                        Have students show each other their nearly completed games!