
-----------------------------------
vapour99
Wed Jun 11, 2008 8:13 am

Using an Array with collision detection
-----------------------------------
My spaceshooter game uses arrays to create bullets and falling meteors. If I implement simple collision detection, it seems to use the x/y vars of the most recently created meteor (possibly due to the "upper" command?)

side notes: 

-I haven't fine tuned collision detection do to the entailed problem
-Asteroids and Meteors produce the same "falling meteor" effect but each use different arrays.
-asteroids are slightly smaller than the meteors and are grey.

Any help is much appreciated!

here is the code:

View.Set ("graphics:400;640,offscreenonly,") % screen will be offscreen only, 400x and 640y, and support graphics

%%%%%%%%%%%%%%%%%%%
%  Array Records  %
%%%%%%%%%%%%%%%%%%%

type bullet : % defines variables (x/y coordinates) to be contained in the array
    record
        x, y : real
    end record

type asteroid :
    record
        x2, y2 : real
    end record

type meteor :
    record
        x3, y3 : real
    end record

%%%%%%%%%%%%%%%%%%%
%    Variables    %
%%%%%%%%%%%%%%%%%%%

var buttonnumber, buttonupdown, buttons : int
var As1, As2, x3, y3, x2, y2, x, y, btn, shot, sopt : int
var exits : boolean := false
var firing, playing : boolean %defines wether or not the ship is firing
var time_last : real := -400 %times used to govern the first shot(can shoot right away)
var time_last2 : real := -400
var time_last3 : real := -400
var chars : array char of boolean %used for keys (pressed or not)
var bullets : flexible array 1 .. 0 of bullet %flexible array that determines the amount of bullets.
var asteroids : flexible array 1 .. 0 of asteroid
var meteors : flexible array 1 .. 0 of meteor
var back : int := Pic.FileNew ("e:/bckspace2.bmp")
var title : int := Pic.FileNew ("e:/spacegame title2.bmp")
var endtitle : int := Pic.FileNew ("e:/spacegame endtitle2.bmp")
var ship : int := Pic.FileNew ("e:/BattleCruiser.gif")
var font1, font2, win : int

%%%%%%%%%%%%%%%%%%%
%  Sound Procs    %
%%%%%%%%%%%%%%%%%%%

process bckgrnd % turing wont play .midi for some reason
    Music.PlayFile ("e:/DesertStrike.mid")
end bckgrnd

process shootsnd
 Music.PlayFileReturn ("e:/laser.mp3")
end shootsnd

%%%%%%%%%%%%%%%%%%%
%   Game Procs    %
%%%%%%%%%%%%%%%%%%%

proc input %the input procedure
    Mouse.Where (x, y, btn) %monitors the mouse position and buttons
    shot := 0
    if btn > 0 and Time.Elapsed - time_last > 400 then %if the mouse button is pressed, there will be a 400 millisecond delay between each shot
        new bullets, upper (bullets) + 1 %creates new values in the array
        bullets (upper (bullets)).x := x %X value for a bullet in the array
        bullets (upper (bullets)).y := y % Y value for a bullet in the array
        time_last := Time.Elapsed %resets the timer for another half second
        shot := 1
    end if
    Input.KeyDown (chars) % if ESC key is presses; the program exits the loop
    if chars (KEY_ESC) then
        exits := true %value "exits" is used outside of the loop
    end if
end input

proc astrds
    if Time.Elapsed - time_last2 > 400 then
        y2 := 640
        randint (x2, 0, 401)
        new asteroids, upper (asteroids) + 1
        asteroids (upper (asteroids)).x2 := x2
        asteroids (upper (asteroids)).y2 := y2
        time_last2 := Time.Elapsed
    end if
end astrds

proc meteorz
    if Time.Elapsed - time_last3 > 400 then
        y3 := 640
        randint (x3, 0, 399)
        new meteors, upper (meteors) + 1
        meteors (upper (meteors)).x3 := x3
        meteors (upper (meteors)).y3 := y3
        time_last3 := Time.Elapsed
    end if
end meteorz

proc update %the procedure for moving the bullets
    for i : 1 .. upper (bullets)
        bullets (i).y += 6 %bullet speed value 1 for home; 6 for school (changes according to computer speed)
    end for
end update

proc update2
    for j : 1 .. upper (asteroids)
        asteroids (j).y2 -= 8
    end for
end update2

proc update3
    for k : 1 .. upper (meteors)
        meteors (k).y3 -= 8
    end for
end update3

proc draw %draws the ship
    x := x - 40
    y := y - 50
    Pic.Draw (ship, x, y, picMerge)
end draw

proc drawastrds
    for j : 1 .. upper (asteroids)
        drawfilloval (round (asteroids (j).x2), round (asteroids (j).y2), As1, As1, grey)
    end for
end drawastrds

proc drawmeteors
    for k : 1 .. upper (meteors)
        drawfilloval (round (meteors (k).x3), round (meteors (k).y3), As2, As2, brown)
    end for
end drawmeteors

proc drawbullets
    for i : 1 .. upper (bullets)         % draws the bullets using the array information
        drawfilloval (round (bullets (i).x), round (bullets (i).y), 5, 5, 32)
    end for
end drawbullets

proc maxxy
    if x > 400 then
        x := 400
    elsif x < 0 then
        x := 0
    elsif y > 640 then
        y := 640
    elsif y < 0 then
        y := 0
    end if
end maxxy

%proc soption % sound option for shooting
%    Input.KeyDown (chars)
%    if chars ("q") then
%        sopt := 1
%    elsif chars ("w") then
%        sopt := 0
%    end if
%end soption

proc sounds
    if shot > 0 then
       fork shootsnd
    end if
    %if shot > 0 and sopt = 1 then
    %    Music.PlayFileReturn ("e:/elite-1.wav")
    %end if
end sounds

proc Acollide      %seems to use the x2 var of the most recent asteroid
    if x + 10 