RFNXVfwQQPS4J

The sprite crosses the road

Source: licencja: CC 0.

Duszek przechodzi przez jezdnię

You will learn
  • how to create a script that uses conditional statements,

  • built a script that uses conditional statements.

R17xWhc9Xm4Hg1
nagranie abstraktu

Enter the following short code and observe how the sprite moves.

Explain what x and y mean in the entered commands.

RaYV6B7jUKjuO1
Source: GroMar, licencja: CC BY 3.0.
RnJKgHZuHsCtL1
nagranie abstraktu

Your task is to create an animation in which the sprite will cross the roadroadroad when the traffic lighttraffic lighttraffic light turns green.

To create such animation, it is useful to know how to determine the positionpositionposition of the sprite accurately and to create scripts in which, after meeting certain conditions, the sprite performs actions that we planned for it. In this case, the sprite‑cat will cross the road after the traffic light turns green.

Sprite position on the stage:

RvCS0GziCrMXk1
nagranie abstraktu

The stage in Scratch is described by coordinatescoordinatescoordinates; therefore each element on the stage is assigned a pair of numbers or coordinates.

You can see them in the bottom right corner of the stage - when you move the mouse pointer on the stage, its current position is displayed. The horizontal position is x and the vertical position is y.

The size of the stage on which the sprite moves is fixed: the width is 480 points, and the height is 360 points.

Useful for the design of various scripts is the xy‑grid backdrop, which you can download from the Backdrop Library. It facilitates the placement of various elements, according to our ideas.

RycqCS4E2UrFe1
Source: GroMar, licencja: CC BY 3.0.
R12TXxep0qmDa1
nagranie abstraktu

Creating a backdrop

Animation with the sprite crossing the roadroadroad can be carried out in many ways. In this example, you'll see how you can use the backdrop to create a traffic light that changes the colours of lights from green to red.

To create a traffic light standing at the pedestrian crossingpedestrian crossingpedestrian crossing, the Urban2 backdrop from the Backdrop Library was used. In the Scratch graphic editor, the traffic light was added. The changed backdrop was duplicated. On one copy a green colour was added to the traffic light, on the second - red. These backdrops will then be used in animation.

What was the effect of these changes you can see on the slideshow.

RxMJoYyLUstPK1
Pokaz przedstawia jak w programie Scratch dodać światła uliczne do tła. Instrukcja obsługi z poziomu klawiatury: 1. Uruchomienie aplikacji - ENTER, 2. Na każdym ze slajdów czytany jest automatycznie tekst alternatywny po polsku, 3. Przy pierwszym uruchomieniu na pierwszym slajdzie, czytanie tekstu po angielsku - TAB, 4. Przejście między slajdami: do następnego slajdu - TAB, do poprzedniego slajdu - TAB + SHIFT, 5. Przejście do czytania napisu po angielsku - strzałka w górę + strzałka w dół (czyta tekst po angielsku widoczny na slajdzie).
Source: GroMar, licencja: CC BY 3.0.
Task 1
Rho8ysN7epuYG1
nagranie abstraktu

Make your own boards with the traffic lighttraffic lighttraffic light. You can use a ready backdrop from the Backdrop Library or do it yourself from scratch in any graphic program.

Task 2
R1XIrQEx3uV231
nagranie abstraktu

You already have ready boards with a traffic light. Create a script that will change the lights within a specified time interval. Switch on the green light first, and after 6 seconds it will turn red. The script should be started with the green flag button. The lights are changing until the script is switched off using the red button.

Solution

R1RyqNcWKDc9z1
Source: GroMar, licencja: CC BY 3.0.
Task 3
R1OcJ5PUn5ahg1
nagranie abstraktu

You already have a crossing where the traffic lights work. Let's agree that one traffic light will serve the whole crossing for the time being.

Build a script that will place the sprite in front of the pedestrian crossingpedestrian crossingpedestrian crossing. The sprite task is to go through the first and the second pedestrian crossing. At this stage, do not worry if the sprite passes red or green light.

Solution

RsuzTfpiCczgw1
Source: GroMar, licencja: CC BY 3.0.
Hint
R1Uh9j730fouN1
nagranie abstraktu

To place the sprite in front of the pedestrian crossing, you need to find the coordinatescoordinatescoordinates of the point at which the sprite will start moving. Let the sprite always start from the same positionpositionposition (e.g. let it look right).

Then turn the sprite in the direction of the passage. After crossing the sprite should turn again towards the next passage.

RT1zmEw12wk7A1
nagranie abstraktu

Creating your own blocks

Sometimes it happens that we want to repeat a series of commands in the script. Every change you want to make requires careful addition of information in many places. This can be avoided by creating a new variable where we will change the settingssettingssettings only once.

To create a new variable, in the More Blocks category, click on the Make a Block button. Give it a name. Then we attach other blocks to it, which we usually use in several places. In the old script, in place of the removed blocks insert a new block that contains all the commands.

Take a look at the drawing below. It shows how we can create a new block.

RaR4x6bSabRWs1
Source: GroMar, licencja: CC BY 3.0.
Task 4
RCic7w9qWscup1
nagranie abstraktu

The sprite can already pass through the crossing, but it does so incorrectly, without waiting for the green light.

Modify the script in a way that the sprite will cross the road on the green light. When the light is red, the sprite must wait for the light to change.

R7rjw8IZLAf1o1
nagranie abstraktu

If we want the sprite to cross the roadroadroad on the green light, and wait on the red light, we can solve it in the way shown in the picture (using the variable previously defined).

R1479yniFxkKF1
Source: GroMar, licencja: CC BY 3.0.

Example solution

RJ7lWxnk6bmqd1
Source: GroMar, licencja: CC BY 3.0.
Hint
R5op5YaAy8D0O1
nagranie abstraktu

A block of conditional instructions is helpful in solving the problem. The commands contained therein will only be executed if the logical condition is true. We build conditions from blocks from the Sensing and Operators category.

R1cJBNzxzkgBh1
Source: GroMar, licencja: CC BY 3.0.
R1HBXAkJWAN6X1
nagranie abstraktu

Conditional instructions are useful in solving various problems. In Scratch you can also define your own variables.

Exercises

RPn50MQ9KogzV
Exercise 1
Wersja alternatywna ćwiczenia: Which sentence is true? Możliwe odpowiedzi: 1. The stage on which the sprite moves is fixed: the width is 280 points and the height is 360 points., 2. In the graphic editor of Scratch, you can modify backdrops from the Backdrops Library., 3. The blocks of conditional statements are in the Control category., 4. The block wait until ... stops the script until it meets the logical condition.
Exercise 2

Modify the script in which the sprite crosses the road so that it informs us that it is already on the other side of the pedestrian passage.

Exercise 3

Explain in English how you can define new variables.

Exercise 4
R7K8uYraaELC8
Wersja alternatywna ćwiczenia: Indicate which pairs of expressions or words are translated correctly. Możliwe odpowiedzi: 1. instrukcja warunkowa - conditional statement, 2. współrzędne - coordinates, 3. pozycja - position, 4. sygnalizator - traffic light, 5. plansza - road, 6. ulica - board
Source: GroMar, licencja: CC BY 3.0.
R1Pb5JXDqUfuU1
Interaktywna gra, polegająca na łączeniu wyrazów w pary w ciągu jednej minuty. Czas zaczyna upływać wraz z rozpoczęciem gry. Jeden ruch to odkrywanie najpierw jednej potem drugiej karty z wyrazem. Każdy wyraz jest odczytywany. Kolejny ruch to odkrywanie trzeciej i czwartej karty. W ten sposób odsłuchasz wszystkie wyrazy. Nawigacja z poziomu klawiatury za pomocą strzałek, odsłuchiwanie wyrazów enterem lub spacją. Znajdź wszystkie pary wyrazów.
Source: Zespół autorski Politechniki Łódzkiej, licencja: CC BY 3.0.

Glossary

board
board

plansza

RVN3k5D1uV1GA1
wymowa w języku angielskim: board
conditional statement
conditional statement

instrukcja warunkowa

R1BFiHK2PrX211
wymowa w języku angielskim: conditional statement
coordinates
coordinates

współrzędne

RnkbvzDuCIdLH1
wymowa w języku angielskim: coordinates
pedestrian crossing
pedestrian crossing

przejście dla pieszych

RjOzid1Nf7eSZ1
wymowa w języku angielskim: pedestrian crossing
position
position

pozycja

R1Y4aNXPyE5G81
wymowa w języku angielskim: position
road
road

ulica

Rm0zFK3IYPO7d1
wymowa w języku angielskim: road
settings
settings

ustawienia

R1HHcNdjVK1VQ1
wymowa w języku angielskim: settings
traffic light
traffic light

sygnalizator

R1D6dOpdqPHPe1
wymowa w języku angielskim: traffic light

Keywords

conditional statementconditional statementconditional statement

coordinatescoordinatescoordinates

pedestrian crossingpedestrian crossingpedestrian crossing

positionpositionposition

roadroadroad