Posted: Mon Jun 20, 2005 7:54 am Post subject: Multiple asteroids.
Hi, I am making a game and its a spaceship game, so i took the code posted before and here it is. The only problem is that how do i make it so there are more rocks at the same time. I tried to make the rocks variable to 20 but they al come at once and then they start flickering, here is the code.
code:
var winId := Window.Open ("graphics:max,max; nobuttonbar; offscreenonly")
var rocks : int := 2
colorback (255)
%%%%%%%%%%%%%%%%%%%%%%% O T H E R S H I P S %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var rock : array 1 .. 100 of
record
x, y, size, spd, dx : int
end record
for i : 1 .. upper (rock) %The amount in array (which is 100)
rock (i).x := Rand.Int (maxx, maxx + 400)
rock (i).y := Rand.Int (0, maxy)
rock (i).size := Rand.Int (10, 20)
rock (i).spd := Rand.Int (10, 20)
end for
proc asteroids
% Make move
for i : 1 .. rocks div 2
rock (i).x -= rock (i).spd
drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, yellow)
if rock (i).x <= 0 then
rock (1).x := Rand.Int (maxx, maxx + 10)
rock (1).y := Rand.Int (50, maxy - 60)
end if
end for
end asteroids
%%%%%%%% MAIN EVERYTHING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
proc drawShipAndShoot
cls
asteroids
View.Update
end drawShipAndShoot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Posted: Mon Jun 20, 2005 8:25 am Post subject: (No subject)
You should make your array flexible, and add 1 to it everytime it's necessary.
In your procedure asteroids, I changed your for loop to this.
code:
for i : 1 .. upper (rock) by 2
that counts up to the total amount of asteroids by 2, so that we dont have an insane amount. When you get around to your collision checking, use that same for loop, or else your program will be screwy.
This code is for when you adda new asteroid. For right now I just make the spacebar adda new asteroid, so you could see for yourself. Just take hat out and put it where its necessary later on.
code:
new rock, upper (rock) + 1
rock (upper (rock)).x := Rand.Int (maxx, maxx + 400)
rock (upper (rock)).y := Rand.Int (0, maxy)
rock (upper (rock)).size := Rand.Int (10, 20)
rock (upper (rock)).spd := Rand.Int (3, 7)
If you need any more help, juss ask away, or check previous topic on this.
code:
var winId := Window.Open ("graphics:max,max; nobuttonbar; offscreenonly")
colorback (255)
var key : array char of boolean
%%%%%%%%%%%%%%%%%%%%%%% O T H E R S H I P S %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var rock : flexible array 1 .. 1 of
record
x, y, size, spd, dx : int
end record
for i : 1 .. upper (rock) %The amount in array (which is 100)
rock (i).x := Rand.Int (maxx, maxx + 400)
rock (i).y := Rand.Int (0, maxy)
rock (i).size := Rand.Int (10, 20)
rock (i).spd := Rand.Int (3, 7)
end for
proc asteroids
% Make move
for i : 1 .. upper (rock) by 2
rock (i).x -= rock (i).spd
drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, yellow)
if rock (i).x <= 0 then
rock (i).x := Rand.Int (maxx, maxx + 400)
rock (i).y := Rand.Int (0, maxy)
rock (i).size := Rand.Int (10, 20)
rock (i).spd := Rand.Int (3, 7)
end if
end for
end asteroids
%%%%%%%% MAIN EVERYTHING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
proc drawShipAndShoot
cls
Input.KeyDown (key)
if key (' ') then
new rock, upper (rock) + 1
rock (upper (rock)).x := Rand.Int (maxx, maxx + 400)
rock (upper (rock)).y := Rand.Int (0, maxy)
rock (upper (rock)).size := Rand.Int (10, 20)
rock (upper (rock)).spd := Rand.Int (3, 7)
delay (100)
end if
asteroids
View.Update
delay (10)
end drawShipAndShoot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Posted: Mon Jun 20, 2005 8:38 am Post subject: (No subject)
The flexible array will keep on adding the asteroids though, i want something that like shoots mutiple asteroids so that there are like more than 1 at a time on the screen.
Cervantes
Posted: Tue Jun 21, 2005 4:22 pm Post subject: (No subject)
MysticVegeta wrote:
The flexible array will keep on adding the asteroids though, i want something that like shoots mutiple asteroids so that there are like more than 1 at a time on the screen.
What? The flexible array does not add asteroids, the new command adds asteroids, combined with the code to initialize their fields. And all this only happens when a certain condition is met: namely the space bar is pressed, as in Jamonathin's code. Are you asking for how to add more than one asteroid at the same time? Well, if that's the case, just use wrap a for loop around that new & initializing code, and you can add however many you want.
And Jamonathin, why did you put that by 2 on that for loop?
MysticVegeta
Posted: Tue Jun 21, 2005 4:39 pm Post subject: (No subject)
I meant new from the flexible array. By the ways, you see, the creator created a varible called "rocks" and when i make it more, say i change it to 4, then 2 of them (div 2) come at the same time (thats what i want) but later on, they keep flickering in the end of the screen. Cervantes can you tell me why this is happening. I just need to know because i haven't used records that much and i am not very familiar with them. Thanks alot.
Cervantes
Posted: Tue Jun 21, 2005 5:37 pm Post subject: (No subject)
Check this section out:
code:
proc asteroids
% Make move
for i : 1 .. rocks div 2
rock (i).x -= rock (i).spd
drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, yellow)
if rock (i).x <= 0 then
rock (1).x := Rand.Int (maxx, maxx + 10)
rock (1).y := Rand.Int (50, maxy - 60)
end if
end for
end asteroids
I'm not referring to the div 2 in the for loop part, though I still have no idea why you're doing that.
MysticVegeta
Posted: Tue Jun 21, 2005 5:53 pm Post subject: (No subject)
yea me neither so i removed it and made the rocks variable to 1. So well any idea still how i can do it??
Cervantes
Posted: Tue Jun 21, 2005 6:29 pm Post subject: (No subject)
MysticVegeta wrote:
made the rocks variable to 1.
You mean, set rocks to 1? Or set the subscript of the rock array/record to 1? Neither is good...
Sponsor Sponsor
jamonathin
Posted: Tue Jun 21, 2005 9:41 pm Post subject: (No subject)
Cervantes wrote:
And Jamonathin, why did you put that by 2 on that for loop?
I think I ment to put that div 2. Oh well, all results to the same thing. It was just there so there weren't an insane amount of asteroids flying. Its an easy method (for me at least) to reduce mass rocks.
MysticVegeta
Posted: Wed Jun 22, 2005 6:26 am Post subject: (No subject)
well thats weird, my collison detection works fine. So to make the asteroid disappear i have to reset the positions of x and y in the array. So i go.
code:
proc asteroids
% Make move
if lasercollidesasteroids then
for i : 1 .. upper (rock) %The amount in array (which is 100)
rock (i).x := Rand.Int (maxx, maxx + 400)
rock (i).y := Rand.Int (0, maxy)
rock (i).size := Rand.Int (10, 20)
rock (i).spd := Rand.Int (10, 20)
end for
lasercollidesasteroids := false
else
for i : 1 .. rocks div 2
rock (i).x -= rock (i).spd
drawfilloval (rock (i).x, rock (i).y, rock (i).size, rock (i).size, yellow)
if rock (i).x <= 0 then
rock (1).x := Rand.Int (maxx, maxx + 10)
rock (1).y := Rand.Int (50, maxy - 60)
end if
end for
end for
end asteroids
In this code when i change the rock amount to 20 then 10 of them come. The rest flicker, but when i shoot in the flickering, 10 of them come again. How can i implement this code so that when the amount of asteroids x's and y's are over they take random values. I think thats the problem.
Cervantes
Posted: Wed Jun 22, 2005 7:05 am Post subject: (No subject)
Okay, if you want to limit the number of asteroids that are coming at you, why would you use that div 2 in the for loop? Just make the rocks variable set to less!
Also, if lasercollidesasteroids, then you are resetting ALL of the asteroids. Chances are you only want to reset the one that you hit. You could make a function that takes a parameter (integer) that represents the asteroid ID (element) and returns true or false depnding on whether you shot it.
Also, why are you still doing this?
code:
if rock (i).x <= 0 then
rock (1).x := Rand.Int (maxx, maxx + 10)
rock (1).y := Rand.Int (50, maxy - 60)
end if
Shouldn't it be
code:
if rock (i).x <= 0 then
rock (i).x := Rand.Int (maxx, maxx + 10)
rock (i).y := Rand.Int (50, maxy - 60)
end if
That's what I was mentioning a few posts up.
MysticVegeta
Posted: Wed Jun 22, 2005 11:35 am Post subject: (No subject)
i see, i thought about it, now about lasercollidesasteroids, i was thinking of making one more boolean that will stay false untill the "x" of that lasercollided asteroid comes down to 10 in which case it will be redrawn. Yes i will put it in another function. Thanks Cervantes, Dam , i never thought that changing 2 character would change the flickering -_-''
vagyb
Posted: Thu Jun 23, 2005 8:46 pm Post subject: (No subject)
wouldn't it be easier to import like 6 astroid pictures (different looking) from bmp file and just change their x value (y too if u like) and make them reset at random locations each time they come
jamonathin
Posted: Thu Jun 23, 2005 10:09 pm Post subject: (No subject)
It wouldn't really make a difference. If he were to use bmp's he would have to change his laserdetection thing, because the oval is drawn in the center, not bottom left (unless he drew his asteroids at [x - Pic.Width(asteroid) div 2]).
Now what he could do is use the size of the asteroid, and make the speed of it relevent to its size. So if the size was 14, the speed could be 20 - size, or whatever.
Adding .bmp's wouldn't make it much easier. His drawfilloval would turn into Pic.Draw, and his x-values of the asteroid would change and thats it.
vagyb
Posted: Fri Jun 24, 2005 6:35 am Post subject: (No subject)
for my program i just made speeds ranging from like 2 to 6, and multiply each by a variable speed that has a initial value of 1, but later when u pass levels the speed becomes 2, therefor astroid speeds doubling.