Computer Science Canada

Multiple asteroids.

Author:  MysticVegeta [ 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

loop
    drawShipAndShoot
end loop

Author:  jamonathin [ Mon Jun 20, 2005 8:25 am ]
Post 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

loop
    drawShipAndShoot
end loop

Author:  MysticVegeta [ Mon Jun 20, 2005 8:38 am ]
Post 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.

Author:  Cervantes [ Tue Jun 21, 2005 4:22 pm ]
Post 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?

Author:  MysticVegeta [ Tue Jun 21, 2005 4:39 pm ]
Post 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.

Author:  Cervantes [ Tue Jun 21, 2005 5:37 pm ]
Post 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.

Author:  MysticVegeta [ Tue Jun 21, 2005 5:53 pm ]
Post 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??

Author:  Cervantes [ Tue Jun 21, 2005 6:29 pm ]
Post 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...

Author:  jamonathin [ Tue Jun 21, 2005 9:41 pm ]
Post 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.

Author:  MysticVegeta [ Wed Jun 22, 2005 6:26 am ]
Post 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.

Author:  Cervantes [ Wed Jun 22, 2005 7:05 am ]
Post 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.

Author:  MysticVegeta [ Wed Jun 22, 2005 11:35 am ]
Post 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 -_-''

Author:  vagyb [ Thu Jun 23, 2005 8:46 pm ]
Post 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

Author:  jamonathin [ Thu Jun 23, 2005 10:09 pm ]
Post 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.

Author:  vagyb [ Fri Jun 24, 2005 6:35 am ]
Post 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.

Author:  Cervantes [ Fri Jun 24, 2005 9:01 am ]
Post subject: 

Instead of doing something like
code:

speed = 20 - size

you could use the formula for kinetic energy. Ek=1/2*mv^2 Therefore, v = sqrt (2*Ek/m).
So, simply decide what you want the kinetic energy of the asteroids to be (this could be somewhat random for each asteroid) and then determine the mass (based on radius and density) and then plug those into the formula and find the appropriate velocity.

If you want to make things harder, just increase the kinetic energy.

Author:  jamonathin [ Fri Jun 24, 2005 10:10 am ]
Post subject: 

code:
Ek=1/2*mv^2 Therefore, v = sqrt (2*Ek/m).
Eh

Yeah, i wish I knew formula's. The only smart class I took this year was calculus.

Author:  MysticVegeta [ Fri Jun 24, 2005 12:22 pm ]
Post subject: 

uh... I think i will go with simplycity...
talk about calculus formulas, let me first spend my first day in Grade 10 math in September lol. Those functions and relations just do nothing but confuse me....

Author:  Cervantes [ Fri Jun 24, 2005 7:22 pm ]
Post subject: 

Simplicity?

Turing:

View.Set ("graphics:600;600;nobuttonbar;offscreenonly")
colourback (black)

module Asteroid
    export add, update, draw, total
    const asteroidDensity := 5
    var asteroid : flexible array 1 .. 0 of
        record
            x, y : real
            velocity, angle : real
            kineticEnergy : real
            radius : int
        end record

    proc add
        new asteroid, upper (asteroid) + 1
        asteroid (upper (asteroid)).radius := Rand.Int (10, 20)
        asteroid (upper (asteroid)).x := Rand.Int (maxx + 10, maxx + 50)
        asteroid (upper (asteroid)).y := Rand.Int (asteroid (upper (asteroid)).radius, maxy - asteroid (upper (asteroid)).radius)
        asteroid (upper (asteroid)).kineticEnergy := Rand.Int (30000, 100000)
        asteroid (upper (asteroid)).angle := Rand.Int (170, 190)
        asteroid (upper (asteroid)).velocity := sqrt (2 * asteroid (upper (asteroid)).kineticEnergy /   %sqrt (2 * Kinetic Energy / mass)
            (4 / 3 * 3.14159265358979323846 * asteroid (upper (asteroid)).radius ** 3 * asteroidDensity))
    end add

    proc update
        for i : 1 .. upper (asteroid)
            asteroid (i).x += asteroid (i).velocity * cosd (asteroid (i).angle)
            asteroid (i).y += asteroid (i).velocity * sind (asteroid (i).angle)
        end for
    end update

    fcn total : int
        result upper (asteroid)
    end total

    proc draw
        for i : 1 .. upper (asteroid)
            Draw.FillOval (round (asteroid (i).x), round (asteroid (i).y), asteroid (i).radius, asteroid (i).radius, 30)
        end for
    end draw

end Asteroid


loop
    if Asteroid.total < 30 then
        Asteroid.add
    end if

    Asteroid.update

    cls
    Asteroid.draw
    View.Update
    delay (5)

    exit when hasch()
end loop


Here's a question to ponder. Is it better to make a module that contains an array of asteroids, or to create an asteroid class and create an array of pointers to the class?

Author:  MysticVegeta [ Sat Jun 25, 2005 8:11 am ]
Post subject: 

Shocked I dont think you know what i meant from my previous post, I dont know modules, i dont know classes, i dont know pointers (except maybe sometimes i came across pointers in game-hacking) but that was a long time ago. Anyways, all my asteroids will be the same shape, because they really are not asteroids, they are spaceships. I will replace the drawfilloval with Pic.Draw and make necessary changes. So i dont think i will need Kinetic Energy formula. Speed will be random. size/mass will be constant. If i change my mind i might use the module.

Cervantes: You love to use flexible array everywhere dont you? Every code i see in your post has array : 1..0 of int/string. lol.

Author:  Cervantes [ Sat Jun 25, 2005 9:39 am ]
Post subject: 

MysticVegeta wrote:
Cervantes: You love to use flexible array everywhere dont you? Every code i see in your post has array : 1..0 of int/string. lol.

Whell, this program should, most definately use flexible arrays. Even if you were to use a class, you'd make a flexible array of pointers to the class. Smile

Author:  jamonathin [ Sat Jun 25, 2005 7:53 pm ]
Post subject: 

Yeah because flexible arrays help keep everything organized, and with them you dont need a variable to represent how many asteroids you have. You can simple use upper (fart)


: