Breakout Game Help

Discussion specific to projects ideas and support.
Post Reply
gamemaker99
Posts: 16
Joined: 09 Oct 2010, 16:50

Breakout Game Help

Post by gamemaker99 »

I am trying to make a breakout style game for the NXT in NXC(I am still learning)
I have all the wall bounces and paddle angles worked out, but I am at a loss as to how to program the bricks.
So far I have

Code: Select all

int paddleleft=0, paddleright=20 ,middle,definer;
float velocx=5, velocy=5, ballx=80, bally=32;
bool cracked=false;
task paddle()
{
 while(true)
 {

 if(ButtonPressed(BTNLEFT,true))
  paddleleft--;
 if(ButtonPressed(BTNRIGHT,true))
  paddleleft++;
 paddleright=paddleleft+21;
 middle=paddleleft+11;
 Wait(50);
}}

task display()
{
 while(true)
 {
 ClearScreen();
 CircleOut(ballx,bally,1);
 LineOut(paddleleft,5,paddleright,5);
 if(!cracked)
  RectOut(0,54,10,10);
 Wait(100);
}}

task ball()
{
 while(true)
 {
 if(ballx<0)
  velocx = -velocx;
 if(ballx>100)
  velocx= -velocx;
 if(bally>64)
  velocy= -velocy;
 if(bally<=5&&ballx>=paddleleft&&ballx<=paddleright)
  {
  definer=ballx-middle;
  velocy= -velocy;
  velocx=definer;
 }

 ballx+=velocx/5;
 bally+=velocy/5;
 Wait(50);
}}
task brick()
{
int brickx=0,bricky=54;
while(true)
 {if(!cracked){
 if(ballx<brickx+10&&ballx>brickx&&bally == bricky)
  {
  cracked=true;
  velocy= -velocy;
 }
 if(ballx<brickx+10&&ballx>brickx&&bally == bricky+10)
  {
  cracked=true;
  velocy= -velocy;
  }
 if(bally<bricky+10&&bally>bricky&&ballx == brickx)
  {
  cracked=true;
  velocx= -velocx;
  }
 if(bally<bricky+10&&bally>bricky&&ballx == brickx+10)
  {
  cracked=true;
  velocx= -velocx;
 }}
}}


task main()
{
Precedes(paddle, display, ball, brick);
}

But that is just one brick.
To make about 30 bricks, would I need to make 30 different tasks for the different blocks
or is there an easier way?
"To be able to ask a question clearly is two-thirds of the way to getting it answered."
-John Ruskin-
kvols
Posts: 29
Joined: 14 Oct 2010, 22:09

Re: Breakout Game Help

Post by kvols »

Hi there

Using a task (or thread, as it may be called in other programming languages) for the ball and for the controller is kinda neat. Both the ball and the controls have their own "life". But modelling the bricks as tasks is not such a great idea, as you point out yourself. It doesn't work well.

Instead, you could perhaps think of the playing field as a table of rows and columns, that can be empty or contain a brick waiting to be hit. Given the ball position, you could calculate where the ball is in your "brick table". The "brick table" would probably have a much lower resolution than the pixels on the display, eg. 5 by 10 bricks.

In NXC (and most other programing languages), such a table is called an array. In this case a two-dimensional array.

To figure out if the ball has hit a brick, you would then translate the ball position to a "brick position". In other words: The ball task will move the ball, translate the ball position to a table position, and see if the table position is empty or occupied with a brick, and behave accordingly (destroy the brick, change course, make a sound, ...).

The array is usually initialized with numbers or codes, signifying if the positions are occupied by bricks or empty cells, eg. 0 meaning empty and 1 meaning a brick. You could even add more meanings to the numbers, such as 5 meaning "a brick that needs 5 more hits to be destroyed", but that's entirely up to your interpretation of the array values...

So ... have a look at arrays, do some tests and try to figure out how they work. Figure out hos to translate between the balls display position and its brick cell position.

Arrays pretty neat, and can be used (and misused!) for all sorts of neat tricks!

Happy coding! ;-)
gamemaker99
Posts: 16
Joined: 09 Oct 2010, 16:50

Re: Breakout Game Help

Post by gamemaker99 »

Wow :shock:
Today, I had almost the same idea as you just said,using rows and columns to determine which brick the ball hit.
I implemented it, finished the game, and posted it in the NXT Games thread.
Then, I saw your reply to my message
Great Minds Think Alike
"To be able to ask a question clearly is two-thirds of the way to getting it answered."
-John Ruskin-
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests