1 of 25

Space War�(The concept of cloning)

Vineet Srivastava

2 of 25

So, what is cloning

  • A block, which allows us to create many copies of a sprite. You can think these are like the twins of the sprite. There are three relevant blocks for cloning in the CONTROL section.

2

www.wibyte.in

Control

The three relevant blocks

3 of 25

Let’s start with an example

  • Say we want to build a sprite which can shoot a lot of Weapons.

3

www.wibyte.in

4 of 25

Create Knight and KnightWeapon sprites

4

Knight is a built-in sprite, knight Weapon we painted. Make sure the painted sprite is in the centre of the canvas.

www.wibyte.in

5 of 25

Getting Knight to shoot Weapons

5

After the flag is clicked, If I click SPACE bar, Create a CLONE of the sprite ‘KNIGHT Weapon’

When a CLONE is created, make it go to the KNIGHT and then move.

Every time the space bar is clicked, the ‘knight’ appears to shoot a new Weapon.

www.wibyte.in

6 of 25

Speed of the Weapons

  • We can control the speed of the Weapons using a variable.

6

With this variable, we can control how fast (how many steps) the Weapon moves

Note, this is NOT a new loop. Just a replacement of 10 with the variable knight Weapon speed.

www.wibyte.in

7 of 25

DELETE the clones too

  • We need to add code to delete the clones. Notice, we have added code to delete the clone when reaching the edge.

7

One the sprite reaches the ‘EDGE’, delete the clone.

This is extremely important as every clone takes up memory. If we do not delete them, eventually the program will crash.

www.wibyte.in

8 of 25

A point of caution

  • Notice, in the previous slide, we have added the code to delete the sprite.
  • One common mistake students make is that when they bring in the sprite first, it is ‘already’ near the edge.
  • Hence, when the clone is created, the clone immediately gets deleted, and it looks like the clone never got created.
  • Simple remedy is to ensure that the sprite is not touching the edge when you first create it.

8

9 of 25

Moving the knight

  • Use UP and DOWN Arrows to move the knight.
  • Use a variable ‘knight speed’ to control HOW fast the knight moves.

9

www.wibyte.in

10 of 25

Add Knight’s Enemy – A retro robot

10

Note: We changed the SIZE to 40%, and used FLIP HORIZONTAL to make the Retro Robot face the knight.

www.wibyte.in

11 of 25

Code to move Retro Robot

  • Every ‘retro glide time’ sec, it glides to a ‘random’ Y position.
  • The smaller the ‘Retro Glide Time’, the faster the movement of the retro robot. .

11

www.wibyte.in

12 of 25

Keeping track of the score

12

Add this to the KnightWeapon code

Variable

If the Weapon hits RETRO

Remember to delete

www.wibyte.in

13 of 25

Let’s give some ammunition to RETRO also

  • Duplicate the Knight Weapon Sprite and give it a new costume and name.

13

www.wibyte.in

14 of 25

Make changes to the RETRO Weapon CODE

14

On becoming a clone, go to RETRO ROBOT

Move according to RETRO ROBOT speed.

If touching knight, increase retro score.

Negative, because RETRO Weapon must move LEFTWARDS

www.wibyte.in

15 of 25

Creating clones of RETRO Weapon

  • Make retro robot fire a Weapon every ‘ retro Weapon wait’ seconds.

15

Every ‘retro Weapon wait’ seconds, Create a clone of RetroWeapon.

Variable added

www.wibyte.in

16 of 25

Coding the ENDING Conditions

  • Game gets over in one of these three events:
    • Time gets over – Nobody won
    • Knight got hit too many times – Retro won
    • Retro got hit too many times – Knight won.

16

www.wibyte.in

17 of 25

Implementation

17

TIME OVER

RETRO REACHED ITS TARGET AND WON

KNIGHT REACHED ITS TARGET AND WON

Relevant Variables

www.wibyte.in

18 of 25

Receiving Ending Broadcast messages

18

www.wibyte.in

19 of 25

And you are all set!

  • This is a very simple game, built mostly to illustrate the concepts.
  • There are many variations you can add, for example, creating an army of robots, pointing the robots’ shots towards the shooter and so on.
  • Try these and more in your independent activity.

19

www.wibyte.in

20 of 25

Extra Innings

20

21 of 25

Cloning is NOT the same as STAMPING

  • Previously (in Class 6) we saw that in the PEN block, we have a STAMP option.
  • STAMP creates a ‘stamp’ of a sprite.
  • This is different from cloning, as CLONE is like ‘a real working’ sprite, not just its picture or stamp.
  • This mean, clones can do stuff for us.

www.wibyte.in

22 of 25

Ideas to make your game more difficult (1)

    • Make the retro Weapon point in the direction of the KNIGHT using

22

Note: For this, RETRO Weapon SPEED must be made more than 0. This is unlike earlier when we did not touch the direction of the Weapon and hence had to make it move <negative> number of steps

www.wibyte.in

23 of 25

Ideas to make your game more difficult (2)

    • Make More robots.
      • Duplicate RETRO Weapon and RETRO ROBOT sprites and give them new costumes.
      • Remember to update the conditions.

23

www.wibyte.in

24 of 25

Ideas to make your game more difficult (3)

    • Create an army of robots that MOVE towards the Knight.
      • CLONE the retro robot (not the retro Weapon).
      • When it starts as a clone, move towards the Knight.

24

www.wibyte.in

25 of 25

Ideas to make your game more difficult (4)

  • Add obstacles.
    • You can add some sprites in the middle that act like obstacles. Perhaps they can block the knight’s Weapons for example.

25