Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

4. Générer des ennemis

Transcript

In this screencast, you will program the game to spawn enemies that bounce around the screen.

This screencast will show you how to complete the programming, then you’ll get to try it for yourself. First, you need to add and resize an enemy.

The example project will use a shark, but you can select any sprite you like.

Now that the sprite is added, you need to program it to bounce around the screen.

Remember how you did this for the escape game on day 5?

First, And finally, you added a flag event to start the motion.

Great! Remember, computer scientists like you often break down a problem into smaller, more manageable pieces and solve them one by one.

That method makes even the largest problems easier to solve.

Now that the sprite is bouncing around the screen, you will need to clone, or spawn, new enemies.

In previous games, you may have manually copied a sprite.

But, one of the great things about computer science is that you can program the computer to do things for you. While you could manually right click to copy a new sprite each time you wanted to make the game harder, it’s much easier to get the computer to do it for you with a “clone” block. To start, try cloning a sprite once.

Do this by dragging out a “create a clone of myself” block and a “when I start as a clone” block. Even though the “when I start as a clone” block is in control, you may recognize by its shape and the word “when” that this is a type of event.

Drag the code you previously created onto the “when I start as a clone” event, and drag the “create clone of myself” block to the flag event.

Now this reads: Test it now and watch what happens.

Awesome! When the flag is clicked, a clone starts, but you’ll notice that the other sprite doesn’t do anything. You’ll fix that in a minute. First, test how the “clone” block works with a “repeat” block. Go to control, drag out a “repeat 10” block, and place it around the “create clone of myself” block.

What happens when you test it?

When this loop runs now, the computer should create 10 clones of the enemy sprite!

Good! In the next screencast, you'll program the sprites to clone only if a point is scored.

There is still a bug in this program. There is a sprite on the screen that doesn’t move!

This happens because only the clones are programmed to move.

There are a few ways to fix this, but an easy fix is to hide the sprite that isn’t moving, and show only the sprites that are cloned.

To do that, add a “hide” block to the end of your flag event.

This will hide the first sprite, but it will also hide all of the clones. Add a “show” block to the “when I start as a clone” event so all the clones will show up.

Now all of your clones move!

arrow_backward Retour
Suivant arrow_forward
Instructions
  1. Ajoute un lutin et redimensionne-le.
  2. Fais rebondir le nouveau lutin dans la scène.
  3. Fais en sorte que le lutin Ennemi se déplace dans des directions aléatoires.
  4. Fais en sorte que le lutin Ennemi se clone lui-même.
  5. Organise ces blocs avec les éléments de ta pile de blocs existante.
  6. Ajoute des blocs "Cacher" et "Montrer".