Saturday, March 21, 2015

Create a game with Scratch on Raspberry Pi

http://www.linuxuser.co.uk/tutorials/create-a-game-with-scratch-on-raspberry-pi

Learn the basics of coding logic by creating a squash-like Pong clone in Scratch that you can play at the end of the day

While Scratch may seem like a very simplistic programming language that’s just for kids, you’d be wrong to overlook it as an excellent first step into coding for all age levels. One aspect of learning to code is understanding the underlying logic that makes up all programs; comparing two systems, learning to work with loops and general decision-making within the code.
Scratch strips away the actual code bit and leaves you with just the logic to deal with. This makes it a great starting point for beginners, separating the terminology so you can learn that later on when you choose to make a proper program. It’s also included on every copy of Raspbian.
Use blocks that represent coding to build your game or animation
Use blocks that represent coding to build your game or animation

Step-by-step

Step 01

The first sprite
Opening up Scratch will display a blank game with the Scratch cat; right-click on it and delete to remove it. Click the Paint New Sprite button below the game window and draw a slim rectangle as the bat using the square drawing tool. Click Set Costume Center so that Scratch knows the basic dimensions of your bat; drag it to the centre of the sprite if needs be.

Step 02

Move the bat
Click OK, and name the new sprite ‘Bat’. Select the Control block from the top-left menu, and drag the When Space Key Pressed block into the script area. Change Space to Up arrow from the drop-down menu, then select the Motion options and drag the Change Y By 10 block to connect to the key pressed block.

Step 03

Reverse the direction
Whenever we press up, the bat will move up by ten pixels, as set. By repeating the process using the down arrow and setting Change Y To -10, we can also make it move down as well. Move the rectangle to the left side of the screen. This will be our starting position.

Step 04

Create the ball
Create a sphere from the new sprite menu like we did with the bat, including setting the Costume Center. Name it ball, and bring the When Green Flag Clicked block from the Control menu into the scripts pane. Add the go to x:0 y:0 block underneath it so that whenever you click the green flag it resets to the ball.

Step 05

Move the ball
Find the Forever block in the control menu, and attach it under our existing block on Ball. Then, go back to the Motion menu and add Move 10 Steps so that the ball moves around the screen. Add the If On Edge, Bounce block from Motion below that so that it will stay within the playing field.

Step 06

Hit the ball
Drag the If block from Control to below If On Edge, and then add touching from Sensing to the empty space on the If block. From the drop-down menu, select Bat so that it will interact with our bat sprite. Add one of the Turn 15 degrees blocks from Motion to the If block, and change it to 180.

Step 07

Simple bouncing
Pressing the green flag now, the ball will bounce between the left and right edges of the screen, or off the bat if it comes into contact with it. We can make it slightly more interactive to make use of the moving bat, and more like Pong.

Step 08

Random bouncing
Go the Operators menu and select the first value, blank + blank. Drag it to where we have 180 degrees in the turn block, and add 180 to the first blank space. Place the Pick Random 1 to 10 block in the second space and change the values to -10 and 10 so that whenever the ball hits the bat, it bounces off at a random angle between 170 and 190 degrees.

Step 09

Further developments
While you now have a functional game of sorts, you can also add in a second player to make it truly Pong-like, and add a scoring system by having a number increase when the ball hits one of the sides.

No comments:

Post a Comment