Skip to content

Please update your browser

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

5. Den Spielstand verfolgen


In this screencast, you’ll create a variable to hold the player’s score, and program the enemies to spawn or clone each time a point is scored. Watch the screencast first to learn how to do it, then try it on your own.

Because the variable will store the points scored by the player, click on your player sprite.

Now, you need to create a variable to hold the score.

Variables are an important computer science concept. You can think of them as a way to store information. You can use variables to hold any sort of information, like numbers, letters or words. Once a value is stored in a variable, you can add to it, subtract from it, and use it in many different places while only needing to update the variable in one spot.

Click on the data menu, then choose make a variable.

You could call this variable whatever you want, but remember that computer scientists often work together, so it’s good to make sure that anyone who looks at your code can understand it easily. So, call this variable something that makes sense, like “score,” and make sure that you select “for all sprites.”

Now that you’ve created a place to store the score, you need a way to add to it.

The player will get a point if the sprite touches the finish line.

To make that happen, go to the control menu and select an if statement.

Then, click the sensing menu, add a touching condition, and change the value to “finish line.”

Now this reads: “If touching finish line then...”

What should the “then” part of this statement be?

Well, if the sprite touches the finish line, the score should go up by one point.

To do that, go to data and add a “change score by 1” block to the if statement.

Now, you need to tell the computer when to check this if statement.

You want to check if the sprite is touching the finish line for the entire game, so go to events and add a “when flag clicked” block. Then, go to the control menu and add a forever loop to ensure that it runs for the entire program.

Test your code now.

Good! You should see that the sprite gets a point added when it touches the finish line.

Now, if the sprite touches the finish line, the score goes up by 1, but it never resets. To fix that, add a “set score to 0” block to the beginning of this stack to ensure that the variable, or storage location, is reset to 0 at the start of each game.

Try the game now!

Oh wow, even though there are enemies, they don’t stop the player from moving. Remember, that’s one reason to test your code often, so you can find bugs like this one and fix them along the way.

That’s how computer scientists find the bugs that could keep their programs from working the way they want them to. To fix this bug, you need to tell the sprite to stop moving if it touches the finish line, or if it’s touching the enemy. To do that, add an “or” operator to the “repeat until” block and add a “touching” block.

The statement now reads: “When space key pressed, repeat moving forward until touching the finish line or touching a shark, then go back to the start.”

Test it to see if that works.

Good! But there are a lot of clones on the screen!

A lot of games start out easy to play, and get more difficult as the player improves. This way, anyone from a beginner to an expert can play and feel challenged. To make this game get harder as it’s played, you can start the game with one enemy and clone new ones each time a player scores a point.

Go to control and drag out a “create clone” block.

Place it after the “change score by 1” block, and change the value to the name of your enemy.

Now, every time the player scores a point, the code creates a clone.

If you’re still cloning 10 sprites, it will be nearly impossible to get to the finish line. Go back to the enemy sprite, and remove the “repeat” block so that only one clone is created at the beginning of the game.

If you test your code now, you should see that you have one sprite to start, and another one appears each time you score a point.

Ask your neighbor to try your game and see how many points they can score!

There are a lot of steps in this screencast, but remember to break down problems and solve them one step at a time, and you should be fine – that’s how computer scientists like you solve problems. If you get stuck anywhere along the way, ask your neighbor for help or put up your sticky to get the attention of your CS First Guru.

arrow_backward Zurück
Weiter arrow_forward
  1. Erstellt eine Variable „Punktestand“ für die Spielfigur.
  2. Fügt eine „falls“-Anweisung zur Variable „Punktestand“ hinzu.
  3. Fügt einen Block „Wenn Flagge angeklickt“ und einen Block „wiederhole fortlaufend“ hinzu, damit die Bedingung immer eintritt, nachdem die Flagge angeklickt wird.
  4. Fügt einen Block „setze Punktestand auf 0“ hinzu, um den Fehler zu beheben, bei dem der Punktestand nicht zurückgesetzt wird.
  5. Lasst die neue Figur auf der Bühne umhergehen.