Friday, 12 January 2018

3) Creating a game using scratch


Creating a game using scratch


Scratch can be used to create amazing animation games. These can further develop the programming skills of students while having fun. Various skills developed are broadcasting messages to sprite, using sensing palette and Operators block palette etc.

Important script blocks for game development

  1. Broadcasting: It is used to communication between various sprite. A sprite can broadcast a message. Others on receiving this broadcast can execute a set of instructions.

The above message can be sent once the objective of the game is achieved.

“Victory” message can then be received by other sprites to stop the script.

  1. Sensing Block Palettes: These blocks can be used to execute a script when some event is sensed by sprite(like touching another sprite, touch edge of stage area, some key is pressed etc ).



For example, the code inside the above if condition is executed, if mouse pointer is touching the sprite. Various type of touching blocks include
  • Edge of stage area
  • Mouse pointer
  • Specific Color

Sprites can also be made to perform specific tasks when user gives a particular input(like space bar, mouse click, arrow keys etc).

  1. Operators Block Palette: This palette can be used to put various conditions in a game. It can be used to compare values , pick random number, perform arithmetic operation like add multiply etc.
In the below example, a number between 5 to 10 is picked up at random. The sprite waits for random number of seconds between 5 to 10.


In this handout a sample City Traffic game is developed.

City Traffic Scratch Game
In this game cat in the city is trying to dodge traffic coming from all directions. Cat jumps over cars and cycles when space bar button is pressed. If it manages to save itself from 30 objects then it wins the game. Perform the following steps to execute the game:

  1. Selecting Background:
  • Select city background.
  • Click on stage.
  • Select backgrounds tab from the script area.
  • Click on import tab.
  • From outdoor folder of scratch, select night-city-with-streets.gif
  1. Adding vehicles to your animation
In costumes folder, go to transportation folder:
  • Add one car sprite
  • Add one bicycle sprite
  • Add one aircraft sprite
In total now you would have four sprites including the default cat sprite.
  1. Setting direction of sprite
  • Select each sprite
  • Set rotation style to “only face left-right” of each sprite. This ensures that sprite rotate only left or right and not vertically.


  1. Resize the sprites
  • Select shrink sprite or grow sprite to change the size of each sprite


Your stage should look similar to the image below.

  1. Scripting your cat sprite
  • Select sprite one
  • Produce the following script in script area


Explanation of script of cat:
  1. When Green Flag is clicked Cat sprite explains the rules of the game. User has to press space bar to jump whenever there is a chance of collision.
  2. When space bar is pressed cat jumps. This part is manged using glide block and wait block. The jump time is adjusted to set difficulty of the game.

  1. When sprite wins, victory message is displayed for 6 sec. It receives victory message from bicycle when it has completed 10 rounds. The bicycles broadcasts message “Victory”. This message starts the code following “when I receive “victory”” block. The sprite announces “Yippee!! I survived”. Finally all the scripts are stopped.

  1. Scripting your bicycle sprite
  • Select the bicycle sprite
  • Produce the following script
 

Explanation of script of bicycle:
While making the script of bicycle blocks from

  1. When green flag is pressed it waits for some time before starting.
  2. It drives repeatedly 10 times. This is achieved using “Repeat 10” block.
  3. If the edge of stage area is reached it goes to the initial location. This is done using sensing block palette. “touching edge” sensing block palette is executed when the sprite reaches the end of the stage area.

  1. Car then waits for a random amount of seconds before restarting. This is done with the help “pick random” block in Operators sensing palette.

  1. If user succeeds to dodge this bicycle 10 times, this sprite broadcasts victory. This message can then be used by other sprites to end game.
  2. When green flag is clicked, bicycle sprite continuously checks if there is any contact with cat sprite. It does this with the help of “touching Sprite1?” block in sensing block palette. Whenever the bicycle sprite touches Sprite1 , the if block is executed. It displays “Oops You are out!!!” and finally stops all the scripts. This results in defeat in game.
  1. Script your car sprite
  • This script is similar to bike sprite
  • Produce code similar in car sprite
 

Explanation of script of car:
  1. Script of car is similar to that of bicycle. Car waits for just 12 secs before it starts entering the road while bicycle waits for 14 secs. Also the message displayed on collision with car is different. Message “Squashed” is displayed to indicate collision of cat with car. Collision is detected using “touching Sprite1 ?” block in sensing palette block.
  1. Scripting your aircraft sprite
  • This sprite is similar car and bike sprite. But the coordinates are different.
  • Produce the script similar to the following script
 


Explanation of aircraft script:

  1. Script of aircraft is similar to the script of car and bicycle with only minor differences. The coordinates of the aircrafts are slightly different. By changing the values of y in “go to x __ y __” to a positive number raises the position of the aircraft. The cat now has to jump carefully to avoid hitting the aircraft.
Image of game while in action: This is how the game would look at run time.
 



Output if victory is achieved:
           

No comments:

Post a Comment

1) Introduction to Email

Introduction to Email Introduction Communication, the transfer of information, has been the key element needed ...