Arcade game on Raspberry Pi using Scratch

Arcade game on Raspberry Pi using Scratch

6th November 2019 0 By Tanna TechBiz

About Scratch

  • With Scratch, you can program your own stories, games, and animations  and share your creations with others community.
  • Scratch helps young people learn to think creatively.
  • Scratch is a project of the Lifelong Kindergarten Group at the MIT Media Lab. It is provided free of charge.
  • Scratch is designed especially for ages 8 to 16, but is used by people of all ages. Millions of people are creating Scratch projects in a wide variety of settings, including homes, schools, museums, libraries, and community centers.

Interface

  • The Scratch interface has three main sections 1.) stage area 2.) blocks palette 3.) coding area to place and arrange the blocks into run able scripts.
  • The stage area features the results and all sprites thumbnails being listed in the bottom area. The stage uses x and y coordinates, with 0,0 being the stage center.
  • The bottom right area is your Sprite List. You can think of sprites as the characters in your game. They’re images that you can make do things, such as move around or change their appearance. For now, there’s just the cat, which has the name Sprite1.
  • One way to think of sprites is like the characters in a game. Each sprite can have a number of costumes, which are different pictures of it. If the costumes look fairly similar, you can create the illusion of animation by switching between them. Your cat sprite comes with two costumes, and when you switch between them, it looks like the cat is running.
  • With a sprite selected at the bottom of the staging area, blocks of commands can be applied to it by dragging them from the blocks palette into the coding area. The Costumes tab, allows users can change the look of the sprite in order to create various effects, including animation. The Sounds tab allows attaching sounds and music to a sprite.
  • When creating sprites and backgrounds, users can draw their own sprite manually, choose a Sprite from a library, or upload an existing image .

Creating Scripts

  • Clicking blocks in the Blocks Palette is one way to give commands to Scratch, but you’re not really programming. To start to create a program, you drag blocks from the Blocks Palette and drop them in the Scripts Area in the middle of the screen.
  • You don’t have to align them perfectly: Scratch snaps them together for you if they’re close enough when you release the mouse button. You put your blocks in the order you want Scratch to run them, starting at the top and working your way down.
  • It’s a bit like making a to-do list for the computer. A group of blocks in the Scripts Area is called a script, and you can run it by clicking anywhere on it. Its border flashes white, and you’ll see the cat move around the Stage as you’ve instructed it to.
  • When you add multiple sprites, each sprite has its own Scripts Area and scripts there to control it. If you drag a block lower down in the script, it is separated from the blocks above it and carries all the blocks below it with it. If you want to delete a block or set of blocks, drag it back to the Blocks Palette on the left.

Arcade Game

  • Here we control blocks that enable you to coordinate the actions of different sprites with each other and with the player. It assumes a basic understanding of the Scratch interface and how you use blocks to build a script.
  • Whenever you’re programming, you should give things meaningful names so that you can easily understand what your program does. Scratch gives your sprites names like Sprite1 and Sprite2, but you can rename them. To rename a sprite, click its name above the Scripts Area and then type its new name.
  • First of all you need to take new sprite for your game for that you can choose sprite or you can draw your own sprite  from Paint.
  • Using the green flag to run scripts : One of the Control blocks is particularly useful for starting your game and synchronizing your scripts across all your sprites. Above the Stage are two buttons: a green flag and a red stop button. The green flag is used to start scripts running, and you can use a Control block to detect when it’s clicked. This Control block has a curved top on it because no other block can go
  • As you can see in the above image, we take one sprite by which we can fire the arrow towards enemy (note that here in image there are two different scrip is there you can choose any of one.)
  • Here first of all we set the position of sprite and after that we take infinite loop that is forever loop to run our game continuously. Forever loop is like while loop in other programming language.
  • In this sprite we want to move it continuously in positive x direction without changing its y direction from left to right. So for that we put a condition in our block as you can see in above image.
  • In other script we need to move our sprite by using left and right arrow key from keyboard.
  • Now look in to our fire bullet that nothing but a clock hand sprite which is available in scratch.
  • Here we done that when game starts it become invisible. And when one will press space from keyboard it will be fired means it will move from the launching sprite towards positive y direction or you can say upward side after that it will automatically be invisible(YOU CAN SEE SCRIPT IN ABOVE IMAGE).
  • Now as you can see in above image we take a sprite of dragon again it is inbuilt sprite of scratch and we take 2 costumes of that one for regular movement. i gave dino1 nameto it and another costume that appears when our dino1 costume strike with our arrow. I gave dino2 name to our 2nd costume.
  • Now come to script for this you can see in above image.
  • Here first we need a dino1 at the starting of game so here we switched to dino1 one by using switch block.
  • Than we take a random x position from -180 to 180 and y position from 0 to 120 where our dino1 one will appear.
  • After that we take one forever loop in which we put condition that if dino1 will strike with the arrow than dino2 will appears and score will increased by 1 and after that it wait for some sec and again dino1 will appear as discussed above. You can see all script in above image.

Changing the Background

  • we’ve been working with a plain white Stage, but you can change the background. The Sprite List contains an entry for the Stage. The Stage can have scripts and different images, just like a sprite can. The Stage’s images are called backgrounds rather than costumes.
  • Click the Stage’s icon in the Sprite List, and then click the Backgrounds tab at the top of the Scripts Area.
  • You can choose to paint a new background, using the built-in art package (see the section “Drawing Sprites in Scratch,” later in this chapter). Alternatively, you can use an existing image file. Scratch comes with a number of backgrounds you can choose from, or you can use your own photo. Scratch can open images in .jpg, .gif, or .png format. For this background.