Computer Science Canada

space invaders help

Author:  faulkner16 [ Fri May 09, 2008 10:16 pm ]
Post subject:  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

Turing:
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 <= 9 then
                totalfired += 1
                fired (totalfired) := 1
                firedelay := 1
                lazerx (totalfired) := x + 25
                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
    %To be used for special move sometime in future (like a bomb)

    if chars (KEY_DOWN_ARROW) then
        Pic.Draw (mypic5, 150, 0, 0)
        bomb := bomb - 1
        if bomb = 50 then
            statsenemy := statsenemy - 3
        end if
    end if


    %Collision detection with the left 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
    %Checks for collision with enemy and lazer
    for count : 1 .. statsenemy
        for count1 : 1 .. upper (fired)
            if fired (count1) = 1 then
                if lazery (count1) >= enemyy (count) - 10 and lazerx (count1) = enemyx (count) - 30 or lazery (count1) <= enemyy (count) - 7 and lazerx (count1) = enemyx (count) + 30 then
                    fired (count) := 0
                    totalfired := 0
                    if enemyhp (count) >= 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) <= 775 then
                lazery (count) += 5
                drawline (lazerx (count), lazery (count), lazerx (count), lazery (count) + 5, white)
            else
                fired (count) := 0
                totalfired := 0
            end if
        end if
    end for

    %Checks if enemy is hit by lazer and then deletes lazer and takes away the hp.
    for count : 1 .. statsenemy
        for count1 : 1 .. upper (fired)
            if fired (count1) = 1 then
                if lazery (count1) >= enemyy (count) - 10 and lazerx (count1) = enemyx (count) - 30 or lazery (count1) <= enemyy (count) - 7 and lazerx (count1) = enemyx (count) + 30 then
                    fired (count) := 0
                    totalfired := 0
                    if enemyhp (count) >= 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!"

Author:  isaiahk9 [ Sat May 10, 2008 7:18 am ]
Post subject:  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.


: