Computer Science Canada

Space Invaders Help

Author:  Ayoun [ Thu Jun 09, 2005 5:45 pm ]
Post subject:  Space Invaders Help

Ok i;m making a star wars invaders game and i need to know how to make my enemies in an array and how to have my scores

Author:  Ayoun [ Thu Jun 09, 2005 5:50 pm ]
Post subject:  Here is the code

code:
var x, y : int := 0
var key : array char of boolean
var plane : int
plane := Pic.FileNew ("X-Wing1.bmp")
var back := Pic.FileNew ("space.jpg")
x := maxx div 2
y := maxy div 2
setscreen ("graphics:max;max")

View.Set ("offscreenonly, nocursor")



% delay for background

setscreen ("graphics:max,max,offscreenonly")
proc myDelay (var last : int, rate : int)
    var now : int
    loop
        clock (now)
        exit when now - last >= rate
    end loop
    last := now
end myDelay


% background

proc background (var y : int)
    y := y - 6
    if y < maxy - 1178 then
        y := 0
    end if

    Pic.Draw (back, 0, y, picCopy)
end background
var gunReady : boolean := true      % because processes can not take call be reference
% parameters this need to be global

type shotType :                     % Note how the use of records makes it easy to organize my code
    record
        x, y : real
        vx, vy : real %velocity of x, y
        live : boolean
    end record


process coolGun
    delay (200)
    gunReady := true
end coolGun

proc vectorToXY (mag, ang : real, var x, y : real)

    x := cosd (ang) * mag
    y := sind (ang) * mag
end vectorToXY

proc xyToVector (x, y : real, var mag, ang : real)

    mag := sqrt (x ** 2 + y ** 2)
    if mag > 0 then
        if x >= 0 then
            ang := arcsind (y / mag)
        else
            ang := 180 - arcsind (y / mag)
        end if
    else
        ang := 0
    end if
end xyToVector

proc trackInput (var mag, ang : real, var shotFired : boolean)
    shotFired := false
    var chars : array char of boolean
    Input.KeyDown (chars)
    if chars (' ') then    %Fire a shot
        shotFired := true
    end if
end trackInput

proc addShot (var shots : array 1 .. 10 of shotType)
    var mx, my, mb : int
    if gunReady then
        for i : 1 .. 10
            if not shots (i).live then
                gunReady := false
                fork coolGun            % this will set gunReady back to true in 150 mill
                shots (i).live := true
                shots (i).x := x - 7    %   This is so the shots come out of the ship
                shots (i).y := y + 9
                exit       % this is so that the shot only takes up one slot
            end if
        end for
    end if
end addShot

proc trackShots (var shots : array 1 .. 10 of shotType)
    for i : 1 .. 10
        if shots (i).live then
            if shots (i).y > maxy then
                shots (i).live := false     % so we can recycle the shots
            else
                shots (i).y += 5
            end if
        end if
    end for
end trackShots


proc drawScene (mag, ang : real, shots : array 1 .. 10 of shotType)
    var linex, liney : real
    var dx, dy : real

    vectorToXY (mag, ang, dx, dy)
    vectorToXY (25, ang, linex, liney)
    for i : 1 .. 10
        if shots (i).live then
            drawfilloval (round (shots (i).x + 28), round (shots (i).y - 5), 1, 10, brightblue)
            drawfilloval (round (shots (i).x - 24), round (shots (i).y - 5), 1, 10, brightblue)
        end if
    end for

end drawScene

var shots : array 1 .. 10 of shotType       % All the data about the 10 shots that our
% gun can fire
for i : 1 .. 10
    shots (i).live := false                 % clear the shots array to allow for new shots
end for

% ----------------------------   MAINLINE   ------------------------------------

var backy, last := 0

loop
    Input.KeyDown (key)
    if key (KEY_UP_ARROW) then
        y := y + 4
    end if
    if key (KEY_DOWN_ARROW) then
        y := y - 4
    end if
    if key (KEY_RIGHT_ARROW) then
        x := x + 4
    end if
    if key (KEY_LEFT_ARROW) then
        x := x - 4
    end if
    x := max (35, x)
    y := max (35, y)
    x := min (maxx - 93, x)
    y := min (maxy - 10, y)

    background (backy)
    %   Draw the plane
    Pic.Draw (plane, x - 35, y - 35, picMerge)

    %  Controls
    myDelay (last, 20)
    View.Update
    var shotFired : boolean     % Did the user try to shoot (won't shoot if gun is "hot"
    var ang : real := 0     % current angle of the big gun
    var mag : real := 10    % Amount of power being used for the shots

 
    trackInput (mag, ang, shotFired)
    if shotFired then
        addShot (shots)
    end if
    trackShots (shots)
    drawScene (mag, ang, shots)
    View.Update
    delay (10)
end loop
code:
code:

Author:  vagyb [ Thu Jun 09, 2005 8:31 pm ]
Post subject: 

well ur code seems to not be working since u didn't list one of the variables

but search for arrays tutorial it will explain

for scoring the simplest thing to do in my opinion is have a counter. and if astroid is broken then counter +=10, this way u get 10 points every time astroid is broken, at the end u just type put "your scrore is".. put counter


: