Breakout

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Breakout

dmoeller
I am making a breakout game for project 9. My issue is when I try to make an array of blocks. I get errors when I try it this way but Im wondering if I can just get help with my logic. here is my code

                while (row < 4)
                {
                        while (col < 8)
                        {
                                let block = Block.new(15+(60*col), 10+(30*row), 60);
                                let blockArray[numBlocks] = block;
                                let col = col + 1;
                                let numBlocks = numBlocks + 1;
                        }
                        let row = row + 1;
                }

I do this so that later I can check if a particular block has been hit with the ball.
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

cadet1620
Administrator
col needs to be initialized to 0 for each iteration of "while (row < 4).

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

dmoeller
in my method for checking collisions with the block. I get the errors

line 215 expected ;
line 215 expected statement

Mycode for that line is "let blockX = blockArray[temp].getX();" and the line above that is my declaration of a while loop "while (temp <= numBlocks){"

Im not sure why it needs a ; or why it says it needs a statement when it has one.
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

cadet1620
Administrator
dmoeller wrote
"let blockX = blockArray[temp].getX();"
The problem is that Jack is an untyped language.  Since blockArray[temp] does not have a type, Jack cannot know what object's getX() should be called.

You need to use two statements to do this.

    var Block block;

    let block = blockArray[temp];
    let blockX = block.getX();

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

dmoeller
I get a VMEmulator error "'That' segment must be in the Heap or Screen range in Breakout.new.59"

I get no compiler errors. Im not sure what this error means. Im not sure which part of my code to include so here is all of my Breakout constructor:

   // Constructs a new Breakout Game
    constructor Breakout new()
        {
        let paddle = Paddle.new(0, 230, 60);
                let ball = Ball.new(10, 100);
        let direction = 0;
                let col = 0;
                let row = 0;
                let numBlocks = 0;
                let vert = true;
                let hor = true;
                let lose = false;
               
                //while loops for making the block array
                while (row < 4)
                {
                        let col = 0;
                        while (col < 8)
                        {
                                let block = Block.new(15+(60*col), 10+(30*row), 60);
                                let blockArray[numBlocks] = block;
                                let col = col + 1;
                                let numBlocks = numBlocks + 1;
                        }
                        let row = row + 1;
                }
        return this;
    }
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

cadet1620
Administrator
"'That' segment must be in the Heap or Screen range" means that a pointer had an invalid value, most likely 0.

blockArray needs to be initialized by creating a new Array large enough to hold all the Block pointers.

I suggest that you add numRows and numCols fields then you can
    let blockArray = Array.new(numRows * numCols);
and use numRows and numCols in the while statement limits, too.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

dmoeller
I implemented to code to check for a collision with a block and it is super tricky and I know I got it wrong but I need help figuring out what is correct.Before this segment of code, the blocks formed just fine in 8 columns and 4 rows. I have included a screenshot of what they look like after this segment, and the red box shows which blocks do what I actually want them to. all other blocks just sit there.

Here is the code I have:

//if the ball collides with a block
                let temp = 0;
                while (temp < numBlocks)
                {
                        let tempBlock = blockArray[temp];
                        let blockX = tempBlock.getX();
                        let blockY = tempBlock.getY();
                        let blockSize = tempBlock.getSize();
                        if (tempBlock.show())
                        {
                                if (((ballY + ballRad) > blockY) & ((ballY - ballRad) < (blockY + (blockSize/2))))
                                {
                                        if (((ballX + ballRad) = blockX) | ((ballX - ballRad) < (blockX + blockSize)))
                                        {
                                                do tempBlock.erase();
                                                let vert = ~vert;
                                                let numBlocks = numBlocks - 1;
                                        }
                                }
                        }
                        let temp = temp + 1;
                        if (numBlocks = 0)
                        {
                                let lose = true;
                        }
                }
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

dmoeller
This post was updated on .
sorry, did some tweaking and got a new problem now. the block array fully loads now, and the ball interacts with MOST of them correctly. there are a select few that dont dissapear when they are hit, the ball just moves right through them. These blocks are different each time I run the program.

here is the code

//if the ball collides with a block
                let temp = 0;
                while (temp < numBlocks)
                {
                        let tempBlock = blockArray[temp];
                        let blockX = tempBlock.getX();
                        let blockY = tempBlock.getY();
                        let blockSize = tempBlock.getSize();
                        if (tempBlock.show())
                        {
                                if((((ballY + ballRad) - 1) > blockY) & (~(((ballY - ballRad)-1) > (blockY +( blockSize/2)))))
                                {
                                        if(((ballX + ballRad) > blockX) & ((ballX - ballRad) < ((blockX + blockSize) + 1)))
                                        {
                                                do tempBlock.erase();
                                                let vert = ~vert;
                                                let numBlocks = numBlocks - 1;
                                        }
                                        else
                                        {
                                                if(((ballX + ballRad) = blockX) | ((ballX - ballRad) = (blockX + blockSize)))
                                                {
                                                        do tempBlock.erase();
                                                        let hor = ~hor;
                                                        let numBlocks = numBlocks - 1;
                                                }
                                        }
                                }
                        }
                        let temp = temp + 1;
                }
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

cadet1620
Administrator
I'm at work at the moment, so I can't look at it now.  If you email me your complete game it will be easier to figure out what's wrong.  It will likely be sometime later tonight before I can look at it.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

dmoeller
My next problem is when I end the game. When the number of shown blocks reaches 0, i set exit to true, and print "you win" to the screen, and to print "Game Over" if the ball ever hits the floor. The problem is that when it prints you win, the ball keeps moving invisibly, hits the floor and then "Game Over" prints. Another problem is that when"Game Over" prints, the while loop keeps going and I get a stack overflow error. but for BOTH win and loose i set exit to true so the loop should stop. Im not sure which part of my code to include. So I have linked the entire Breakout.jack fileBreakout.jack
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

cadet1620
Administrator
You are stuck in the while (key = 0) loop.  It needs to break when exit is true.
        while (~exit)
        {
            // waits for a key to be pressed.
            while (key = 0)
            {
                let key = Keyboard.keyPressed();
                do moveBall();
            }
            if (key = 81) 
Also, you are printing the "Game Over" message twice because of two checkCollision() calls in moveBall().
Can you change it to call checkCollision() once at the end?
    method void moveBall()
    {
        //veritcle movement
        if(vert)
        {
            do ball.moveDown();
--          do checkCollision();
        }
        else
        {
            do ball.moveUp();
--          do checkCollision();
        }
        //horizontal movement
        if(hor)
        {
            do ball.moveRight();
--          do checkCollision();
        }
        else
        {
            do ball.moveLeft();
--          do checkCollision();
        }
++      do checkCollision();
        do Sys.wait(10);
        return;
    }

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

dmoeller
so i will add an if(exit) statement in the  while (key = 0) loop but to break it do i just put return; or do i put break; or something?
Reply | Threaded
Open this post in threaded view
|

Re: Breakout

cadet1620
Administrator
Jack doesn't support 'break'. You need to add ~exit to the while condition.

--Mark