• Homework
  • Thirds
  • Upper Thirds
  • Fourth
  • Divisions
    • ICT
    • Computer Science
  • Fifth
  • Lower Sixth
  • Upper Sixth
    • Upper Sixth Theory
  • Blog
  • Careers
  • Unity
  • Networking

Setting up the Ball Sprite blocks


Most of the game will be written using blocks attached to the Ball sprite.  Our first task is to make it move.  Using two control blocks, the 'When Flag Clicked' and 'Forever' we make a simple script that will make the ball bounce all over the screen.

Try it and make sure you have a bouncing ball.  It doesn't work with the paddle yet.
Picture

Picture
To make the ball bounce off the bat we detect that it's touching the colour of the bat, then make some simple alterations to the direction to have it bounce back with a little bit of randomness thrown in to make the game more challenging.
This code isn't really as good as it should be.  A more sophisticated version of the game will vary the angle predictably but will depend on which part of the bat was hit, with more extreme angles at the edges.

Life, death and scores

Before we go any further we'll need to set up a variable to hold the score.  

Find the 'Data' part of the blocks menu and click to create a new variable.  Call it 'lives'
Picture

Picture
The final set of blocks starts by setting the score to 3 when the game starts.  Then it detects that the ball has touched the red 'death' colour and subtracts a life.

If there are no lives left, the game just stops.  It woudl be good to have something more sophisticated, perhaps a message or animation - both when a life is lost and when the game is over.

Next:  making the paddle move

Powered by Create your own unique website with customizable templates.