Session 4: When is the pong finished?

Work plan

  • Control of the end of the game
  • The variable concept
  • The lives in the game

We do not always agree with the racket

When we play in the pong and we move the racket, we may not be right to play the ball. We have to get control when this happens to know that the game is over and, as we will see later, decide whether we can continue or not.

Icon tasksLet’s do it step by step

    • We enter our Scratch account and make a copy of our project m2 challenge31  and we change the name to m2repte41
    • In this project we surely have two rackets, one vertical and one horizontal. We eliminate the vertical and we stand only with the horizontal racket
    • We will add the “line of death” to the bottom of the screen. When the ball touches this line the game will end
    • We will use the Sensors to detect when there is this interaction between the ball and the “line of death.” To do so, first of all we have to create a new character that will be precisely this line and that it must be of the same length as the width of the screen and the thickness and color that we like the most. We need to achieve this result

Line of death

  • Now only the interaction between the ball and the line of death is necessary. We can do it in different ways, using conditionals or with an iterative structure (the two possibilities we saw in module 1 in session 9). Whether we use a possibility like the other, we will have to add the sensors that control the interaction of the ball with the “line of death”, considering it as a character or taking into account its color
    Conditional
    Iteration

Icon challenge Challenge 1:

The game also ends when the ball is at a height lower than the racket, although it does not reach the “line of death”, since there is no possibility of reacting and recovering. Modify the m2repte41 project that we have open so that the game ends when the ball is lower than the racket.

Icon track Track: We have to check if the coordinate “y” (vertical position) of the ball is less than the “y” coordinate of the racket.


We want to play more than once …

That the pong is finished at the moment when we can not return the ball, it is not fun. We have to have more opportunities to continue playing for a while and thus we will be more capable. To achieve this, we will add “lives” in our game.

These “lives” have to be decreasing each time we lose, that is, their value will vary. This means that if we start the game with 4 lives and we lose, we will stay only in three and so on. In order to control the variation of this value, we must use a new concept, the one of variable that is within the Data block .

Block data

Icon tasksLet’s do it step by step

  • We continue working on our m2repte project41
  • Let’s go to the data block and create a variable with the name of lives (or any other name we want, although it is recommended to put names that relate to what we want to keep in order to make the program more clear to follow).
  • Once we have created a variable, the blog offers the following possibilities:
    Variable creation
  • Once the variable has been created we have to give it an initial value that will be the amount of opportunities we have to lose before the game is finished.
  • So that each time we start the game the variable “lives” start from the initial value we have decided, we must place the corresponding block to the assignment just after the green flag.
    Variable creation
  • The value of the variable appears on the screen if we have marked it when defining it. We can change the way we view it when we are running the project by clicking on it.

Icon challenge Challenge 2:

We add in our m2repte41 project the control of lives, so that they are decreasing every time the ball does not bounce in the racket. We will not control now the end of the game when the lives are over , we will do it in the next session.

Icon trackTrack: once the variable “vides” has been created and we have established its initial value, we must reduce our lives each time we lose. At Scratch there is no resting operation, we can do it equally by adding a negative number (-1). We will have to use an iteration to repeat the action.


Icon project Our project grows …

It is time to review the description we have made in our pinball project considering that we need to decide and add the concept of “lives.” How many opportunities do we want to give to the players? What do we want to happen when the lives end?


Glossary icon Because we are programmers and programmers we talk about …

  • Variable: These are spaces where we can store values that will be modified (variant) throughout the program, that is, they do not have a fixed value.