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

Username:   Password: 
 RegisterRegister   
 A Helicopter/ shooter game
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
petree08




PostPosted: Thu Mar 01, 2007 11:57 am   Post subject: A Helicopter/ shooter game

This is a little game I made the point is to kill enemies until you win
I thought my helicopter looked kinda cool and I got the blades to spin (wich was hard until I found out about trig commands).
there are some fun modifactions you can do like if you change the FIRE_RATE const to 1 and hold spacebar it turns the bullets into a kind of whip type thing

code:


% subprograms
% draw the helicotper
procedure Draw_Copter (X, Y, C : int, Angle : real4)
    var XR, YR : int
    drawfillbox (X - 7, Y + 10, X + 13, Y - 10, C) % main body
    drawfilloval (X + 13, Y, 15, 10, C)
    drawfillbox (X - 7, Y + 6, X - 40, Y + 2, C) % tail
    drawfilloval (X - 7, Y, 7, 5, C)
    drawfillbox (X + 20, Y + 4, X + 26, Y - 1, 9) % window
    drawfillbox (X - 2, Y + 10, X + 2, Y + 16, C) % rotar base
    % top rotar
    XR := ((cos (Angle) * 40) + X) div 1
    YR := ((sin (Angle) * 7) + (Y + 16)) div 1
    % rotars
    drawline (X, Y + 16, XR, YR, 7)
    XR := (- (cos (Angle) * 40) + X) div 1
    YR := (- (sin (Angle) * 7) + (Y + 16)) div 1
    % rotars
    drawline (X, Y + 16, XR, YR, 7)
    % bottom rotar
    XR := ((cos (Angle) * 7) + X - 40) div 1
    YR := ((sin (Angle) * 7) + (Y + 4)) div 1
    % rotars
    drawline (X - 40, Y + 4, XR, YR, 7)
    XR := (- (cos (Angle) * 7) + X - 40) div 1
    YR := (- (sin (Angle) * 7) + (Y + 4)) div 1
    % rotars
    drawline (X - 40, Y + 4, XR, YR, 7)
    % landing gear
    drawline (X - 5, Y - 10, X - 5, Y - 15, 7)
    drawline (X + 10, Y - 10, X + 10, Y - 15, 7)
    drawline (X - 8, Y - 15, X + 15, Y - 15, 7)
    drawfillbox (X + 3, Y + 3, X + 12, Y - 3, 7)
    % star
    drawfillstar (X - 6, Y - 2, X + 2, Y + 4, 0)

end Draw_Copter



procedure Draw_Enemey (X, Y, C : int)
    drawfillbox (X - 15, Y + 5, X + 9, Y - 5, C)
    drawline (X + 15, Y, X, Y, 12)
    drawfilloval (X - 15, Y, 4, 3, 10)
    drawfilloval (X + 5, Y, 10, 4, C)
end Draw_Enemey

function Length_Find (X1, Y1, X2, Y2 : int) : real4

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


procedure Draw_Tree (X, Y : int)
    Draw.ThickLine (X, Y, X, Y + 15, 7, brown)
    drawfilloval (X, Y + 17, 8, 7, 194)
end Draw_Tree


% sound stuff
process Sound_Effect (Frequencey, Length : int)
    sound (Frequencey, Length)
end Sound_Effect
process Sound_Boom
    sound (75, 100)
    sound (35, 200)
    sound (35, 100)
end Sound_Boom

% screen set up
setscreen ("graphics:max,400,nobuttonbar,offscreenonly,nocursor")

% Constants

const COPTER_C := 7 % helicopter colour
const NUM_OF_ENIMIES := 20
const NUM_OF_BULLETS := 20
const SPEED := 1
const HELI_SPEED := 2
const ROTAR_SPEED := .3
const SPEED_B := 3
const NUM_OF_TREES := 30
const NUM_OF_CLOUDS := 5
const CLOUD_SPEED := 0.5
const FIRE_RATE := 20
const MID_X := maxx div 2
const MID_Y := maxy div 2
const VICTORY_COUNT := 400
const EMP_COUNT := 5
const EMP_RAD := 150




% VARS

var Choice : string (1)
var Shoot : boolean
var NumOfEMPs : int
var XC, YC : array 1 .. NUM_OF_CLOUDS of int
var XCT : array 1 .. NUM_OF_CLOUDS of real4
var Key : array char of boolean
var Shot : array 1 .. NUM_OF_BULLETS of boolean
var XP, YP : int %player position
var Angle : real4 % the rotar blade angle for helicopter
var XB, YB : array 1 .. NUM_OF_BULLETS of int % bullet
var XE, YE : array 1 .. NUM_OF_ENIMIES of int
var XBE, YBE : array 1 .. NUM_OF_ENIMIES of int
var ReloadCount : array 1 .. NUM_OF_ENIMIES of nat
var ShotE, Hit : array 1 .. NUM_OF_ENIMIES of boolean
var XT, YT : array 1 .. NUM_OF_TREES of int
var Length : real4
var BulletNum : nat
var EMPLaunch : boolean
var PlayAgain : string (1)
var Health : int
var HealthC : nat1
var ShootCount : nat1
var XBoom, YBoom : int
var Font1 : int := Font.New ("Arial:20")
var Font2 : int := Font.New ("Arial:30")
var Font3 : int := Font.New ("Agencey FB:15")
var Score : int

var IntToStr : string (10)
loop
    NumOfEMPs := EMP_COUNT
    EMPLaunch := false
    % random tree placement

    for Index : 1 .. NUM_OF_TREES
        randint (XT (Index), 1, maxx)
        randint (YT (Index), 1, 20)
    end for

    % random cloud placement
    for Index : 1 .. NUM_OF_CLOUDS
        randint (XC (Index), 1, maxx)
        randint (YC (Index), maxy - 200, maxy)
        XCT (Index) := XC (Index)
    end for

    % bullet starts
    for Index : 1 .. NUM_OF_BULLETS
        XB (Index) := -10
        Shot (Index) := false
        YB (Index) := -10
    end for

    % enemy stats

    for Index : 1 .. NUM_OF_ENIMIES
        randint (XE (Index), maxx, maxx + 200)
        randint (YE (Index), 60, maxy - 60)
        ShotE (Index) := false
        Hit (Index) := false
        ReloadCount (Index) := 0
    end for

    XP := 100
    YP := 100
    Angle := 0
    Health := 100
    ShootCount := 0
    BulletNum := 1
    Shoot := false
    HealthC := 10
    Score := 0
    colorback (7)
    cls
    Font.Draw ("Helicopter Escape", 100, 300, Font2, 10)
    Font.Draw ("made by Peter Watt", 100, 200, Font1, 10)
    Font.Draw ("(press any key to continue)", 100, 100, Font1, 10)
    View.Update
    getch (Choice)
    color (0)
    cls
    put ""
    put "Controls"
    put "Movement : arrow keys"
    put "Shoot    : space bar"
    put "pause    : enter"
    put ""
    put "The objective is to escape the enemey's un-maned drone defence grid."
    put "Destroy ", VICTORY_COUNT, " enemies to escape"
    put "You are flying in a heavy machine gun transport chopper"
    put "You also have an EMP device onboard to destory all enemies in a small radius"
    put "Use the w key to launc an EMP"
    View.Update
    getch (Choice)
    colorback (54)
    loop
        cls
        Input.KeyDown (Key)
        if Key (KEY_UP_ARROW) then
            if YP > maxy - 35 then
                YP := YP - HELI_SPEED
            else
                YP := YP + HELI_SPEED
            end if
        elsif Key (KEY_DOWN_ARROW) then
            YP := YP - HELI_SPEED
        end if
        if Key (KEY_RIGHT_ARROW) and XP < maxx - 20 then
            XP := XP + 1
        elsif Key (KEY_LEFT_ARROW) and XP > 20 then
            XP := XP - 1
        end if


        % crash and burn
        if Health <= 0 then
            YP := YP - HELI_SPEED * 2
        end if

        if Angle > 360 then
            Angle := 0
        else
            Angle := Angle + ROTAR_SPEED
        end if

        ShootCount := ShootCount + 1

        if Key (' ') and ShootCount > FIRE_RATE then
            ShootCount := 0
            Shot (BulletNum) := true
            XB (BulletNum) := XP
            YB (BulletNum) := YP
            Shot (BulletNum) := false
            fork Sound_Effect (100, 50)
            if BulletNum = NUM_OF_BULLETS then
                BulletNum := 1
            else
                BulletNum := BulletNum + 1
            end if
        end if
        if ShootCount > FIRE_RATE then
            ShootCount := 0
        end if
        if Key (KEY_ENTER) = false then
            Shoot := false
        end if
        if Key ('w') and EMPLaunch = false and NumOfEMPs > 0 then
            EMPLaunch := true
            NumOfEMPs := NumOfEMPs - 1
            drawoval (XP, YP, EMP_RAD, EMP_RAD, 12)
            for Index : 1 .. NUM_OF_ENIMIES
                if Length_Find (XP, YP, XE (Index), YE (Index)) < EMP_RAD then
                    drawline (XP, YP, XE (Index), YE (Index), 12)
                    randint (XE (Index), maxx, maxx + 50)
                    randint (YE (Index), 60, maxy - 60)
                    Score := Score + 1
                    delay (10)
                    View.Update
                end if
            end for
        end if
        if Key ('w') = false then
            EMPLaunch := false
        end if
        % display stuff
        % ground
        drawfillbox (1, 20, maxx, 0, 122)
        % sun
        drawfilloval (maxx - 100, maxy - 50, 50, 50, 14)
        % CLOUDS

        for Index : 1 .. NUM_OF_CLOUDS

            XCT (Index) := XCT (Index) - CLOUD_SPEED

            drawfilloval (XCT (Index) div 1, YC (Index), 50, 20, 0)
            if XCT (Index) + 50 < 1 then
                randint (XC (Index), maxx + 50, maxx + 100)
                randint (YC (Index), maxy - 200, maxy)
                XCT (Index) := XC (Index)
            end if
        end for


        % trees
        for Index : 1 .. NUM_OF_TREES
            if XT (Index) + 5 < 1 then
                randint (XT (Index), maxx, maxx + 20)
                randint (YT (Index), 1, 20)
            else
                XT (Index) := XT (Index) - SPEED
            end if
            Draw_Tree (XT (Index), YT (Index))
        end for
        % heli

        Draw_Copter (XP, YP, COPTER_C, Angle)
        % enemies
        for Index : 1 .. NUM_OF_ENIMIES
            XE (Index) := XE (Index) - 2
            Draw_Enemey (XE (Index), YE (Index), 15)
            if YE (Index) > YP - 4 and YE (Index) < YP + 4 and ShotE (Index) = false then
                ShotE (Index) := true
                YBE (Index) := YE (Index)
                XBE (Index) := XE (Index)
            end if

            %collision with helicopter
            if Length_Find (XP, YP, XE (Index), YE (Index)) < 20 then
                if Health <= 0 then
                else
                    Health := Health - 25
                end if
                randint (XE (Index), maxx, maxx + 20)
                randint (YE (Index), 60, maxy - 60)
            end if
            if XE (Index) + 30 < 1 then
                randint (XE (Index), maxx, maxx + 20)
                randint (YE (Index), 60, maxy - 60)
            end if
            % enemy fire
            if ShotE (Index) = true then
                if ReloadCount (Index) > 300 then
                    ShotE (Index) := false
                    Hit (Index) := false
                    ReloadCount (Index) := 0

                end if
                ReloadCount (Index) := ReloadCount (Index) + 1
                XBE (Index) := XBE (Index) - 3
                if Hit (Index) = false then
                    drawfilloval (XBE (Index), YBE (Index), 2, 2, 10)
                    drawline (XBE (Index), YBE (Index), XBE (Index) + 15, YBE (Index), 10)
                    if Length_Find (XP, YP, XBE (Index), YBE (Index)) < 20 then
                        Hit (Index) := true
                        if Health > 0 then
                            Health := Health - 2
                        end if
                    end if

                end if

            end if
        end for

        % bullets
        for Index : 1 .. NUM_OF_BULLETS

            XB (Index) := XB (Index) + SPEED_B
            if Shot (Index) = false then
                drawfilloval (XB (Index), YB (Index), 1, 1, 7)
                for Index2 : 1 .. NUM_OF_ENIMIES
                    if Length_Find (XB (Index), YB (Index), XE (Index2), YE (Index2)) < 10 then
                        Shot (Index) := true
                        randint (XE (Index2), maxx, maxx + 50)
                        randint (YE (Index2), 60, maxy - 60)
                        Score := Score + 1
                    end if
                end for
            end if

            % drawline (XP, YP, XB (Index), YB (Index), 10)


        end for


        % stats
        if Health > 50 and Health < 70 then
            HealthC := 14
        elsif Health < 50 then
            HealthC := 12
        end if
        IntToStr := intstr (Health)

        Font.Draw ("Health: " + IntToStr + "%", 10, maxy - 20, Font1, 8)
        drawfillbox (173, maxy - 25, 478, maxy - 3, 8)
        if Health > 0 then
            drawfillbox (175, maxy - 23, (Health * 3) + 175, maxy - 5, HealthC)
        end if
        drawbox (175, maxy - 23, 475, maxy - 5, 0)
        IntToStr := intstr (Score)

        Font.Draw ("Enemies killed: " + IntToStr, maxx - 250, maxy - 20, Font1, 8)
        IntToStr := intstr (NumOfEMPs)

        Font.Draw ("EMPs left :" + IntToStr, maxx - 250, maxy - 50, Font1, 8)

        IntToStr := intstr (YP)

        Font.Draw ("Alt: " + IntToStr, 10, maxy - 40, Font3, 8)
        exit when YP < 50 or Score > VICTORY_COUNT
        Input.KeyDown (Key)
        if Key (KEY_ENTER) then
            Font.Draw ("Paused", MID_X, MID_Y, Font2, 12)
            Font.Draw ("press any key to continue", MID_X, MID_Y - 50, Font1, 12)
            View.Update
            getch (Choice)
        end if
        View.Update
    end loop

    if YP < 50 then
        for Boom : 1 .. 200
            loop
                randint (XBoom, XP - 50, XP + 60)
                randint (YBoom, YP - 50, YP + 60)

                exit when Length_Find (XBoom, YBoom, XP, YP) < 50
            end loop
            if Boom mod 2 = 1 then

                drawdot (XBoom, YBoom, 12)
            else
                drawdot (XBoom, YBoom, 14)
            end if
        end for
        Font.Draw ("You've crashed!", MID_X, MID_Y, Font2, 12)
        fork Sound_Boom
    else
        Font.Draw ("You win!!", MID_X, MID_Y, Font2, 12)

    end if
    Font.Draw ("Would you like to play again?(y/n)", 100, 50, Font1, 10)
    View.Update
    loop
        getch (Choice)
        exit when Choice = "n" or Choice = "y"
    end loop
    exit when Choice = "n"
end loop
colorback (0)
cls


Sponsor
Sponsor
Sponsor
sponsor
BenLi




PostPosted: Thu Mar 01, 2007 4:30 pm   Post subject: RE:A Helicopter/ shooter game

I realise you're doing the sound to sound like a real heli and machine guns and stuff, but its REALLY annoying...
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  [ 2 Posts ]
Jump to:   


Style:  
Search: