
-----------------------------------
faulkner16
Fri May 09, 2008 10:16 pm

space invaders help
-----------------------------------
i need some help with my space invaders program, with the help of some tutorials i have it basically the way i want, but the collision detection for teh aliens does not seem to be working correctly.. can anyone help me out with it?

* some of it is copied from a tutorial, i will be making a complete new one later, and just going off this one, but most of it is different anyways

View.Set ("graphics:800;600,offscreenonly")
%Variables
var x, hitleftwall, hitrightwall, firedelay, firedelaytime, totalfired, statsenemy : int := 0
var lazerx, lazery, fired : array 1 .. 10 of int
var enemyhp, enemyx, enemyy : array 1 .. 20 of int
var chars : array char of boolean
var mypic : int := Pic.FileNew ("spaceship2.jpg")
var mypic2 : int := Pic.FileNew ("enemy.jpg")
var mypic3 : int := Pic.FileNew ("enemy2.jpg")
var mypic4 : int := Pic.FileNew ("enemy3.jpg")
var mypic5 : int := Pic.FileNew ("bomb.jpg")
var continue : string
var font : int
var diameter : int
diameter := 25
var bomb : int
bomb := 100

%instructions
font := Font.New ("serif:40")
Draw.Text ("INSTRUCTIONS", maxx div 2 - 180, 450, font, red)
font := Font.New ("serif:20")
Draw.Text ("left....  move ship left", maxx div 2 - 120, 350, font, red)
Draw.Text ("right...  move ship right ", maxx div 2 - 120, 320, font, red)
Draw.Text ("up......  shoot laser", maxx div 2 - 120, 290, font, red)
Draw.Text ("down....  shoot bomb", maxx div 2 - 120, 260, font, red)
Draw.Text ("Shoot down all the aliens. Each alien has three lives. After you kill", 20, 190, font, red)
Draw.Text ("all the aliens, you will move on to the next level, where the aliens", 20, 160, font, red)
Draw.Text ("will begin to return fire. Each time you move up a level, you will get", 20, 130, font, red)
Draw.Text ("one bomb, which will randomly take out 3 of the aliens at once.", 20, 100, font, red)
loop
    Draw.Text ("Continue? Press y, then enter to continue onto the game", 20, 40, font, red)
    get continue
    exit when continue = "y"
end loop


%settings
x := 400
statsenemy := 12
colorback (black)
cls
%Sets up the arrays
for count : 1 .. statsenemy
    enemyhp (count) := 3
    enemyx (count) := 50 + (50 * count)
    enemyy (count) := 500
end for
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 = enemyy (count) - 10 and lazerx (count1) = enemyx (count) - 30 or lazery (count1) = 1 then
                        enemyhp (count) -= 1
                    end if
                end if
            end if
        end for
    end for
    %Draws enemys with correct color
    for count : 1 .. statsenemy
        if enemyhp (count) = 3 then
            Pic.Draw (mypic2, (enemyx (count)), (enemyy (count)), 0)
        elsif enemyhp (count) = 2 then
            Pic.Draw (mypic3, (enemyx (count)), (enemyy (count)), 0)
        elsif enemyhp (count) = 1 then
            Pic.Draw (mypic4, (enemyx (count)), (enemyy (count)), 0)
        end if
    end for

    %Draws lazers and checks if they are off the screen.
    for count : 1 .. upper (fired)
        if fired (count) = 1 then
            if lazery (count) = enemyy (count) - 10 and lazerx (count1) = enemyx (count) - 30 or lazery (count1) = 1 then
                        enemyhp (count) -= 1
                    end if
                end if
            end if
        end for
    end for
    %Draws player
    Pic.Draw (mypic, x, 30, 0)

    View.Update
    delay (10)
    cls
end loop

Gandalf says, "Use code tags!"

-----------------------------------
isaiahk9
Sat May 10, 2008 7:18 am

RE:space invaders help
-----------------------------------
So long as your aliens aren't moving (right now) you could just say if your bomb or laser blast is a multiple of however much the aliens are separated by (say each alien has 20 pixels between them and the next alien) and your laser blast or bomb is a multiple of how the aliens are placed in height (up 20 from the last row of aliens will get you to the next), then tat coordinate of laser, and the alien on it loses some health.  If the aliens are moving, say in a for loop with alienx := alienx + 5, then just make it if the laser blast or bomb is a multiple of whatever the distance is between aliens + 5.

If this isn't clear enough, then ask for clarification and I can give it.
