Computer Science Canada

Breakout-styled game problems

Author:  ziddy [ Wed Jun 01, 2005 8:29 am ]
Post subject:  Breakout-styled game problems

So I decided to make a Breakout style game and I got most of the things going until this happened:


[EDIT]
Here's the original code


code:

var gx, gy : int %ball
var bchangex, bchangey : int := 5 %ball change
var bspeed : int
var bcolor : int
var bsize : int
var x1, x2, y1, y2 : int %paddle
var boxx, boxy, boxx2, boxy2: int %bricks
var chars : array char of boolean
var speed: int

speed := 5
x1 := 100
x2 := 10
gx := 10
gy := 10
y1 := 10
y2 := 20


setscreen ("graphics:max;max,nobuttonbar,title:D:")

View.Set ("offscreenonly")

locate (23, 30)
put "Before starting the program, make sure your dimensions are at 1024x728."
delay (2000)

colorback (31)
cls



loop
   
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%paddle

    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        x1 := x1 + 10
        x2 := x2 + 10
        if x1 > 1000 then
            x1 := 999
        end if
        if x2 > 900 then
            x2 := 899
        end if
        View.Update

        cls
    end if
    if chars (KEY_LEFT_ARROW) then
        x1 := x1 - 10
        x2 := x2 - 10
        if x1 < 100 then
            x1 := 90
        end if
        if x2 < 1 then
            x2 := 1
        end if
        View.Update

        cls
    end if

    drawfillbox (x1, y1, x2, y2, 78)   

    bcolor := red
    bsize := 7
   
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ball and ball collision
   
    Draw.Oval (gx, gy, bsize, bsize, bcolor)
    View.Update

    if gx <= 0 then
        bchangex := 1
    elsif gy <= 1 then
        bchangey := 1
    end if

    if gx >= maxx then
        bchangex := -1
    elsif gy >= maxy then
        bchangey := -1
    end if

    gx += bchangex
    gy += bchangey

    cls
   
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%paddle collision
   
   
    if gx + bsize > x1 - x2 and gx - bsize < x1 + x2 and gy + bsize > y1 - y2 and gy - bsize < y1 +
            y2 then

        bchangex := bchangex * +1
        bchangey := bchangey * -1

    end if
   
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%bricks and brick collision

    boxx := 100
    boxy := 150
    boxx2 := 300
    boxy2 := 200

    Draw.Box (boxx, boxy, boxx2, boxy2, black)

        if bchangex > 0 then
            if whatdotcolor (gx + bchangex + bsize, gy) = black then
                Draw.Box (boxx, boxy, boxx2, boxy2, white)   
                bchangex := -bchangex 
            end if
        else
            if whatdotcolor (gx + bchangex - bsize, gy) = black then
                Draw.Box (boxx, boxy, boxx2, boxy2, white)
                bchangex := +bchangex
            end if
        end if

        if bchangey > 0 then
            if whatdotcolor (gx, gy + bchangey + bsize) = black then
                Draw.Box (boxx, boxy, boxx2, boxy2, white)   
                bchangey := -bchangey
            end if
        else
            if whatdotcolor (gx, gy + bchangey - bsize) = black then   
                Draw.Box (boxx, boxy, boxx2, boxy2, white)   
                bchangey := +bchangey 
            end if
        end if


That was the code I placed in the program to cover the and existing black rectangle with a white rectangle so the next the ball passes on the space where the rectangle was, it would pass through instead of just bouncing on the rectangle again.

Advanced thank yous for any help I get ._.

Author:  Delos [ Wed Jun 01, 2005 1:01 pm ]
Post subject: 

Let me see if I understand you:
- You draw a black box that acts as your 'bumper'
- Keyboard input (or something else) moves this bumper
- To clear the previously drawn bumper, you draw a white box over it

If this is the case, as I think it is, there's an easier way:
- do this in a loop, your 'main' loop that controls everything
- draw your box
- do other necassary stuff like checking for collision, input etc
- Update the Screen
- pause (delay) for a bit
- clear the screen

This requries you to use setscreen("offscreenonly") and View.Update.
It's a lot easier than dynamically locating and clearing only portions of the screen.
However, that being said, if you really want to clear only portions, you can use View.UpdateArea() but please be aware that if you have other objects on the screen (the bricks to be brocken for example), you'll have to set up sepearte Updates for them as well!

Author:  ziddy [ Wed Jun 01, 2005 1:38 pm ]
Post subject: 

Sorry, I'll update my post with the whole program's code in it, instead of just the part I was having trouble with. And I'm sorry for not making myself clear enough :S

Okay, here's the deal. The black box is actually the brick in which the ball has to hit to score and move onto the next level. The bumper is a blue box(please check updated code).

And yeah I have done most of the things you have said, the only other thing that confuses me is where exactly should I put my View.Updates? I got help with the ones in my code from a friend and neglected to ask him where I should put my next View.Updates

Author:  Neo [ Wed Jun 01, 2005 2:12 pm ]
Post subject: 

For this type of program you really only need 1 view.update, 1 delay and one cls. These usually go at the end of the program because you only need to update the screen after you have drawn everything, clear it, then redraw it again, clear it again and so on...


: