
-----------------------------------
Aniota
Sun Jun 01, 2008 2:50 pm

Help with Arrays
-----------------------------------
So this is my code so far,
View.Set ("graphics:800;600")
var asteroidy,asteroidx1,asteroidx2, asteroidx3,asteroidx4, size1, size2 : int
var chars : array char of boolean 

asteroidy := 575

asteroidx1 := Rand.Int (10, 800)
asteroidx2 := Rand.Int (10, 800)
asteroidx3 :=Rand.Int (10, 800)
asteroidx4 :=Rand.Int (10, 800)
size1 := Rand.Int (10,20)
size2 := Rand.Int (10, 20)


loop
cls
put asteroidy

drawoval (asteroidx1, asteroidy, size1, size1, red)
Draw.FillOval ( asteroidx1, asteroidy, size1, size1, red)

drawoval (asteroidx2, asteroidy, size1, size1, red)
Draw.FillOval ( asteroidx2, asteroidy, size1, size1, red)
drawoval (asteroidx3, asteroidy, size2, size2, red)
Draw.FillOval ( asteroidx3, asteroidy, size2, size2, red)
drawoval (asteroidx4, asteroidy, size2, size1, red)
Draw.FillOval ( asteroidx4, asteroidy, size2, size1, red)
delay (20)
asteroidy -=5

Input.KeyDown (chars) 
exit when chars (KEY_UP_ARROW)
end loop

I have read all the array tutorials and i still dont seem to get it =/ To make my code look cleaner you put things in arrays but im not sure how to do this. Does anyone know how to alter this code and put it into an array and still work
thanks

-----------------------------------
Stove
Sun Jun 01, 2008 5:00 pm

Re: Help with Arrays
-----------------------------------
You could make an array for each asteroid variable, like:


var asteroidX : array 0 .. 3 of int
var asteroidY : array 0 .. 3 of int
var asteroidColor : array 0 .. 3 of int
var asteroidSize : array 0 .. 3 of int


Although doing it that way is still pretty dirty. So instead of declaring an array for each feature, declare a record containing all the features your asteroids will have, then make a single array of records which you can easily index through. It'd look something like this:


type Asteroid :
    record
        X : int
        Y : int
        Color : int
        Size : int
    end record

var numAsteriods : int := 4
var Asteroids : array 1 .. numAsteriods of Asteroid

for i : 1 .. numAsteriods
    Asteroids (i).X := Rand.Int (10, 800)
    Asteroids (i).Color := red
    Asteroids (i).Size := Rand.Int (10, 20)
end for


Then when you go to draw them all, it becomes a much cleaner:


    for i : 1 .. numAsteriods
        Draw.FillOval (Asteroids (i).X, asteroidy, Asteroids (i).Size, Asteroids (i).Size, Asteroids (i).Color)
    end for


You could use a class instead of a record, but it doesn't look like you'd need to. Anyways, hope that helps.

-----------------------------------
TheWanderer
Sun Jun 01, 2008 5:06 pm

Re: Help with Arrays
-----------------------------------
Firstly, what are you trying to write this program to do? I.e. the purpose

it looks like it draws a line of ovals across the screen and has them descend simultaneously?

right now your code looks fine so I'll assume it executes how you like it.

to make this run from an array setup, the way I think you're trying to get at:

var numasteroids := 4
var asteroidx : array 1..numasteroids of int

for i : 1..numasteroids
asteroidx(i) := Rand.Int (10,800)
end for

and then continue your program. when you need the x coordinates of your individual asteroids, call up asteroidx (number)

good luck!

Edit : lol Stove beat me to it by half a minute XD

Edit : btw, it looks like you already have a thread asking about this project. I'm not fully aware of compsci rules (like by heart) but that's a little poor etiquette. try not to create extra threads, ok? ^^

-----------------------------------
Aniota
Sun Jun 01, 2008 6:25 pm

Re: Help with Arrays
-----------------------------------
Oh sorry, i thought if i had a new question i create a new topic >_< Sorry. Is there a way for me to delete it?

Well i have another question, im using collision detection so that if my lazer hits the circles the circle disappears.  so i know how to tell if it hits but after that im not sure how to then make the circle disappear before it comes into collision with the spaceship.


Thanks for replying to my first question =)

I tried using arrays for asteroids but i had some problems so i just used the old code. The collision detection doesnt work any suggestions?
View.Set ("graphics:800;600")
%Variables
var x, hitleftwall, hitrightwall, firedelay, firedelaytime, totalfired : int := 0
var asteroidx1, asteroidx2, asteroidx3, asteroidx4, asteroidx5, asteroidy : int
var lazerx, lazery, fired : array 1 .. 10 of int
var distance_between_centres1 : real
var distance_between_centres2 : real
var distance_between_centres3 : real
var distance_between_centres4 : real
var distance_between_centres5 : real
var hit : int
var lazerhit1 : real
var lazerhit2 : real
var lazerhit3 : real
var lazerhit4 : real
var lazerhit5 : real

var chars : array char of boolean



asteroidy := 575
x := 400
asteroidx1 := Rand.Int (10, 755)
asteroidx2 := Rand.Int (150, 755)
asteroidx3 :=Rand.Int (500, 755)
asteroidx4 :=Rand.Int (20, 755)
asteroidx5 :=Rand.Int (20,700)
colorback (black)
hit := 0

cls

for count : 1 .. 10
    fired (count) := 0
    
end for
loop
    Input.KeyDown (chars)
    %Handles shooting the lazer
    if chars (KEY_UP_ARROW) then
        if firedelay = 0 then
            if totalfired 