Computer Science Canada

learning flexible arrays

Author:  shoobyman [ Sat Nov 11, 2006 3:14 pm ]
Post subject:  learning flexible arrays

code:

setscreen ("offscreenonly")
var bulletx, bullety : flexible array 1 .. 0 of int
var bulletshot : flexible array 1 .. 0 of boolean
var shotdelay : int := 0
var x, y : int
var chars : array char of boolean
x := 200
y := 30

loop
    Input.KeyDown (chars)
    View.Update
    delay (5)
    cls
    drawfilloval (x, y, 10, 10, 7)
    if chars (KEY_DOWN_ARROW) then
        y -= 1
    elsif chars (KEY_UP_ARROW) then
        y += 1
    end if
    if chars (KEY_RIGHT_ARROW) then
        x += 1
    elsif chars (KEY_LEFT_ARROW) then
        x -= 1
    end if
    shotdelay += 1
    if shotdelay > 10 then
        shotdelay := 0
    end if
    %%%%%%%%%%%%%%%%%%%%%
    %%BULLET IS CREATED%%
    %%%%%%%%%%%%%%%%%%%%%
    if shotdelay = 0 then
        if chars ('q') then
            new bulletx, upper (bulletx) + 1
            new bullety, upper (bullety) + 1
            new bulletshot, upper (bulletshot) + 1
            bulletx (upper (bulletx)) := x + 5
            bullety (upper (bullety)) := y + 5
            bulletshot (upper (bulletshot)) := true
        end if
    end if
    %%%%%%%%%%%%%%%%%%%%
    %%BULLET IS MOVING%%
    %%%%%%%%%%%%%%%%%%%%
    for i : 1 .. upper (bulletshot)
        if bulletshot (i) = true then
            bullety (i) += 2
            drawline (bulletx (i), bullety (i), bulletx (i), bullety (i) + 10, 7)
        end if
    end for
    %%%%%%%%%%%%%%%%%%%%%
    %%BULLET IS STOPPED%%
    %%%%%%%%%%%%%%%%%%%%%
    for i : 1 .. upper (bulletshot)
        if bulletshot (i) = true and bullety (i) > maxy then
            new bulletshot, upper (bulletshot) - 1
            new bulletx, upper (bulletx) - 1
            new bullety, upper (bullety) - 1
        end if
    end for
end loop

I am learning flexible arrays, and since i am making a space shooter, i thought that i should try making a flexible array for the bullets. I came up with this code, but i am in need of help. It gives me an error because the for loop is not updated with the bounds of the array, so everything gets screwed up. Anyone know how I can fix this?

Author:  shoobyman [ Sat Nov 11, 2006 5:55 pm ]
Post subject: 

nevermind, problem solved

Author:  Clayton [ Sat Nov 11, 2006 9:25 pm ]
Post subject: 

pray do tell what you did to fix the problem, I'm sure you're not the only person with this problem (I ran into it once too).

Author:  shoobyman [ Tue Nov 14, 2006 4:26 pm ]
Post subject: 

code:

setscreen ("offscreenonly")
var bulletx, bullety : flexible array 1 .. 1 of int
var bulletshot : flexible array 1 .. 1 of boolean
var shotdelay, vardelete : int := 0
var x, y : int
var chars : array char of boolean
x := 200
y := 30
bulletx (1) := 0
bullety (1) := 0
bulletshot (1) := false
loop
    Input.KeyDown (chars)
    View.Update
    delay (5)
    cls
    drawfilloval (x, y, 10, 10, 7)
    if chars (KEY_DOWN_ARROW) then
        y -= 1
    elsif chars (KEY_UP_ARROW) then
        y += 1
    end if
    if chars (KEY_RIGHT_ARROW) then
        x += 1
    elsif chars (KEY_LEFT_ARROW) then
        x -= 1
    end if
    shotdelay += 1
    if shotdelay > 10 then
        shotdelay := 0
    end if
    %%%%%%%%%%%%%%%%%%%%%
    %%BULLET IS CREATED%%
    %%%%%%%%%%%%%%%%%%%%%
    if shotdelay = 0 then
        if chars ('q') then
            new bulletx, upper (bulletx) + 1
            new bullety, upper (bullety) + 1
            new bulletshot, upper (bulletshot) + 1
            bulletx (upper (bulletx)) := x
            bullety (upper (bullety)) := y
            bulletshot (upper (bulletshot)) := true
        end if
    end if
    %%%%%%%%%%%%%%%%%%%%
    %%BULLET IS MOVING%%
    %%%%%%%%%%%%%%%%%%%%
    for i : 1 .. upper (bulletshot)
        if bulletshot (i) = true then
            bullety (i) += 5
            drawline (bulletx (i), bullety (i), bulletx (i), bullety (i) + 20, 7)
        end if
    end for
    %%%%%%%%%%%%%%%%%%%%%
    %%BULLET IS STOPPED%%
    %%%%%%%%%%%%%%%%%%%%%
    for i : 1 .. upper (bulletshot)
        if bulletshot (i) = true and bullety (i) > maxy then
            vardelete := i
        end if
    end for
    if vardelete > 0 then
        for i : vardelete + 1 .. upper (bulletshot)
            bulletshot (i - 1) := bulletshot (i)
            bulletx (i - 1) := bulletx (i)
            bullety (i - 1) := bullety (i)
        end for
        vardelete := 0
        new bulletx, upper (bulletx) - 1
        new bullety, upper (bullety) - 1
        new bulletshot, upper (bulletshot) - 1
    end if
end loop


It seems that i had forgotten to rearrange the array, so i was deleting a non-existent part of the array. whatever, it's all good now, although i think people will still have problems with this.


: