Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Flexible Arrays
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mr. T




PostPosted: Sun Oct 23, 2005 5:10 pm   Post subject: Flexible Arrays

In my jezzball game, I made the parameters of Mouse.Where ( ) as elements of a flexible array. I did this with the hope that every time I would click the screen, the array would add an element to itself, and a new bar would be drawn without deleting the previous. My issue is that the flexible array doesnt seem to be doing anything(previous bar is deleted every time a new bar is drawn). Can anyone see why?
code:

const RADIUS : int := 5
const NUM_BALLS : int := 2
var x, y, dx, dy, clr : array 1 .. NUM_BALLS of int
var barX, barY : flexible array 1 .. 1 of int
var barXStore, barYStore, barXFinished1, barXFinished2, button, left, middle, right : int := 0
var horizontalBar := false
var grow1, grow2, topCounterY, bottomCounterY : int := 0
var score : int := 3
var fontScore : int
fontScore := Font.New ("tahoma:80")
View.Set ("offscreenonly,nobuttonbar,graphics,title:JezzBall           Score: " + intstr (score))

for i : 1 .. NUM_BALLS
    x (i) := Rand.Int (RADIUS, maxx - RADIUS)
    y (i) := Rand.Int (RADIUS, maxy - RADIUS)
    dx (i) := Rand.Int (-5, 5)
    dy (i) := Rand.Int (-5, 5)
    clr (i) := Rand.Int (1, 15)
end for

loop
    %Frame
    Draw.Box (0, 0, maxx, maxy, blue)

    %Outer Boundary Collision
    for i : 1 .. NUM_BALLS
        if x (i) + dx (i) < RADIUS or
                x (i) + dx (i) > maxx - RADIUS then
            dx (i) := -dx (i)
        end if
        if y (i) + dy (i) < RADIUS or
                y (i) + dy (i) > maxy - RADIUS then
            dy (i) := -dy (i)
        end if

        %Incompleted Wall Collision
        if whatdotcolour (x (i) + RADIUS, y (i)) = red or whatdotcolour (x (i), y (i) + RADIUS) = red or whatdotcolour (x (i) - RADIUS, y (i)) = red or whatdotcolour (x (i), y (i) - RADIUS) = red then
            score -= 1 %lose 1 life
            View.Set ("offscreenonly,graphics,title:JezzBall           Score: " + intstr (score))
            grow1 := 0 %reset
            %grow2 := 0 %reset
            horizontalBar := false %new wall can now be created
        elsif whatdotcolour (x (i) + RADIUS, y (i)) = green or whatdotcolour (x (i), y (i) + RADIUS) = green or whatdotcolour (x (i) - RADIUS, y (i)) = green or whatdotcolour (x (i), y (i) - RADIUS)
            = green then
            score -= 1 %lose 1 life
            View.Set ("offscreenonly,graphics,title:JezzBall           Score: " + intstr (score))
            %grow1 := 0 %reset
            grow2 := 0 %reset
            horizontalBar := false %new wall can now be created
        end if

        %Gameover
        if score <= 0 then
            cls
            View.Set ("nooffscreenonly,graphics,title:JezzBall           Score: 0          You Lose!")
            for col : 1 .. 10
                Font.Draw ("Game Over!", 40, 175, fontScore, col)
                delay (100)
            end for
            score := 0 %reset
            return
        end if

        %Completed Wall Collision
        if x (i) + dx (i) >= barXStore and whatdotcolour (x (i) - RADIUS, y (i)) = blue then
            dx (i) *= -1
        end if

        if x (i) + dx (i) <= barXStore and whatdotcolour (x (i) + RADIUS, y (i)) = blue then
            dx (i) *= -1
        end if

        if y (i) + dy (i) >= barYStore and whatdotcolour (x (i), y (i) - RADIUS) = blue then
            dy (i) *= -1
        end if

        if y (i) + dy (i) <= barYStore + 10 and whatdotcolour (x (i), y (i) + RADIUS) = blue then
            dy (i) *= -1
        end if

        x (i) := x (i) + dx (i)
        y (i) := y (i) + dy (i)

        %Draw Ball
        Draw.FillOval (x (i), y (i), RADIUS, RADIUS, black)
    end for

    View.Update
    delay (10)
    cls

    %Wall Creation
    if horizontalBar = false then %Can't click when a wall is being created
        Mouse.ButtonChoose ("multibutton")
        Mouse.Where (barX (upper (barX)), barY (upper (barY)), button)
        left := button mod 10 % left = 0 or 1
        middle := (button - left) mod 100 % middle = 0 or 10
        right := button - middle - left % right = 0 or 100
        grow1 := 0  %reset
        grow2 := 0  %reset
        topCounterY := 0 %reset
        bottomCounterY := 0 %reset

        if button = 1 and left = 1 then %Left button click

            %Adding an element to the flexible array
            barXStore := barX (upper (barX))
            barYStore := barY (upper (barY))

            new barX, upper (barX) + 1
            new barY, upper (barY) + 1

            horizontalBar := true %Wall Creation now possible
        end if
    end if

    if horizontalBar = true then %Wall Creation
        grow1 += 3 %Wall heading right
        grow2 += 3 %Wall heading left
        Draw.FillBox (barXStore, barYStore, barXStore + grow1, barYStore + 10, red)     %Wall heading right
        Draw.FillBox (barXStore, barYStore, barXStore - grow2, barYStore + 10, green)     %Wall heading left
        barXFinished1 := barXStore + grow1 %Saving the coords of finished wall heading right
        barXFinished2 := barXStore - grow2 %Saving the coords of finished wall heading left
    end if

    if barXFinished1 >= maxx then %Finished wall (heading right) at maxx
        Draw.FillBox (barXStore, barYStore, barXFinished1, barYStore + 10, blue) %Change finished wall blue
    end if

    if barXFinished2 <= 0 then %Finished wall (heading left) at 0
        Draw.FillBox (barXStore, barYStore, barXFinished2, barYStore + 10, blue) %%Change finished wall blue
    end if

    if barXFinished1 >= maxx and barXFinished2 <= 0 then %Both walls are finished

        %Box Creation Counter (above, below, or none)
        for i : 1 .. NUM_BALLS
            if y (i) > (barYStore + 10) then %Balls above wall
                topCounterY += 1 %Balls above wall counter
            elsif y (i) < barYStore then %Balls below wall
                bottomCounterY += 1 %Balls below wall counter
            end if
        end for

        %Box Creation Result (above, below, or none)
        if topCounterY not= 0 and bottomCounterY not= 0 then %balls located above AND below wall
            put "Balls are both above and below this bar."
        elsif topCounterY not= 0 then %all balls located above wall
            Draw.FillBox (0, 0, maxx, barYStore, blue) %fill in area beneath balls
            put "All balls are above this bar."
        elsif bottomCounterY not= 0 then      %all balls located below wall
            Draw.FillBox (0, barYStore + 10, maxx, maxy, blue)
            put "All balls are below this bar."
        end if

        horizontalBar := false  %new wall can now be created

    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
jamonathin




PostPosted: Sun Oct 23, 2005 5:49 pm   Post subject: (No subject)

Well pwned. You're in kind of a mess now. You see, you dont want to make the Mouse.Where variables flexible, you want to make the boxes flexible and the boolean variables flexible and whatnot. It should also be in a record now that theres going to be more than one box.

What you should be doing for this is:
Whenever the mouse is clicked, add 1 to the upper of these variables:
Xvalues of box. Yvalues of box.

All the 'horizontalBar' and 'barXFinished1' things that you have scattered throughout your program. And even the variables that change the size of the box (make it grow till the edge).

All of this should be done in a for loop where:
code:

for i: 1 .. upper(boxes)
%drawbox(boxes(i).x,boxes(i).y,width,height,color)
do all the checking, where everything is at boxes(i).WHATEVER


Since your program isn't too huge. I suggest starting over. Copyb all of the techniques that you used, but put them into flexible records.

Repitition is Good.
Mr. T




PostPosted: Sun Oct 23, 2005 7:07 pm   Post subject: Alex's Opinion

Should i stick a for loop around the entire wall creating portion of the code? Or should i make individual for loops around each of the if statements? [ for i: 1..upper(wallArray) ]
jamonathin




PostPosted: Sun Oct 23, 2005 7:39 pm   Post subject: (No subject)

If your going to stick with the same layout as you did for the single wall, then you should have all this in a for loop of ( i : 1 .. upper(wall) )
code:

 %Wall Creation
    if horizontalBar = false then %Can't click when a wall is being created
        Mouse.ButtonChoose ("multibutton")
        Mouse.Where (barX (upper (barX)), barY (upper (barY)), button)
        left := button mod 10 % left = 0 or 1
        middle := (button - left) mod 100 % middle = 0 or 10
        right := button - middle - left % right = 0 or 100
        grow1 := 0  %reset
        grow2 := 0  %reset
        topCounterY := 0 %reset
        bottomCounterY := 0 %reset

        if button = 1 and left = 1 then %Left button click

            %Adding an element to the flexible array
            barXStore := barX (upper (barX))
            barYStore := barY (upper (barY))

            new barX, upper (barX) + 1
            new barY, upper (barY) + 1

            horizontalBar := true %Wall Creation now possible
        end if
    end if

    if horizontalBar = true then %Wall Creation
        grow1 += 3 %Wall heading right
        grow2 += 3 %Wall heading left
        Draw.FillBox (barXStore, barYStore, barXStore + grow1, barYStore + 10, red)     %Wall heading right
        Draw.FillBox (barXStore, barYStore, barXStore - grow2, barYStore + 10, green)     %Wall heading left
        barXFinished1 := barXStore + grow1 %Saving the coords of finished wall heading right
        barXFinished2 := barXStore - grow2 %Saving the coords of finished wall heading left
    end if

    if barXFinished1 >= maxx then %Finished wall (heading right) at maxx
        Draw.FillBox (barXStore, barYStore, barXFinished1, barYStore + 10, blue) %Change finished wall blue
    end if

    if barXFinished2 <= 0 then %Finished wall (heading left) at 0
        Draw.FillBox (barXStore, barYStore, barXFinished2, barYStore + 10, blue) %%Change finished wall blue
    end if

    if barXFinished1 >= maxx and barXFinished2 <= 0 then %Both walls are finished

        %Box Creation Counter (above, below, or none)
        for i : 1 .. NUM_BALLS
            if y (i) > (barYStore + 10) then %Balls above wall
                topCounterY += 1 %Balls above wall counter
            elsif y (i) < barYStore then %Balls below wall
                bottomCounterY += 1 %Balls below wall counter
            end if
        end for

        %Box Creation Result (above, below, or none)
        if topCounterY not= 0 and bottomCounterY not= 0 then %balls located above AND below wall
            put "Balls are both above and below this bar."
        elsif topCounterY not= 0 then %all balls located above wall
            Draw.FillBox (0, 0, maxx, barYStore, blue) %fill in area beneath balls
            put "All balls are above this bar."
        elsif bottomCounterY not= 0 then      %all balls located below wall
            Draw.FillBox (0, barYStore + 10, maxx, maxy, blue)
            put "All balls are below this bar."
        end if

        horizontalBar := false  %new wall can now be created

    end if


Now here's how you put this into a flexible record
code:

var wall : flexible array 1 .. 0 of
    record
        horizontalBar : boolean
        barXStore, barYStore, barXFinished1, barXFinished2, button, left, middle, right : int
        grow1, grow2, topCounterY, bottomCounterY : int
    end record

Then in that whole if statement, you would change all of your variable names to (ex. using 'barXstore') ' wall(i).barXstore := whatever '

I'll gladly help you along the way, but i wont do it for you Razz. You'll learn much more trying it out yourself. Just ask when ur stuck (i'll usually respond Wink)
Mr. T




PostPosted: Sun Oct 23, 2005 9:05 pm   Post subject: Alex's Opinion

Alright here's the updated flexible array code... but i get a "variable has no value" error for wallArray (a).barXFinished1 and wallArray (a).barXFinished2 when I try to assign them a value (possibly because their values get assigned inside an if statement). Anyways, here's the code.
code:

const RADIUS : int := 5
const NUM_BALLS : int := 2
var x, y, dx, dy, clr : array 1 .. NUM_BALLS of int
var barX, barY, button, left, middle, right : int := 0
var horizontalBar, wallCreated := false
var grow1, grow2, topCounterY, bottomCounterY : int := 0
var score : int := 3
var fontScore : int
fontScore := Font.New ("tahoma:80")
View.Set ("offscreenonly,nobuttonbar,graphics,title:JezzBall           Score: " + intstr (score))

%Ball initialization
for i : 1 .. NUM_BALLS
    x (i) := Rand.Int (RADIUS, maxx - RADIUS)
    y (i) := Rand.Int (RADIUS, maxy - RADIUS)
    dx (i) := Rand.Int (-5, 5)
    dy (i) := Rand.Int (-5, 5)
    clr (i) := Rand.Int (1, 15)
end for

%Wall initialization
type wallData :
    record
        barXStore : int
        barYStore : int
        barXFinished1 : int
        barXFinished2 : int
    end record

var wallArray : flexible array 1 .. 1 of wallData

loop
    %Frame
    Draw.Box (0, 0, maxx, maxy, blue)

    %Outer Boundary Collision
    for i : 1 .. NUM_BALLS
        if x (i) + dx (i) < RADIUS or
                x (i) + dx (i) > maxx - RADIUS then
            dx (i) := -dx (i)
        end if
        if y (i) + dy (i) < RADIUS or
                y (i) + dy (i) > maxy - RADIUS then
            dy (i) := -dy (i)
        end if

        %Incompleted Wall Collision
        if whatdotcolour (x (i) + RADIUS, y (i)) = red or whatdotcolour (x (i), y (i) + RADIUS) = red or whatdotcolour (x (i) - RADIUS, y (i)) = red or whatdotcolour (x (i), y (i) - RADIUS) = red then
            score -= 1 %lose 1 life
            View.Set ("offscreenonly,graphics,title:JezzBall           Score: " + intstr (score))
            grow1 := 0 %reset
            %grow2 := 0 %reset
            horizontalBar := false %new wall can now be created
        elsif whatdotcolour (x (i) + RADIUS, y (i)) = green or whatdotcolour (x (i), y (i) + RADIUS) = green or whatdotcolour (x (i) - RADIUS, y (i)) = green or whatdotcolour (x (i), y (i) - RADIUS)
                = green then
            score -= 1 %lose 1 life
            View.Set ("offscreenonly,graphics,title:JezzBall           Score: " + intstr (score))
            %grow1 := 0 %reset
            grow2 := 0 %reset
            horizontalBar := false %new wall can now be created
        end if

        %Gameover
        if score <= 0 then
            cls
            View.Set ("nooffscreenonly,graphics,title:JezzBall           Score: 0          You Lose!")
            for col : 1 .. 10
                Font.Draw ("Game Over!", 40, 175, fontScore, col)
                delay (100)
            end for
            score := 0 %reset
            return
        end if

        %Completed Wall Collision
        if wallCreated = true then %collision can only occur after 1 wall has been created
            for a : 1 .. upper (wallArray)
                if x (i) + dx (i) >= wallArray (a).barXStore and whatdotcolour (x (i) - RADIUS, y (i)) = blue then
                    dx (i) *= -1
                end if

                if x (i) + dx (i) <= wallArray (a).barXStore and whatdotcolour (x (i) + RADIUS, y (i)) = blue then
                    dx (i) *= -1
                end if

                if y (i) + dy (i) >= wallArray (a).barYStore and whatdotcolour (x (i), y (i) - RADIUS) = blue then
                    dy (i) *= -1
                end if

                if y (i) + dy (i) <= wallArray (a).barXStore + 10 and whatdotcolour (x (i), y (i) + RADIUS) = blue then
                    dy (i) *= -1
                end if
            end for
        end if

        x (i) := x (i) + dx (i)
        y (i) := y (i) + dy (i)

        %Draw Ball
        Draw.FillOval (x (i), y (i), RADIUS, RADIUS, black)
    end for

    View.Update
    delay (10)
    cls

    %Wall Creation
    for a : 1 .. upper (wallArray)

        if horizontalBar = false then %Can't click when a wall is being created
            Mouse.ButtonChoose ("multibutton")
            Mouse.Where (barX, barY, button)
            left := button mod 10 % left = 0 or 1
            middle := (button - left) mod 100 % middle = 0 or 10
            right := button - middle - left % right = 0 or 100
            grow1 := 0 %reset
            grow2 := 0 %reset
            topCounterY := 0 %reset
            bottomCounterY := 0 %reset

            if button = 1 and left = 1 then %Left button click

                %Adding an element to the flexible array
                wallArray (a).barXStore := barX
                wallArray (a).barYStore := barY

                new wallArray, upper (wallArray) + 1
                new wallArray, upper (wallArray) + 1

                horizontalBar := true %Wall Creation now possible
            end if
        end if

        if horizontalBar = true then %Wall Creation
            grow1 += 3 %Wall heading right
            grow2 += 3 %Wall heading left
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXStore + grow1, wallArray (a).barYStore + 10, red) %Wall heading right
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXStore - grow2, wallArray (a).barYStore + 10, green) %Wall heading left
            wallArray (a).barXFinished1 := wallArray (a).barXStore + grow1 %Saving the coords of finished wall heading right
            wallArray (a).barXFinished2 := wallArray (a).barXStore - grow2 %Saving the coords of finished wall heading left
        end if

        if wallArray (a).barXFinished1 >= maxx then %Finished wall (heading right) at maxx
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXFinished1, wallArray (a).barYStore + 10, blue) %Change finished wall blue
        end if

        if wallArray (a).barXFinished2 <= 0 then %Finished wall (heading left) at 0
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXFinished2, wallArray (a).barYStore + 10, blue) %%Change finished wall blue
        end if

        if wallArray (a).barXFinished1 >= maxx and wallArray (a).barXFinished2 <= 0 then %Both walls are finished

            %Box Creation Counter (above, below, or none)
            for i : 1 .. NUM_BALLS
                if y (i) > (wallArray (a).barYStore + 10) then %Balls above wall
                    topCounterY += 1 %Balls above wall counter
                elsif y (i) < wallArray (a).barYStore then %Balls below wall
                    bottomCounterY += 1 %Balls below wall counter
                end if
            end for

            %Box Creation Result (above, below, or none)
            if topCounterY not= 0 and bottomCounterY not= 0 then %balls located above AND below wall
                put "Balls are both above and below this bar."
            elsif topCounterY not= 0 then %all balls located above wall
                Draw.FillBox (0, 0, maxx, wallArray (a).barYStore, blue) %fill in area beneath balls
                put "All balls are above this bar."
            elsif bottomCounterY not= 0 then  %all balls located below wall
                Draw.FillBox (0, wallArray (a).barYStore + 10, maxx, maxy, blue)
                put "All balls are below this bar."
            end if

            horizontalBar := false %new wall can now be created

        end if

    end for

end loop
jamonathin




PostPosted: Mon Oct 24, 2005 6:00 am   Post subject: (No subject)

What you have to do, is right after the mouse is clicked, before the program can get to any other 'if' statements, you have to assign values to the 'Store' variables and whatnot. And forthose 'Finished' variables, just set them to a default value before you enter the loop.

So right after this statement:
code:
Mouse.ButtonChoose ("multibutton")
            Mouse.Where (barX, barY, button)

set values for the boxes.
Mr. T




PostPosted: Mon Oct 24, 2005 8:50 am   Post subject: Alex's Opinion

I thought I had already assigned them a value:
code:

wallArray (a).barXStore := barX
wallArray (a).barYStore := barY

Confused
jamonathin




PostPosted: Mon Oct 24, 2005 10:44 am   Post subject: (No subject)

Yeah, for some reason i though left was right, dont ask, lol. . .
But what the problem then is here:

code:

%Adding an element to the flexible array
                wallArray (a).barXStore := barX
                wallArray (a).barYStore := barY

                new wallArray, upper (wallArray) + 1
                new wallArray, upper (wallArray) + 1

and the fact that your for loop goes from
code:
for a : 1 .. upper (wallArray)

Well you just added one to wallArray, so now there's two of em. And there's no value for the second (new) wallArray's, so when the for loop goes through the second time, its like wtf mayte, wheres the value?

So what i suggest doing is change you var statement to 1 .. 0 of, like this
code:

var wallArray : flexible array 1 .. 0 of wallData


and changing the new element to look like this, rather than was it was before.
code:

%Adding an element to the flexible array
                new wallArray, upper (wallArray) + 1
                new wallArray, upper (wallArray) + 1

                wallArray (a).barXStore := barX
                wallArray (a).barYStore := barY

Now, when you add that new element, thats where you may have to set default values for the other variables.
And the for loop wont even run if it is 'a: 1 .. 0'

try that out.
Sponsor
Sponsor
Sponsor
sponsor
Mr. T




PostPosted: Mon Oct 24, 2005 2:05 pm   Post subject: Alex's Opinion

Alright, the program runs now without getting a "var has no value error." But now when you click nothing happens; the bar doesn't draw (no Turing error, though). It seems like solving one problem only creates another. Crying or Very sad Here's the updated code:
code:

const RADIUS : int := 5
const NUM_BALLS : int := 2
var x, y, dx, dy, clr : array 1 .. NUM_BALLS of int
var barX, barY, button, left, middle, right : int := 0
var horizontalBar, wallCreated := false
var grow1, grow2, topCounterY, bottomCounterY : int := 0
var score : int := 3
var fontScore : int
fontScore := Font.New ("tahoma:80")
View.Set ("offscreenonly,nobuttonbar,graphics,title:JezzBall           Score: " + intstr (score))

%Ball initialization
for i : 1 .. NUM_BALLS
    x (i) := Rand.Int (RADIUS, maxx - RADIUS)
    y (i) := Rand.Int (RADIUS, maxy - RADIUS)
    dx (i) := Rand.Int (-5, 5)
    dy (i) := Rand.Int (-5, 5)
    clr (i) := Rand.Int (1, 15)
end for

%Wall initialization
type wallData :
    record
        barXStore : int
        barYStore : int
        barXFinished1 : int
        barXFinished2 : int
    end record

var wallArray : flexible array 1 .. 0 of wallData



loop
    %Frame
    Draw.Box (0, 0, maxx, maxy, blue)

    %Outer Boundary Collision
    for i : 1 .. NUM_BALLS
        if x (i) + dx (i) < RADIUS or
                x (i) + dx (i) > maxx - RADIUS then
            dx (i) := -dx (i)
        end if
        if y (i) + dy (i) < RADIUS or
                y (i) + dy (i) > maxy - RADIUS then
            dy (i) := -dy (i)
        end if

        %Incompleted Wall Collision
        if whatdotcolour (x (i) + RADIUS, y (i)) = red or whatdotcolour (x (i), y (i) + RADIUS) = red or whatdotcolour (x (i) - RADIUS, y (i)) = red or whatdotcolour (x (i), y (i) - RADIUS) = red then
            score -= 1 %lose 1 life
            View.Set ("offscreenonly,graphics,title:JezzBall           Score: " + intstr (score))
            grow1 := 0 %reset
            %grow2 := 0 %reset
            horizontalBar := false %new wall can now be created
        elsif whatdotcolour (x (i) + RADIUS, y (i)) = green or whatdotcolour (x (i), y (i) + RADIUS) = green or whatdotcolour (x (i) - RADIUS, y (i)) = green or whatdotcolour (x (i), y (i) - RADIUS)
                = green then
            score -= 1 %lose 1 life
            View.Set ("offscreenonly,graphics,title:JezzBall           Score: " + intstr (score))
            %grow1 := 0 %reset
            grow2 := 0 %reset
            horizontalBar := false %new wall can now be created
        end if

        %Gameover
        if score <= 0 then
            cls
            View.Set ("nooffscreenonly,graphics,title:JezzBall           Score: 0          You Lose!")
            for col : 1 .. 10
                Font.Draw ("Game Over!", 40, 175, fontScore, col)
                delay (100)
            end for
            score := 0 %reset
            return
        end if

        %Completed Wall Collision
        if wallCreated = true then %collision can only occur after 1 wall has been created
            for a : 1 .. upper (wallArray)
                if x (i) + dx (i) >= wallArray (a).barXStore and whatdotcolour (x (i) - RADIUS, y (i)) = blue then
                    dx (i) *= -1
                end if

                if x (i) + dx (i) <= wallArray (a).barXStore and whatdotcolour (x (i) + RADIUS, y (i)) = blue then
                    dx (i) *= -1
                end if

                if y (i) + dy (i) >= wallArray (a).barYStore and whatdotcolour (x (i), y (i) - RADIUS) = blue then
                    dy (i) *= -1
                end if

                if y (i) + dy (i) <= wallArray (a).barXStore + 10 and whatdotcolour (x (i), y (i) + RADIUS) = blue then
                    dy (i) *= -1
                end if
            end for
        end if

        x (i) := x (i) + dx (i)
        y (i) := y (i) + dy (i)

        %Draw Ball
        Draw.FillOval (x (i), y (i), RADIUS, RADIUS, black)
    end for

    View.Update
    delay (10)
    cls

    %Wall Creation
    for a : 1 .. upper (wallArray)

        if horizontalBar = false then %Can't click when a wall is being created
            Mouse.ButtonChoose ("multibutton")
            Mouse.Where (barX, barY, button)
            left := button mod 10 % left = 0 or 1
            middle := (button - left) mod 100 % middle = 0 or 10
            right := button - middle - left % right = 0 or 100
            grow1 := 0 %reset
            grow2 := 0 %reset
            topCounterY := 0 %reset
            bottomCounterY := 0 %reset

            if button = 1 and left = 1 then %Left button click

                %Adding an element to the flexible array
                new wallArray, upper (wallArray) + 1
                new wallArray, upper (wallArray) + 1
               
                wallArray (a).barXStore := barX
                wallArray (a).barYStore := barY
               
                horizontalBar := true %Wall Creation now possible
            end if
        end if

        if horizontalBar = true then %Wall Creation
            grow1 += 3 %Wall heading right
            grow2 += 3 %Wall heading left
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXStore + grow1, wallArray (a).barYStore + 10, red) %Wall heading right
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXStore - grow2, wallArray (a).barYStore + 10, green) %Wall heading left
            wallArray (a).barXFinished1 := wallArray (a).barXStore + grow1 %Saving the coords of finished wall heading right
            wallArray (a).barXFinished2 := wallArray (a).barXStore - grow2 %Saving the coords of finished wall heading left
        end if

        if wallArray (a).barXFinished1 >= maxx then %Finished wall (heading right) at maxx
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXFinished1, wallArray (a).barYStore + 10, blue) %Change finished wall blue
        end if

        if wallArray (a).barXFinished2 <= 0 then %Finished wall (heading left) at 0
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXFinished2, wallArray (a).barYStore + 10, blue) %%Change finished wall blue
        end if

        if wallArray (a).barXFinished1 >= maxx and wallArray (a).barXFinished2 <= 0 then %Both walls are finished

            %Box Creation Counter (above, below, or none)
            for i : 1 .. NUM_BALLS
                if y (i) > (wallArray (a).barYStore + 10) then %Balls above wall
                    topCounterY += 1 %Balls above wall counter
                elsif y (i) < wallArray (a).barYStore then %Balls below wall
                    bottomCounterY += 1 %Balls below wall counter
                end if
            end for

            %Box Creation Result (above, below, or none)
            if topCounterY not= 0 and bottomCounterY not= 0 then %balls located above AND below wall
                put "Balls are both above and below this bar."
            elsif topCounterY not= 0 then %all balls located above wall
                Draw.FillBox (0, 0, maxx, wallArray (a).barYStore, blue) %fill in area beneath balls
                put "All balls are above this bar."
            elsif bottomCounterY not= 0 then  %all balls located below wall
                Draw.FillBox (0, wallArray (a).barYStore + 10, maxx, maxy, blue)
                put "All balls are below this bar."
            end if

            horizontalBar := false %new wall can now be created

        end if

    end for

end loop
jamonathin




PostPosted: Tue Oct 25, 2005 11:56 am   Post subject: (No subject)

Ok now the bars are drawn. What happened was, you were checking in the for loop that ran from ' a : 1 .. upper(wallArray) ' (which is 1 .. 0 default) for when the mouse was clicked. Well, you can never get to that mouse command if teh for loop never runs right? So nothing insdie that for loop could be accessed. I moves that whole 'if' statement outside the for loop.

Before i looked for the problem, i could see that you would already run itno future problems. For instance, grow1 and grow2. You never put them into that flexible array, so when ever you click the mouse, grow1 and grow2 restart. And whenever you draw the other walls, you have them draw with grow1 and grow2. But they restart, so that wall never stays there. But the x and y values do because they're in teh flexible array. So i fixed that up.

Also, Horizontalbar needs to be in that record as well. So that we know which horizontal bar we're talking about. We dont want 1 variable to represent them all. So that was thrown in as well.

I didn't finish everything, because well, thats your job. What you can notice is that they new variable waitBar (kinda like horizontalBar) restricts a new bar from being drawn (1 at a time). But 'waitBar' only goes back to '0' once BOTH bars are drawn. It never will if only one gets hit. Thats where you can reset 'waitBar' in your wall collision thing.

Another thing you'll notice is that there's no more collision on the walls. :O! So check that out Smile. Also, change the text that you draw. Kinda messes things up. (maybe draw on another output screen?)

So try out those things i said. Good - Luck.


code:

const RADIUS : int := 5
const NUM_BALLS : int := 2
var x, y, dx, dy, clr : array 1 .. NUM_BALLS of int
var barX, barY, button, left, middle, right : int := 0
%var horizontalBar, wallCreated := false
var waitBar : boolean := false %waits so only 1 wall at a time
var topCounterY, bottomCounterY : int := 0
var score : int := 3
var fontScore : int
fontScore := Font.New ("tahoma:80")
View.Set ("offscreenonly,nobuttonbar,graphics,title:JezzBall           Score: " + intstr (score))

%Ball initialization
for i : 1 .. NUM_BALLS
    x (i) := Rand.Int (RADIUS, maxx - RADIUS)
    y (i) := Rand.Int (RADIUS, maxy - RADIUS)
    dx (i) := Rand.Int (-5, 5)
    dy (i) := Rand.Int (-5, 5)
    clr (i) := Rand.Int (1, 15)
end for

%Wall initialization
type wallData :
    record
        barXStore : int
        barYStore : int
        barXFinished1 : int
        barXFinished2 : int
        grow1, grow2 : int
        horizontalBar, wallCreated : boolean

    end record

var wallArray : flexible array 1 .. 0 of wallData



loop
    %Frame
    Draw.Box (0, 0, maxx, maxy, blue)

    %Outer Boundary Collision
    for i : 1 .. NUM_BALLS
        if x (i) + dx (i) < RADIUS or
                x (i) + dx (i) > maxx - RADIUS then
            dx (i) := -dx (i)
        end if
        if y (i) + dy (i) < RADIUS or
                y (i) + dy (i) > maxy - RADIUS then
            dy (i) := -dy (i)
        end if

        %Incompleted Wall Collision
        for a : 1 .. upper (wallArray)
            if whatdotcolour (x (i) + RADIUS, y (i)) = red or whatdotcolour (x (i), y (i) + RADIUS) = red or whatdotcolour (x (i) - RADIUS, y (i)) = red or whatdotcolour (x (i), y (i) - RADIUS) = red
                    then
                score -= 1 %lose 1 life
                View.Set ("offscreenonly,graphics,title:JezzBall           Score: " + intstr (score))
                wallArray (a).grow1 := 0 %reset
                %grow2 := 0 %reset
                wallArray (a).horizontalBar := false %new wall can now be created
            elsif whatdotcolour (x (i) + RADIUS, y (i)) = green or whatdotcolour (x (i), y (i) + RADIUS) = green or whatdotcolour (x (i) - RADIUS, y (i)) = green or whatdotcolour (x (i), y (i) -
                    RADIUS)
                    = green then
                score -= 1 %lose 1 life
                View.Set ("offscreenonly,graphics,title:JezzBall           Score: " + intstr (score))
                %grow1 := 0 %reset
                wallArray (a).grow2 := 0 %reset
                wallArray (a).horizontalBar := false %new wall can now be created
            end if
        end for
        %Gameover
        if score <= 0 then
            cls
            View.Set ("nooffscreenonly,graphics,title:JezzBall           Score: 0          You Lose!")
            for col : 1 .. 10
                Font.Draw ("Game Over!", 40, 175, fontScore, col)
                delay (100)
            end for
            score := 0 %reset
            return
        end if

        %Completed Wall Collision
        for a : 1 .. upper (wallArray)
            if wallArray (a).wallCreated = true then %collision can only occur after 1 wall has been created
                if x (i) + dx (i) >= wallArray (a).barXStore and whatdotcolour (x (i) - RADIUS, y (i)) = blue then
                    dx (i) *= -1
                end if

                if x (i) + dx (i) <= wallArray (a).barXStore and whatdotcolour (x (i) + RADIUS, y (i)) = blue then
                    dx (i) *= -1
                end if

                if y (i) + dy (i) >= wallArray (a).barYStore and whatdotcolour (x (i), y (i) - RADIUS) = blue then
                    dy (i) *= -1
                end if

                if y (i) + dy (i) <= wallArray (a).barXStore + 10 and whatdotcolour (x (i), y (i) + RADIUS) = blue then
                    dy (i) *= -1
                end if
            end if
        end for
        x (i) := x (i) + dx (i)
        y (i) := y (i) + dy (i)

        %Draw Ball
        Draw.FillOval (x (i), y (i), RADIUS, RADIUS, black)
    end for

    View.Update
    delay (10)
    cls
    if waitBar = false then         %Can't click when a wall is being created
        Mouse.ButtonChoose ("multibutton")
        Mouse.Where (barX, barY, button)
        left := button mod 10     % left = 0 or 1
        middle := (button - left) mod 100     % middle = 0 or 10
        right := button - middle - left     % right = 0 or 100
        topCounterY := 0            %reset
        bottomCounterY := 0     %reset
        if button = 1 and left = 1 then     %Left button click
            %Adding an element to the flexible array %%% DEFAULT VALUES!!!!!!
            new wallArray, upper (wallArray) + 1 %only need this once

            wallArray (upper (wallArray)).barXStore := barX % using upper(wallArray)
            wallArray (upper (wallArray)).barYStore := barY % because we want the
            wallArray (upper (wallArray)).grow1 := 0        % newest one.
            wallArray (upper (wallArray)).grow2 := 0

            wallArray (upper (wallArray)).horizontalBar := true
            wallArray (upper (wallArray)).wallCreated := false

            wallArray (upper (wallArray)).barXFinished1 := barX
            wallArray (upper (wallArray)).barXFinished2 := barX

            waitBar := true %just so we dont need another for loop
            %horizontalBar := true     %Wall Creation now possible
        end if
    end if
    %Wall Creation
    for a : 1 .. upper (wallArray)

        if wallArray (a).horizontalBar = true then %Wall Creation
            wallArray (a).grow1 += 3 %Wall heading right
            wallArray (a).grow2 += 3 %Wall heading left
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXStore + wallArray (upper (wallArray)).grow1, wallArray (a).barYStore + 10, red)
            %Wall heading right
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXStore - wallArray (upper (wallArray)).grow2, wallArray (a).barYStore + 10, green) %Wall heading left
            wallArray (a).barXFinished1 := wallArray (a).barXStore + wallArray (a).grow1 %Saving the coords of finished wall heading right
            wallArray (a).barXFinished2 := wallArray (a).barXStore - wallArray (a).grow2 %Saving the coords of finished wall heading left
        end if

        if wallArray (a).barXFinished1 >= maxx then %Finished wall (heading right) at maxx
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXFinished1, wallArray (a).barYStore + 10, blue) %Change finished wall blue
        end if

        if wallArray (a).barXFinished2 <= 0 then %Finished wall (heading left) at 0
            Draw.FillBox (wallArray (a).barXStore, wallArray (a).barYStore, wallArray (a).barXFinished2, wallArray (a).barYStore + 10, blue) %%Change finished wall blue
        end if

        if wallArray (a).barXFinished1 >= maxx and wallArray (a).barXFinished2 <= 0 then %Both walls are finished

            %Box Creation Counter (above, below, or none)
            for i : 1 .. NUM_BALLS
                if y (i) > (wallArray (a).barYStore + 10) then %Balls above wall
                    topCounterY += 1 %Balls above wall counter
                elsif y (i) < wallArray (a).barYStore then %Balls below wall
                    bottomCounterY += 1 %Balls below wall counter
                end if
            end for

            %Box Creation Result (above, below, or none)
            if topCounterY not= 0 and bottomCounterY not= 0 then %balls located above AND below wall
                put "Balls are both above and below this bar."
            elsif topCounterY not= 0 then %all balls located above wall
                Draw.FillBox (0, 0, maxx, wallArray (a).barYStore, blue) %fill in area beneath balls
                put "All balls are above this bar."
            elsif bottomCounterY not= 0 then  %all balls located below wall
                Draw.FillBox (0, wallArray (a).barYStore + 10, maxx, maxy, blue)
                put "All balls are below this bar."
            end if

            waitBar := false %new wall can now be created

        end if

    end for

end loop

Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: