Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Better space invader
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
petree08




PostPosted: Wed Jan 10, 2007 11:28 am   Post subject: Better space invader

Yeah i got rid of the proccesses and the shooting is much improved
I put a boss near the end
if you fond it to hard to play then just make the num of enimies const smaller
enjoy

code:

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

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

end Find_Length

process Boss_Defeat_Sound
    for D : 1 .. 40
        sound (D * 10, 100)
    end for
end Boss_Defeat_Sound

process Boom
    sound (100, 100)
end Boom

process Boom2
    sound (300, 200)
    sound (100, 100)
end Boom2
%%%%Vars and consts
const NUM_OF_STARS := 100
const BULLET_NUM := 20
const NUM_OF_ENEMIES := 100
const SPEED := 4

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 DXB : int1 := 1
var BulletNum : nat1 := 1
var Fire : boolean := false
var BossKill : boolean := false
var BossHealth : int1 := 100
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 Boss : boolean := false
var KillCount : nat
var BX, BY : int
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,title:SPACE ACE")
colorback (7)
cls

%starting values for vars



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)
    YE (Index) := maxy + 200
    XB (Index) := XE (Index)
    YB (Index) := YE (Index)

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


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

end for
BX := MID_X - NUM_OF_ENEMIES div 2
BY := maxy - 40

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 ("Space 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, 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)
View.Update

PauseKey := getchar


cls


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 ""

color (10)
put "MISSION OBJECTIVE"

put "********"
put "The point of this mission is to destroy the enemy's latest weapon"
put "The weapon has been code named LIGHTING ROD because of it's new "
put "electro-beam canon witch causes great damage."
put "The enemy fighters should fall back to let the LIGHTING ROD destroy you"
put "if you destroy enough of them"
put "good luck"
color (0)
put ""
put ""

put "The strength of your shields is shown by the bar across the top of the" ..
put " screen."

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
        if Boss = true then
            XE (Index) := BX + Index
            YE (Index) := BY
        else

            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)
        end if
        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)
            if Boss = false then
                drawfilloval (XB (Index), YB (Index), 2, 3, 10)

            else
                drawfilloval (XB (Index), YB (Index), 2, 3, 14)
            end if
            if Count (Index) > 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 Boom2
            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)
        if Boss = false then
            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
                    if KillCount >= 200 then
                        Boss := true
                    end if


                    fork Boom
                end if



            end for
        end if
    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 (' ') and Fire = false then
        Fire := true
        YPB (BulletNum) := Y
        XPB (BulletNum) := X
        PCount (BulletNum) := 0
        Hit (BulletNum) := true
        GotOne (BulletNum) := false
        if BulletNum >= BULLET_NUM then
            BulletNum := 1
        else
            BulletNum := BulletNum + 1
        end if

    elsif Key (' ') = false then
        Fire := false

    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) > 50 then
                Hit (Index) := false
            end if
        end if
        if GotOne (Index) = false and Hit (Index) then
            drawfilloval (XPB (Index), YPB (Index), 2, 3, 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)




    if BossKill = false and Boss = true then
        BX := BX + DXB
        if BX + NUM_OF_ENEMIES > maxx or BX <= 1 then
            DXB := -DXB
        end if
        drawfillbox (BX, BY, BX + NUM_OF_ENEMIES, BY + 20, 15)
        drawfilloval (BX + NUM_OF_ENEMIES div 2, BY + 3, 3, 4, 10)
        IntToString := intstr (BossHealth)
        drawfillbox (maxx - 18, 20 + (BossHealth * 2), maxx - 25, 20, 12)
        drawbox (maxx - 19, 21 + (101 * 2), maxx - 24, 22, 0)
        Font.Draw (IntToString + "%", maxx - 20, 10, Font3, 0)
        for Index : 1 .. BULLET_NUM
            if YPB (Index) < BY + 10 and YPB (Index) > BY and XPB (Index) > BX and
                    XPB (Index) < BX + NUM_OF_ENEMIES and GotOne (Index) = false then
                BossHealth := BossHealth - 1
                if BossHealth <= 0 then
                    BossKill := true
                end if
                GotOne (Index) := true


                fork Boom
            end if
        end for
    end if
    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 BossKill = true
    delay (10)
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
    fork Boss_Defeat_Sound
    for Explosion : 1 .. 300

        randint (XEX, BX + NUM_OF_ENEMIES div 2 - 70, BX + NUM_OF_ENEMIES div 2 + 70)
        randint (YEX, BY - 70, BY + 70)





        randint (RY, 1, 2)
        if RY = 1 then
            RY := 12
        else
            RY := 14
        end if
        drawdot (XEX, YEX, RY)
    end for
    Font.Draw ("You Win!", MID_X - 50, MID_Y, Font1, 10)
end if

View.Update
PauseKey := getchar
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: