Computer Science Canada

Space Invader

Author:  petree08 [ Tue Dec 12, 2006 2:47 pm ]
Post subject:  Space Invader

This is probably the best game i've made in the 1.5 years i've been programming

Is like space invaders

There is a lot of code and not much is commented but i have some constants that change the speed of how it runs

enjoy


code:



function Find_Length (X1, Y1, X2, Y2 : int) : real

    result sqrt (((X2 - X1) ** 2) + ((Y2 - Y1) ** 2))

end Find_Length


process Boom
    sound (100, 100)
end Boom
process Shield_Sound
    sound (300, 100)
    sound (200, 50)
end Shield_Sound

%%%%Vars and consts

% these constants can be changed if you are getting some speed issues
const OVER_ALL_DELAY := 0 % if you don't want to tweak the others and the
% program runs way to fast
% make this bigger
const BULLET_DELAY_FACTOR := 20000 % change if bullets shoot to slow
const NUM_OF_STARS := 100 % don't bother changing unless computer
% blows very very bad.
const BULLET_NUM := 10 % change if you whant more bullets on screen (player's)
const NUM_OF_ENEMIES := 25
const SPEED := 2 % determines the speed the enemies move and how fast evey
% thing goes not recomended to change 2 much , wont work if less than 1


var XS, YS : array 1 .. NUM_OF_STARS of int
var XE, YE : array 1 .. NUM_OF_ENEMIES of int
var XB, YB : array 1 .. NUM_OF_ENEMIES of int
var DX, DY : array 1 .. NUM_OF_ENEMIES of int
var Count : array 1 .. NUM_OF_ENEMIES of int

var PCount : array 1 .. BULLET_NUM of nat2
var XPB, YPB : array 1 .. BULLET_NUM of int
var PauseKey : char
var EnHit : array 1 .. NUM_OF_ENEMIES of boolean
var Hit : array 1 .. BULLET_NUM of boolean
var GotOne : array 1 .. BULLET_NUM of boolean
var Key : array char of boolean

var KillCount : nat

var X, Y : int
var Shields : int := 100
var Font1 : int := Font.New ("Fixedsys:50")
var Font2 : int := Font.New ("Fixedsys:30")
var Font3 : int := Font.New ("Fixedsys:12")
var IntToString : string
var HC : int := 10
% setup screen
setscreen ("graphics:max,max,nobuttonbar,offscreenonly,nocursor,title:STAR ACE")
colorback (7)
cls

%starting values for vars

process Shoot_Pause


    for Index : 1 .. BULLET_NUM
        if Hit (Index) = false and Key (' ') then

            GotOne (Index) := false
            Hit (Index) := true
            PCount (Index) := 0
            XPB (Index) := X
            YPB (Index) := Y

            delay (BULLET_DELAY_FACTOR)
        end if

    end for

end Shoot_Pause

const MID_X := maxx div 2
const MID_Y := maxy div 2

for Index : 1 .. NUM_OF_STARS
    randint (XS (Index), 1, maxx)
    randint (YS (Index), 1, maxy)
end for

for Index : 1 .. NUM_OF_ENEMIES
    randint (XE (Index), 1, maxx)
    randint (YE (Index), maxy + 50, maxy + 200)
    XB (Index) := XE (Index)
    YB (Index) := YE (Index)

    randint (DX (Index), -1, 1)


    Count (Index) := 0
    EnHit (Index) := false

end for

X := MID_X
Y := 15
for Index : 1 .. BULLET_NUM
    Hit (Index) := false
    PCount (Index) := 0
    GotOne (Index) := false
    KillCount := 0
    YPB (Index) := 1
    XPB (Index) := 1
end for

%Menu

cls
Font.Draw ("Star Ace", 150, 400, Font1, 10)
Font.Draw ("Made by Peter Watt", 150, 200, Font2, 0)
Font.Draw ("(press any key to continue)", 50, 100, Font2, 10)
for Index : 1 .. NUM_OF_STARS
    drawdot (XS (Index), YS (Index), 0)


    if YS (Index) < 1 then
        YS (Index) := maxy
        randint (XS (Index), 1, maxx)
    end if

    YS (Index) := YS (Index) - SPEED
end for

drawfilloval (X, Y + 3, 5, 5, 15)
drawfillbox (X - 5, Y - 10, X + 5, Y + 5, 15)
drawfillbox (X - 10, Y - 8, X + 10, Y - 5, 15)
drawfillbox (X - 2, Y + 2, X + 2, Y - 1, 10)
View.Update

PauseKey := getchar


cls
color (0)
put "The purpose of this game is to destroy has many .."
put " enemey fighters as you can."
put "You win if you have destroyed 400 ships."
put "You lose if your ship is destroyed."
put "The strength of your shields is shown by the bar across the top of the" ..
put " screen."
put ""
put ""
color (15)
put "CONTROLS"

put "********"
put ""
put "Fire : space bar"
put "Pause : enter key"
put "Move left : left arrow key"
put "Move right : right arrow key"
put ""
put "Press any key to continue"
View.Update
PauseKey := getchar

%%%%%%%MAIN%%%%%%%%%%%%%%





color (0)
loop
    cls

    %STARS
    for Index : 1 .. NUM_OF_STARS
        drawdot (XS (Index), YS (Index), 0)


        if YS (Index) < 1 then
            YS (Index) := maxy
            randint (XS (Index), 1, maxx)
        end if

        YS (Index) := YS (Index) - SPEED
    end for


    %BAD GUYS

    for Index : 1 .. NUM_OF_ENEMIES

        drawfilloval (XE (Index), YE (Index) - 3, 5, 5, 15)
        drawfillbox (XE (Index) - 5, YE (Index) + 10, XE (Index) + 5,
            YE (Index) - 5, 15)

        drawfillbox (XE (Index) - 10, YE (Index) + 8, XE (Index) + 10
            , YE (Index) + 5, 15)
        drawfillbox (XE (Index) - 2, YE (Index) - 2, XE (Index) + 2,
            YE (Index) + 1, 12)

        if XE (Index) > maxx or XE (Index) < 1 then
            DX (Index) := -DX (Index)
        end if
        XE (Index) := XE (Index) + DX (Index)


        if XE (Index) < X + 10 and XE (Index) > X - 10
                and EnHit (Index) = false then

            EnHit (Index) := true
            Count (Index) := 0
            XB (Index) := XE (Index)
            YB (Index) := YE (Index)
        end if
        if EnHit (Index) = true then
            Count (Index) := Count (Index) + 1
            YB (Index) := YB (Index) - (SPEED * 3)
            drawfilloval (XB (Index), YB (Index), 2, 3, 10)
            if Count (Index) > maxy div (SPEED * 2) + 100 then
                EnHit (Index) := false
                Count (Index) := 0
            end if
            if XB (Index) < X + 10 and XB (Index) > X - 10
                    and YB (Index) > Y - 8 and YB (Index) < Y + 5 then
                Shields := Shields - 2
                fork Shield_Sound
            end if

        end if


        if YE (Index) < 1 then
            randint (YE (Index), maxy + 50, maxy + 200)
            randint (XE (Index), 1, maxx)
        end if
        YE (Index) := YE (Index) - (SPEED div 2)

        for PlayerHit : 1 .. BULLET_NUM

            if YPB (PlayerHit) < YE (Index) + 10 and
                    YPB (PlayerHit) > YE (Index) - 5 and
                    XPB (PlayerHit) > XE (Index) - 10 and
                    XPB (PlayerHit) < XE (Index) + 10
                    and GotOne (PlayerHit) = false then

                randint (YE (Index), maxy + 50, maxy + 200)
                randint (XE (Index), 1, maxx)
                GotOne (PlayerHit) := true
                KillCount := KillCount + 1


                fork Boom
            end if



        end for
    end for
    %Player
    Input.KeyDown (Key)

    if X - 10 < 1 then
        X := X + SPEED
    elsif X + 10 > maxx then
        X := X - SPEED
    end if

    if Key (KEY_RIGHT_ARROW) then
        X := X + SPEED
    elsif Key (KEY_LEFT_ARROW) then
        X := X - SPEED
    end if
    % pause
    if Key (KEY_ENTER) then

        Font.Draw ("Game Paused", MID_X - 200, MID_Y + 100, Font1, 0)
        Font.Draw ("(press any key to continue", MID_X - 100, MID_Y, Font3, 0)
        View.Update
        PauseKey := getchar
    end if

    %shooting
    if Key (' ') then
        fork Shoot_Pause

    end if
    for Index : 1 .. BULLET_NUM

        if Hit (Index) = true then

            YPB (Index) := YPB (Index) + (SPEED * 3)

            PCount (Index) := PCount (Index) + 1
            if PCount (Index) > maxy div (SPEED * 3) then
                Hit (Index) := false
            end if
        end if
        if GotOne (Index) = false and Hit (Index) then
            drawfilloval (XPB (Index), YPB (Index), 1, 2, 12)
        end if
    end for
    %drawplayer
    drawfilloval (X, Y + 3, 5, 5, 0)
    drawfillbox (X - 5, Y - 10, X + 5, Y + 5, 0)
    drawfillbox (X - 10, Y - 8, X + 10, Y - 5, 0)
    drawfillbox (X - 2, Y + 2, X + 2, Y - 1, 9)


    IntToString := intstr (KillCount)
    Font.Draw ("Enemies destroyed " + IntToString, 1, maxy - 35, Font3, 12)

    IntToString := intstr (Shields)
    if Shields < 60 and Shields > 30 then
        HC := 14
    elsif Shields < 30 then
        HC := 12
    end if
    drawbox (maxx - 400, maxy - 20, maxx - 200, maxy - 10, 0)
    drawfillbox (maxx - 398, maxy - 18, maxx - 398 + (Shields * 2)
        , maxy - 12, HC)

    Font.Draw (IntToString + "%", maxx - 198, maxy - 20, Font3, 0)



    View.Update
    exit when Shields <= 0 or KillCount >= 100
    delay (OVER_ALL_DELAY)
end loop

var XEX, YEX : int
var RY : int
if Shields <= 0 then
    Font.Draw ("You've been killed!", 1, MID_Y, Font1, 10)
    for Explosion : 1 .. 300
        loop
            randint (XEX, X - 40, X + 40)
            randint (YEX, Y - 40, Y + 40)
            exit when Find_Length (X, Y, XEX, YEX) < 40
        end loop

        randint (RY, 1, 2)
        if RY = 1 then
            RY := 12
        else
            RY := 14
        end if
        drawdot (XEX, YEX, RY)
    end for
else


    Font.Draw ("You Win!", MID_X - 50, MID_Y, Font1, 10)
end if

View.Update
PauseKey := getchar




told you there was a lot of code

Author:  uberwalla [ Thu Dec 14, 2006 9:19 pm ]
Post subject: 

i have to say... another great game man. it is a nice remake of the flash space invaders. well done. i like that health bar a lot better then the regular lives thing in the flash one.

just a few tips:
1) if you really think its a lot of code... just post the .t version instead of using tags and putting in Razz
2) maybe in a next version put different types of power ups

overall i think it was good.

Author:  CodeMonkey2000 [ Thu Dec 14, 2006 10:48 pm ]
Post subject: 

pretty good, but u should be able to shoot faster, and more often. i liked it more than the original.

Author:  Kerpail [ Mon Jan 08, 2007 6:34 pm ]
Post subject:  Re: Space Invader

When you die, how do you restart?

Author:  Clayton [ Mon Jan 08, 2007 6:39 pm ]
Post subject:  Re: Space Invader

might I suggest getting rid of the process at the top? I know it may seem trivial for such a small thing, but it really isn't necessary. make it a procedure and incorporate it into the main line instead. Also, cut down on all of the global variables/constants. Instead, organize your code into smaller methods that take parameters to cut down on the amount of global variables you use.

Author:  T-Revv [ Wed Mar 07, 2007 12:30 pm ]
Post subject:  RE:Space Invader

awsome - only way to say it

Author:  XxJU571NxX [ Sat Apr 28, 2007 9:37 pm ]
Post subject:  RE:Space Invader

Nice game.


: