Scratch e il Google T-rex

Blog Image

Scratch e il Google T-rex

  • Monti Cecilia
  • February 11, 2020

Riproduciamo il gioco di Google con Scratch.

A questo link il gioco da me creato: https://scratch.mit.edu/projects/108312291/

T-Rex Game by CeciuiDownload

Grafica per il gioco

Per prima cosa bisogna sapere che questo videogioco può essere creato tramite il programma Scratch. Il programma ès caricabile sul sito https://scratch.mit.edu/ o può essere utilizzato online alla pagina https://scratch.mit.edu/projects/editor/?tutorial=getStarted

What you can do with Scratch?

With Scratch you can program your own interactive stories, games and animations, and share your creations with other in the online community. Scratch is a programming language that use block to create something. With scratch u can create your games instead of buy them.

Let’s start! Today we will try to create a game that someone may have seen in google chrome (you know the browser by google!) as an easteregg when you don’t have internet connection available. There is a T-REX that is running in a desert and you have to jump to avoid cactus. The more time you run the more points you earn, if you hit a cactus you lose.

T-REX RUNNING

  1. Cambiamo semplicemente il costume sfruttando lo scratch già presente.

First of all we have to import our T-REX images. Go to COSTUMES tab of sprite one, upload costume with this icon (fai vedere icona) choose screenshot_3 and click open. Make the same thing for screenshot_2. Delete scratch costumes. Now u can rename the sprite from Scratch to T-REX. In order to do that click on the blue info icon in the sprite area and write T-REX.

  • With Scratch the game starts when the user click on the green flag that u can see on the top right corner of the stage. So in the script area we have to write code that will run after the user click on the green flag. To do that we can use the green flag block that we find in the EVENTS menu. Drag it on the script area.

Now move the T-REX sprite on the stage area in the bottom left corner. In order to reset our T-REX position every time the game starts we use the GO TO motion block. Go to MOTION menu and drag the GO TO X & Y block in the script area. X & Y have the current T-REX position.

  • Now we must create a VARIABLE to store our score. Go to data menu and click MAKE A VARIABLE button. We can call our variable CURRENT SCORE and initialize to zero (0). To do that drag SET orange block below the previous blue block, choose CORRENT SCORE variable from the drop down menu and write zero after the TO.

  • Now we run out T-REX.

For the T-REX sprite we have created two costumes as you can see at the COSTUMES tab. As u can see the two costumes have different legs. Now we try to continuously change the two costumes in order to emulate T-REX running.

Go to CONTROL block, choose FOREVER block as our T-REX have to run for the entire duration of the game. Inside the forever block we put GO TO FRONT block that u can find in the LOOKS menu, NEXT COSTUME block and WAIT block from the CONTROL menu of few cent of seconds (for example 0.1), to simulate T-REX running legs.

  1. Now let our T-REX jump every time you hit the space bar on the keyboard to avoid cactus.
  • Drag another GRREN FLAG block from the EVENTS menu. Go to CONTROL block, choose FOREVER block and then choose IF THEN block from the same menu and drag it inside the FOREVER block.

In order to make the T-REX jump only when we hit spacebar, we check for the condition of key space pressed that you can find in that sensing menu. (fagli vedere come si inserisce il blocco azzurro)

  • So when the space key is pressed we move the T-REX up and after a short period down again to its start position.

To do that go to MOTION menu, drag GLIDE 1 SECS TO X Y and change the first value to 0.3. From the OPERATION menu drag the ADDITION OPERATOR, as the first addend put Y POSITION from MOTION menu and the second one write 100. Then drag WAIT 1 SECS block from CONTROL menu and change its value to 0.15 and then the same GLIDE block u put before but this time we subtract 100 from the Y POSITION.

TERRAIN

  1. In order to draw our TERRAIN we have to add a new sprite. Upload new sprite from the icon. Upload screenshot_4 and rename it as TERRAIN. Move the sprite on the stage area below T-REX legs in the left area of the stage as I do.
  • Upload a new sprite screenshot_6 and rename to TERRAIN2. Also move this sprite below T-REX legs, this time in the right area of the stage as I do.

Now let’s simulate the terrain moving.
Drag the FLAG GREEN block from the EVENTS menu.

  • Go to MOTION menu and drag the GO TO X & Y block in the script area. From the LOOKS menu drag the SHOW block below the GO TO block.

  • Go to CONTROL block, choose FOREVER block as our TERRAIN have to run for the entire duration of the game. From the CONTROL menu drag the CREATE CLONE OF MYSELF block and drag the WAIT 1 SECS block AND CHANGE THE VALUE TO 3.

Now we have to make something when the sprite as been cloned. In order to do that drag the WHEN I START AS A CLONE block from the CONTROL menu and drag the GLIDE block from the MOTION menu; change the glide duration to 2 seconds and change X value to the negative of the current value (if u have 240 put -240). Now drag the HIDE block from LOOKS menu.

CACTUS

  1. In order to draw our CACTUS we have to add a new sprite. Upload new sprite from the icon (fai vedere icona). Upload screenshot_10 and rename it as CACTUS. Move the sprite on the terrain level in the bottom right corner. If it is too big u can reduce it from the costumes tab. Click the select button, draw an area around the image and reduce it.

Now let’s simulate the cactus moving.

Drag the FLAG GREEN block from the EVENTS menu.

  • Go to MOTION menu and drag the GO TO X & Y block in the script area. From the LOOKS menu drag the HIDE block below the GO TO block.

  • Go to CONTROL block, choose FOREVER block as our CACTUS have to run for the entire duration of the game. Inside the forever block put the WAIT block and put in the round box a PICK RANDOM NUMBER operators. Go to OPERATORS menu and drag PICK RANDOM 1 TO 10 block inside the WAIT block. Then put CREATE CLONE OF MY SELF block below it from CONTROL menu.

  1. Now we have to make something when the sprite as been cloned. In order to do that drag the WHEN I START AS A CLONE block and drag the SHOW block and then the GLIDE block from the MOTION menu change the glide duration to 2 seconds and change X value to the negative of the current value (if u have 240 put -240). Now drag the HIDE block from LOOKS menu.
  • The same steps must be done for the CACTUS2.

GAME OVER

  1. In order to draw our GAME OVER we have to add a new sprite. Upload new sprite from the icon. Upload screenshot_9 and rename it as GAME OVER.
  • When u have a game over? When the T-REX hit the cactus. We have to program that.

Go to T-REX sprite and drag the FLAG GREEN block from the EVENTS menu. Go to CONTROL menu, choose FOREVER block and put into IF THEN block. Below IF put sensing block like TOUCHING COLOR block and select the cactus colour clicking inside green pixels of the cactus sprite (dire: in this way). Now put into IF THEN the BROADCAST block from the EVENTS menu. Now you must create a new message called GAMEOVER.

Now go to GAME OVER sprite, drag  the FLAG GREEN block from the EVENTS menu and below drag HIDE block from LOOKS menu. Then drag WHEN I RECEIVED block from EVENTS menu and select GAME OVER. Then drag SHOW block from LOOKS menu and then drag STOP block from CONTROL menu and select ALL.

SCORE

Now we can add the score on our game. Go to T-REX sprite and drag the FLAG GREEN block from the EVENTS menu. Go to CONTROL block, choose FOREVER block. Then go to DATA menu and drag CHANGE CURRENT SCORE by 1. Now go to CONTROL menu and choose and drag WAIT SECS and change values to 0.1.

RE-START

  1. In order to make our game start again after game over we have to add a reset buttom. To do that create a new sprite uploading screenshot_1 and rename it as RESTART.
  • Now drag the FLAG GREEN block from the EVENTS menu and drag below HIDE block.

  • Then drag WHEN I RECEIVED block from EVENTS menu and select GAME OVER and below drag SHOW block from EVENTS menu. After that drag STOP block from CONTROL menu and select ALL.

RESET

  • Go to T-REX sprite. First of all we must create a NEW MESSAGE on EVENTS menu. Go to EVENTS menu, drag WHEN I RECEIVED block and select NEW MESSAGE (write reset).

  • Now drag FOREVER block from CONTROL menu and then drag IF THEN block in the same menu. Now put on the below IF… TOUCHING COLOR block from SENSING menu and select the right colour clicking inside green pixels of the cactus sprite (dire: in this way). Then choose BROADCAST block in the EVENTS menu and select GAME OVER.

Now drag WHEN I RECEIVE RESET block from EVENTS menu. Then drag FOREVER block and IF THEN block. Now put on the below IF… KEY PRESSED block, from SENSING menu, and select SPACE. After that drag GLIDE 1 SECS TO X Y and put in the Y box the ADDITION OPERATOR, from OPERATIONS menu, putting on the first box the Y POSITION block from MOTION menu and in the second box write 100. Then drag WAIT 1 SECS block from CONTROL menu and then the same GLIDE block u put before with the same options.

Now drag the same FOREVER block we can put just now. Go to MOTION menu and drag the GOT TO X & Y block in the script area. Then drag FOREVER block and into that put CHANGE BY 1 block from VARIABLE menu and below choose WAIT 0.1 SECS block from CONTROL menu.

Now drag the same FOREVER block we can put just now and below drag SET TO 0 block from variable menu. Then drag FOREVER block. Now drag GO TO FRONT block and NEXT COSTUME block from LOOKS menu. At the end drag WAIT 0.03 SECS from CONTROL menu. Now go to CACTUS sprite.

  • Now drag FOREVER block from CONTROL menu. Go to MOTION menu and drag the GOT TO X & Y block in the script area. Then drag HIDE block from LOOKS menu. From CONTROL menu choose FOREVER block and inside that drag WAIT SECS block and put below WAIT the PICK RANDOM 1 TO 10 block from OPERATORS menu. Then drag CREAE CLONE OF block and select MYSELF.

  • The same steps must be done for the CACTUS2 sprite. Try do it by yourself.

h)     Now go to RESTART sprite

  • Now drag WHEN THIS SPRITE CLICKED block from CONTROL menu and below drag BROADCAST block and select RESET.
  • Then drag WHEN I RECEIVE block from CONTROL menu and select RESET and then drag HIDE block from LOOKS menu. The same steps must be done for the GAME OVER sprite. Try do it by yourself.

Condividi questo post