Home > Books > Mission Python > Adding hidden objects

Mission Python: Adding hidden objects for an Easter egg hunt

Brightly painted and patterned plastic eggs

My book Mission Python shows you how to build a space adventure game, in which you collect objects and use them to make your escape from a space station. Part of the challenge is to find the right objects, spread across the space station and sometimes hidden from plain sight.

There are three ways to hide collectable objects (called props) in the Escape game from Mission Python:

  • Hiding a prop behind a piece of scenery
  • Hiding a prop inside a piece of scenery
  • Hiding a prop inside a random piece of scenery (uses new code)

In this article, I'll talk you through them. The book explains how the code works in more detail, but even if you haven't read the book, you can use the code here to add the Easter egg hunt to the game. If you haven't read the book, download the PDF sampler of Mission Python for instructions for getting started, including details of how to download and install the game. Mission Python is available to buy now, including in ebook for immediate delivery direct from the publisher.

Hiding a prop behind a piece of scenery in the Escape game from Mission Python

The simplest approach is to position a prop behind a piece of scenery. To make sure the game is fair and players don't have to examine every hidden floor space to find something, you should ensure that the object is still partially shown. In room 39, pictured below, I partially hid the spoon behind the table. I considered hiding the hammer behind the robot arm in room 50 but wasn't sure that would be clear enough to be fair, given that players wouldn't have seen any other robot arms by that point in the game, and might just think the hammer was part of the arm. You can have some fun repositioning objects so they're partly obscured by other objects. Use the map here for inspiration!

Crew's quarters, showing a spoon partially hidden behind a table

Crew's quarters, showing a spoon partially hidden behind a table

Hiding a prop inside a piece of scenery in the Escape game from Mission Python

Alternatively, you can position a prop inside a piece of scenery. When the scenery is examined, the prop will be discovered. For example, prop 76 (scissors) is located in room 41 (shown below) at y=3, x=5 (coordinates are numbered from the top left, starting at 0 and including the spaces occupied by the wall). The scenery for room 41 also includes item 10 (a cabinet) at that position. So, if you examine the cabinet, you'll discover the scissors. It's important to give players a strong hint about where to search, either by introducing a new and distinctive piece of scenery or by making the scenery something that naturally can contain things. The game would be frustrating if players had to examine every piece of wall or table to find an important object.

The sick bay in Mission Python, with cabinets you can hide things in

The sick bay in Mission Python, with cabinets you can hide things in

Hiding a prop inside a randomly chosen piece of scenery in the Escape game from Mission Python

The third way you can hide things is to randomly position them. An earlier iteration of the Escape game included a puzzle where you were trapped in your bedroom and had to find the access card to get out. The card was positioned somewhere randomly in your untidy bedroom, hidden in a random piece of scenery. I received feedback that this wasn't especially enjoyable, and took it out when redesigning the map. Games like Animal Crossing have shown that search puzzles can be fun, though, provided that there's enough of a hint to the players so that they don't have to search everything. You can hide inessential items as a reward to players who explore the game more fully.

I'm going to show you how to add an object to Escape and how to randomly position it, so that you can have an Easter egg hunt on the space station.

How to add new objects to the Escape game from Mission Python

  1. blue Easter egg imageAdd the image file: I'm going to use an Easter egg image, created by Fraang at OpenGameArt, which is shared under the CC-BY-SA 3.0 licence. I've cropped the image and resized it for use in Escape. Download the image here (right-click that link and use Save As or the equivalent option in your browser). Save it to your computer, in the images folder for the Escape game.

  2. Add the object to the objects list: You need to add the image filename, long description and short description to the game. If the image has a shadow you add its details now too. We're adding a prop and props cast no shadow, so we just use "None" for the shadow information. In the OBJECTS part of the program, add the new object to the end of the list, like this (pay attention to the extra comma at the end of object 81!):

    ###############
    ##  OBJECTS  ##
    ###############

    objects = {
        0: [images.floor, None, "The floor is shiny and clean"],

    --snip--

        81: [images.access_card, None,
            "This access card belongs to " + FRIEND2_NAME, "an access card"],
        82: [images.egg, None, "This chocolate egg looks delicious!", "an Easter egg"]
        }

  3. Make the object carryable: To enable the player to carry the Easter egg, we need to add it to the list items_player_may_carry. We can do that by changing the range of that list to include object 82. Change the 82 to 83 in the instruction that sets up this list (the last number in the range instruction isn't included in the range):

    items_player_may_carry = list(range(53, 83))

  4. Position your new prop: Now you can add an entry for this prop in the props list so it appears in the game. Props that are not in the game yet usually go into room 0, but let's put it into room 31 (the start room) so you can test it works okay. Pay attention to the new comma after object 81. You also need to comment out the checksum code which is used to make sure you entered the code correctly if typing it from the book. It will otherwise detect the new object as a typing error. Here's the modified code. Your changes start with the comma at the end of object 81, on the line that begins with 78.

    ###############
    ##   PROPS   ##
    ###############

    # Props are objects that may move between rooms, appear or disappear.
    # All props must be set up here. Props not yet in the game go into room 0.
    # object number : [room, y, x]
    props = {
         20: [31, 0, 4], 21: [26, 0, 1], 22: [41, 0, 2], 23: [39, 0, 5],

    --snip--

         78: [35, 9, 11], 79: [26, 3, 2], 80: [41, 7, 5], 81: [29, 1, 1],
         82: [31, 3, 3]
    }

    ### commented out following code to add EASTER EGG ###

    ##checksum = 0
    ##for key, prop in props.items():
    ##     if key != 71: # 71 is skipped because it's different each game.
    ##         checksum += (prop[0] * key
    ##             + prop[1] * (key + 1)
    ##             + prop[2] * (key + 2))
    ##print(len(props), "props")
    ##assert len(props) == 37, "Expected 37 prop items"
    ##print("Prop checksum:", checksum)
    ##assert checksum == 61414, "Error in props data"

  5. Adding a prop interaction: The USE OBJECTS part of the program is where the instructions are for using an object. The simplest action is to display a message when the object is used. Just add the new object 82 to the standard_responses list with an appropriate message, as shown below. (If you want to do something more sophisticated, take a look at the code for object 16 in this section which increases the player's energy when they eat the space lettuce.). Remember the new comma after object 75 in the list.

         standard_responses = {

    --snip--

         75: "You are at Sector: " + str(current_room) + " // X: " \
            + str(player_x) + " // Y: " + str(player_y),
         82: "You nibble at the chocolate. It tastes divine!"
         }

You should now be able to start the game as usual, and see the Easter egg in the start room. You can pick it up, drop it, and use it to eat a piece.

You can now hide it behind scenery by changing the room number and y and x positions for the egg (object 82) in the props list (see Step 4 above).

To add more eggs, add additional objects (83, 84 etc) that use the same images.egg file.

Hiding objects in the Escape game from Mission Python

The next step is to hide the Easter egg in a random piece of scenery, so it is revealed when that scenery is examined. Below is a new function for hiding objects in the scenery in a room. You tell it which object to hide, and which room to hide it in. The code chooses a random piece of scenery to hide the object in. Remember to provide a strong hint about which room an object is hidden in, so that players don't waste time hunting in rooms where nothing is hidden. For example, you could modify the room description to say there's a lovely chocolatey smell here.

I've included an instruction to hide the Easter egg in the crew's quarters (pictured at the top of this page). You can easily find this room, and there's a map of the Mission Python game here if you get stuck.

Add this code before the START section, near the end of the program.

########################
##   HIDING OBJECTS   ##
########################

def hide_object(room_number, object_to_hide):
     # this function puts an object into a place where there is already a piece of scenery
     # it can be used for hiding things in a random location in a particular room
     # for example an Easter egg in the crew room
     global props
     number_of_items_in_room = len(scenery[room_number])
     item_to_hide_in = random.randint(0, number_of_items_in_room - 1)
     print(item_to_hide_in)
     props[object_to_hide][0] = room_number
     props[object_to_hide][1] = scenery[room_number][item_to_hide_in][1]
     props[object_to_hide][2] = scenery[room_number][item_to_hide_in][2]

hide_object(39, 82) # hide object 82 (egg) in room 39 (crew community room)

Download the modified Escape code incorporating an Easter egg hunt

You can download the modified game including a hidden Easter egg here (right-click that link and use Save As to download the file). It downloads as a .txt file, so you'll need to rename it to a .py file.

Happy hunting!

Find out more about Mission Python

Photo credit: Starry sky by Pel at Unsplash

Credits

© Sean McManus. All rights reserved.

Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!

Discover my latest books

Coding Compendium

Coding Compendium

A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.

Web Design in Easy Steps

Web Design IES

Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.

100 Top Tips: Microsoft Excel

100 Top Tips: Microsoft Excel

Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.

Scratch Programming in Easy Steps

Scratch Programming IES

This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.

Mission Python book

Mission Python

Code a space adventure game in this Python programming book published by No Starch Press.

Cool Scratch Projects in Easy Steps book

Cool Scratch Projects in Easy Steps

Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!

Walking astronaut from Mission Python book Top | Search | Help | Privacy | Access Keys | Contact me
Home | Newsletter | Blog | Copywriting Services | Books | Free book chapters | Articles | Music | Photos | Games | Shop | About