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:
- Create a New file in PictoBlox App.
- Create a list named Animals from Variable
- You will observe a new set of blocks appearing for the list.
- For this program, we are interested in 3 blocks:
- delete all of () block: This block deletes all the items of the list.
- add () to () block: This block adds an item to the list.
- item () of () block: This block reports the item from the specified position from the list.
- 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.
- Now ask the user for the number of animals and add a repeat () block to repeat with the answer.
- Next, for every repeat iteration, create a clone of the animal using create clone of () block from Control Select Animal.
- 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.
- Add the show block below when I start as a clone
- 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 ()
- Next, place the clone at a random location on the stage using the go to (random position) block from Motion
- Finally, add the forever and glide () secs to (random position) block to glide the clone to a random position constantly.
- Run the script by clicking the green flag and enjoy the show.
- Save the file as Collections.