Help with Arrays
Author |
Message |
Aniota
|
Posted: Sun Jun 01, 2008 2:50 pm Post subject: 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 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Stove
|
Posted: Sun Jun 01, 2008 5:00 pm Post subject: Re: Help with Arrays |
|
|
You could make an array for each asteroid variable, like:
Turing: |
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:
Turing: |
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:
Turing: |
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
|
Posted: Sun Jun 01, 2008 5:06 pm Post subject: 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:
Turing: |
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
|
Posted: Sun Jun 01, 2008 6:25 pm Post subject: 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 <= 9 then
totalfired += 1
fired (totalfired) := 1
firedelay := 1
lazerx (totalfired) := x
lazery (totalfired) := 35
end if
end if
end if
%Moves tank to the right
if chars (KEY_RIGHT_ARROW) then
if hitrightwall = 0 then
x := x + 5
end if
end if
%Moves tank to the left
if chars (KEY_LEFT_ARROW) then
if hitleftwall = 0 then
x := x - 5
end if
end if
%Collision detection with the let and right walls.
if x <= 25 then
hitleftwall := 1
elsif x >= 775 then
hitrightwall := 1
else
hitrightwall := 0
hitleftwall := 0
end if
%Creates a delay so that bullets can only fire every 10 frames.
if firedelay = 1 then
if firedelaytime < 10 then
firedelaytime += 1
else
firedelaytime := 0
firedelay := 0
end if
end if
%Draws lazers and checks if they are off the screen.
for count : 1 .. upper (fired)
if fired (count) = 1 then
if lazery (count) <= 775 then
lazery (count) += 5
Draw.FillOval (lazerx (count), lazery (count), 5, 0 + 5 , yellow)
else
fired (count) := 0
totalfired := 0
end if
end if
end for
for count : 1 .. upper (fired)
if fired (count) = 1 then
lazerhit1 := sqrt ((asteroidx1 - lazerx(count)) ** 2 + (asteroidy - 5) **2)
lazerhit2 := sqrt ((asteroidx2 - lazerx(count)) ** 2 + (asteroidy - 5) **2)
lazerhit3 := sqrt ((asteroidx3 - lazerx(count)) ** 2 + (asteroidy - 5) **2)
lazerhit4 := sqrt ((asteroidx4 - lazerx(count)) ** 2 + (asteroidy - 5) **2)
lazerhit5 := sqrt ((asteroidx5 - lazerx(count)) ** 2 + (asteroidy - 5) **2)
if lazerhit1 <= 5 +15 or lazerhit2 <=5 + 15 or lazerhit3 <= 5 + 15 or lazerhit4 <= 5 + 15 or lazerhit5 <= 5 + 15 then
Draw.FillOval (200,200,20,20,red)
end if
end if
end for
%Draws player
Draw.FillOval (x,30,8,8, blue)
View.Update
delay (10)
cls
cls
if asteroidy >= -25 then
Draw.FillOval ( asteroidx1, asteroidy, 15, 15, grey)
Draw.FillOval ( asteroidx2, asteroidy, 15, 15, grey)
Draw.FillOval ( asteroidx3, asteroidy, 15, 15, grey)
Draw.FillOval ( asteroidx4, asteroidy, 15, 15, grey)
Draw.FillOval ( asteroidx5, asteroidy, 15, 15, grey)
delay (20)
asteroidy -=5
else
asteroidx1 := Rand.Int (10, 755)
asteroidx2 := Rand.Int (150, 755)
asteroidx3 :=Rand.Int (500, 755)
asteroidx4 :=Rand.Int (20, 755)
asteroidy += 600
asteroidx5 :=Rand.Int (20,700)
end if
distance_between_centres1 := sqrt ((asteroidx1 - x) ** 2 + (asteroidy - 30) **2)
distance_between_centres2 := sqrt ((asteroidx2 - x) ** 2 + (asteroidy - 30) **2)
distance_between_centres3 := sqrt ((asteroidx3 - x) ** 2 + (asteroidy - 30) **2)
distance_between_centres4 := sqrt ((asteroidx4 - x) ** 2 + (asteroidy - 30) **2)
distance_between_centres5 := sqrt ((asteroidx1 - x) ** 2 + (asteroidy - 30) **2)
if distance_between_centres1 <= 8 + 15 or distance_between_centres2 <= 8 + 15 or distance_between_centres3 <= 8 + 15 or distance_between_centres4 <= 8 + 15 or distance_between_centres5 <= 8 + 15 then
exit
end if
end loop |
|
|
|
|
|
|
|