• Secondary
  • Lesson 10 – Understanding Collections

Lesson 10 – Understanding Collections

Lesson Description

In this lesson, you will learn: Arrays are collections of similar data type variables and how to create & use them in Python & PictoBlox. You’ll also learn how to create them using the List block in PictoBlox.

INTRODUCTION

A collection is nothing but a container that groups multiple items into a single object. Collections are used to store data. We can also retrieve and manipulate data that is stored in the Collections.

In the real world, you can consider a collection of cards, a telephone directory, or a mailbox to be an example of Collections

ADVANTAGES OF USING COLLECTION

  • Collections allow programmers to group multiple items into a common set.
  • Grouping of items makes it easier to identify and perform different operations on them like retrieving and modifying data.
  • It increases the performance of the program.

It increases productivity and reduces operational time

COLLECTIONS IN PICTOBLOX

In PictoBlox, Collection can be implemented with List. The list is available in the Variables palette.

ACTIVITY: BUILDING A ZOO

In this project, we are going to create a zoo in PictoBlox.

The main features of the project are:

  • Tobi will ask the user the number of animals you want in the zoo.
  • The program has an animal sprite which has the following animals:
    • Bear
    • Bat
    • Crab
    • Lion
  • Based on the numbers entered, the clones of the animals will be created in the zoo.
  • Once the clone is created, the animals will roam around the zoo.

Important Note: Please click on this link: https://pictoblox.page.link/SBX5GRgA3RDF5B9Q9. A sb3 file will get downloaded, open it in your PictoBlox environment. We will continue making the project in this file.

You will observe that the file already has the sprites Tobi and Animal. Also, the animal sprite has 4 costumes.

CODING STEPS

Follow the steps below:

  1. Create a New file in PictoBlox App.
  2. Create a list named Animals from Variable
  3. You will observe a new set of blocks appearing for the list.
  4. For this program, we are interested in 3 blocks:
    1. delete all of () block: This block deletes all the items of the list.
    2. add () to () block: This block adds an item to the list.
    3. item () of () block: This block reports the item from the specified position from the list.
  5. Select Tobi sprite. Create the following script which deletes all the items of the Animals and then add the four animals: Bear, Bat, Crab, and Lion.
  6. Now ask the user for the number of animals and add a repeat () block to repeat with the answer.
  7. Next, for every repeat iteration, create a clone of the animal using create clone of () block from Control Select Animal.
  8. Select Animal Add when I start as a clone block from the Control palette. This block will start executing whenever a new clone is created.
  9. Add the show block below when I start as a clone
  10. As we want to have a random animal is selected from the list we will execute the following script to change the costume of the clone.
    • First, we are picking a random number between 1 to 4 using the pick random () to ()
    • Then, get the name of the animal being selected from the list using the random number using item () of ()
    • Finally, switch the costume to the animal’s name using switch costume to ()
  11. Next, place the clone at a random location on the stage using the go to (random position) block from Motion
  12. Finally, add the forever and glide () secs to (random position) block to glide the clone to a random position constantly.
  13. Run the script by clicking the green flag and enjoy the show.
  14. Save the file as Collections.

Table of Contents