All pastes #2068698 Raw Edit

Something

public text v1 · immutable
#2068698 ·published 2011-05-24 23:53 UTC
rendered paste body
const MAX_PLANE := 100  % maximum number of planes we can keep track of at once
const MAX_BULLET := 13
var ch : char % user input
var gun : int := 5 % gun position: 1 is lowest 9 is highest
var shoot : boolean := false % has gun been fired?
var planeX, planeY : array 1 .. MAX_PLANE of int % arrays to keep track of x and y coordinates of planes
var planeCount : int := 1  % used to keep track of where in arrays to store current plane - when it gets
% past 100 go back to 1 and start again
var planeAlive : array 1 .. MAX_PLANE of boolean  % used to keep track of which of the 100 possible planes
% are actually active
var spawn : int := 1  % keeps track of how many attempted plane spawnings have happened since last plane
% actually spawned
var hit, miss : int := 0 % usedto keep score of number of planes hit and # escaped
var speed : int := 1  % used to control speed of spawing of airplanes
var loaded : boolean := true  % is gun ready to be fired?
var bulletalive : array 1 .. MAX_BULLET of boolean
var test : int := 0
var count : int := 0 % bullet count
var guntype : int := 1  % gun type used
var bomx, bomy : array 1 .. 1000 of int
var bomcount : int := 1



% You will complete this procedure including the comments - it should display
% number of destroyed and escaped planes near the top centre of the screen

procedure updateScore (hit : int, miss : int)
    locate (1, 13)
    put "Destroyed: ", hit, "  Escaped:  ", miss
end updateScore

% You will complete this process including the comments - it should show a plane
% exploding.  X and Y are the coordinates of the bottom left corner of the plane

process explode (x : int, y : int)
    var b : int := 10
    if x > 20 and y > 20 then
        for i : 1 .. 5
            drawfillbox (0, 0, 20, 20, white)
            drawfilloval (x + 10, y + 10, b, b, red)
            drawfilloval (x + 10, y + 10, b - 3, b - 3, white)
            delay (100)
            b := b + 3
        end for
    end if
    drawfilloval (x + 10, y + 10, b, b, white)
end explode
%*******  checkCollision  **********************************************
% Purpose: To see if bullet hit the plane (called each time a bullet   *
%          moves)                                                      *
% Parameters: 1) the x coordinate of the bullet                        *
%             2) the y coordinate of the bullet                        *
% Note: You need to complete part of this procedure                    *
%***********************************************************************
procedure checkCollision (x : int, y : int, count : int)
    % check all possible planes
    test := planeX (1)
    for i : 1 .. MAX_PLANE
        % see if current plane is alive
        if planeAlive (i) = true then
            % check if bullet hit plane
            if planeX (i) > 30 and planeY (i) > 30 then
                if x >= planeX (i) and x <= planeX (i) + 20 and y >= planeY (i) and y <= planeY (i) + 20 then
                    % plane was hit; kill it
                    planeAlive (i) := false
                    if guntype = 1 then
                        bulletalive (count) := false
                    end if
                    %********************  Your code starts
                    %update hit count and scoreboard
                    hit := hit + 1
                    updateScore (hit, miss)
                    %********************  Your code stops
                    % blow up plane
                    fork explode (planeX (i), planeY (i))
                end if
            end if
        end if
    end for
end checkCollision
process reload (x : int, bigX : int)
    if x = (bigX div 10) then
        loaded := true
    end if
end reload
%*******  bullet  ******************************************************
% Purpose: This process is called each time a new bullet is forked     *
% Parameters: 1) controls the width of the parabolic path this bullet  *
%                will follow                                           *
%             2) the y coordinate of the vertex of the parabola        *
%             3) the largest value of x to use when calculating the    *
%                bullet's path                                         *
% Note: You need to complete part of this procedure                    *
%***********************************************************************
process bullet (a : real, k : int, bigX : int, count : int, btypex : int, btypey : int)
    var h : real     % x coordinate of the vertex of the parabola
    var y : int     % current y coordinate of the bullet
    bulletalive (count) := true
    if loaded = true then
        loaded := false
    end if
    %********************  Your code starts
    % - Check if bullet collided with a plane (call procedure)
    % - If the bullet (x) has travelled 1/10 of its path (bigX) allow the
    % gun to be reloaded
    % - draw a bullet at location (x,y)
    % - pause an appropriate amount of time
    % -erase the bullet
    for x2 : 1 .. bigX
        fork reload (x2, bigX)
        h := sqrt (-k / a)
        y := round (a * (x2 - h) ** 2 + k)
        % if guntype = 1 then
        if bulletalive (count) = true then
            %  end if
            drawfilloval (x2, y, btypex, btypey, black)
            checkCollision (x2, y, count)
            delay (3)
            drawfilloval (x2, y, btypex, btypey, white)
        end if
    end for
    %********************  Your code stops
end bullet
process bullet2 (a : real, k : int, bigX : int, count : int, btypex : int, btypey : int)
    var h : real     % x coordinate of the vertex of the parabola
    var y : int     % current y coordinate of the bullet
    if loaded = true then
        loaded := false
        for x : 1 .. bigX
            h := sqrt (-k / a)
            y := round (a * (x - h) ** 2 + k)
            %********************  Your code starts
            % - Check if bullet collided with a plane (call procedure)
            % - If the bullet (x) has travelled 1/10 of its path (bigX) allow the
            % gun to be reloaded
            % - draw a bullet at location (x,y)
            % - pause an appropriate amount of time
            % -erase the bullet
            if x = (bigX div 10) then
                loaded := true
            end if
            drawfilloval (x, y, btypex, btypey, black)
            checkCollision (x, y, count)
            delay (5)
            drawfilloval (x, y, btypex, btypey, white)
            %********************  Your code stops
        end for
    end if
end bullet2
%*******  drawPlane  ***************************************************
% Purpose: To draw or erase the plane                                  *
% Parameters: 1) the x coordinate of top right of the plane            *
%             2) the y coordinate of top right of the plane            *
%             3) the colour to draw the plane in                       *
%***********************************************************************
procedure drawPlane (x, y, c : int)
    drawfillbox (x, y, x + 20, y + 20, c)
    /* drawline (x, y, x + 14, y, c)
     drawline (x + 14, y, x + 14, y - 3, c)
     drawline (x + 16, y, x + 16, y - 3, c)
     drawline (x, y - 3, x + 30, y - 3, c)
     drawline (x, y - 2, x + 30, y - 2, c)
     drawline (x + 16, y, x + 30, y + 7, c)
     drawline (x + 30, y + 7, x + 38, y + 4, c)
     drawline (x + 38, y + 4, x + 38, y + 13, c)
     drawline (x + 38, y + 13, x + 8, y + 13, c)
     drawline (x + 8, y + 13, x, y + 5, c)
     drawline (x, y + 5, x, y, c)
     drawline (x + 7, y + 5, x + 13, y + 5, c)
     drawline (x + 13, y + 5, x + 13, y + 9, c)
     drawline (x + 13, y + 9, x + 10, y + 9, c)
     drawline (x + 10, y + 9, x + 7, y + 5, c)
     drawfill (x + 1, y + 1, black, black)
     drawfill (x + 9, y + 6, 52, black)
     delay (10)
     drawline (x, y, x + 14, y, white)
     drawline (x + 14, y, x + 14, y - 3, white)
     drawline (x + 16, y, x + 16, y - 3, white)
     drawline (x, y - 3, x + 30, y - 3, white)
     drawline (x, y - 2, x + 30, y - 2, white)
     drawline (x + 16, y, x + 30, y + 7, white)
     drawline (x + 30, y + 7, x + 38, y + 4, white)
     drawline (x + 38, y + 4, x + 38, y + 13, white)
     drawline (x + 38, y + 13, x + 8, y + 13, white)
     drawline (x + 8, y + 13, x, y + 5, white)
     drawline (x, y + 5, x, y, white)
     drawline (x + 7, y + 5, x + 13, y + 5, white)
     drawline (x + 13, y + 5, x + 13, y + 9, white)
     drawline (x + 13, y + 9, x + 10, y + 9, white)
     drawline (x + 10, y + 9, x + 7, y + 5, white)
     drawfill (x + 1, y + 1, black, white)
     drawfill (x + 9, y + 6, 52, white)
     x := x + 1
     */
end drawPlane

process bombs (bomcount : int, count : int)
    bomx (bomcount) := planeX (count)
    bomy (bomcount) := planeY (count)
    for i : 1 .. maxint
        if planeAlive (count) = true and bomy(bomcount) > 1 then
            drawfilloval (bomx (bomcount), bomy (bomcount), 4, 4, red)
            delay (10)
            drawfilloval (bomx (bomcount), bomy (bomcount), 4, 4, white)
            bomy (bomcount) := bomy (bomcount) - 1
        end if
    end for
    %for i : 1 .. buildings
    %    exit when bomx (bomcount) => buildx (i) and bomy (bomcount) => buildy (i) and bomx (bomcount) <= buildx (i) + 20 and bomy (bomcount) <= buildy (i) + 40
    %end for
    %exit when bomy (bomcount) = 0
    % exit when planeAlive (count) = false

end bombs
% You will complete this process including the comments - each time a
% new plane is made this process is forked - it controls all aspects of the
% plane including if it escaped -  If you are having trouble with this process
% see me and I will give you some more code and comments to help out
process plane (height : int, count : int)
    var x : int := maxx - 21     % x coordinate of the plane
    var bomb : int := Rand.Int (1, 80)     %tells when bomb will drop
    planeAlive (count) := true
    loop
        if x = 1 then
            miss := miss + 1
            updateScore (hit, miss)
            planeAlive (count) := false
        end if
        drawPlane (x, height, black)
        delay (100)
        drawPlane (x, height, white)
        exit when planeAlive (count) = false
        x := x - 1
        planeX (count) := x
        planeY (count) := height
        bomb := bomb + 1
        if bomb = 100 then
            bomb := 0
            bomcount := bomcount + 1
            fork bombs (bomcount, count)
        end if


    end loop
end plane
% You will complete this procedure including the comments - it will
% see if there is any input, adjust the gun position and erase the gun
% highest gun position is 9 and lowest is 1

procedure getInput (var c : char, var gun : int, var shoot : boolean)
    if hasch then
        c := getchar
        if c = 'x' and gun > 1 then
            gun := gun - 1
            drawfillbox (0, 0, 60, 60, white)
        elsif c = 'z' and gun < 9 then
            gun := gun + 1
            drawfillbox (0, 0, 60, 60, white)
        elsif c = ' ' and loaded = true then
            shoot := true
        elsif c = '1' then
            guntype := 1
        elsif c = '2' then
            guntype := 2
        end if
    end if
end getInput



%*******  drawGun  *****************************************************
% Purpose: To draw the gun and see if a bullet is to be fired          *
% Parameters: 1) the new position of the gun                           *
%             2) whether the gun is ready to be fired                  *
% Note: you do not need to modify this procedure unless you want to    *
%       change how the gun looks or where the bullet goes              *
%***********************************************************************
procedure drawGun (var gun : int, var shoot : boolean)
    if count = 13 then
        count := 0
    end if
    case gun of
        label 1 :
            drawline (0, 0, 60, 10, black)
            if shoot then
                shoot := false
                count := count + 1
                fork bullet (-0.00002, 380, 805, count, 4, 4)
            end if
        label 2 :
            drawline (0, 0, 55, 17, black)
            if shoot then
                shoot := false
                count := count + 1
                fork bullet (-0.00008, 380, 805, count, 4, 4)
            end if
        label 3 :
            drawline (0, 0, 51, 26, black)
            if shoot then
                shoot := false
                count := count + 1
                fork bullet (-0.00015, 380, 805, count, 4, 4)
            end if
        label 4 :
            drawline (0, 0, 45, 35, black)
            if shoot then
                shoot := false
                count := count + 1
                fork bullet (-0.0004, 380, 805, count, 4, 4)
            end if
        label 5 :
            drawline (0, 0, 39, 40, black)
            if shoot then
                shoot := false
                count := count + 1
                fork bullet (-0.0008, 380, 805, count, 4, 4)
            end if
        label 6 :
            drawline (0, 0, 33, 46, black)
            if shoot then
                shoot := false
                count := count + 1
                fork bullet (-0.0015, 380, 805, count, 4, 4)
            end if
        label 7 :
            drawline (0, 0, 27, 50, black)
            if shoot then
                shoot := false
                count := count + 1
                fork bullet (-0.0025, 380, 805, count, 4, 4)
            end if
        label 8 :
            drawline (0, 0, 21, 54, black)
            if shoot then
                shoot := false
                count := count + 1
                fork bullet (-0.005, 380, 570, count, 4, 4)
            end if
        label 9 :
            drawline (0, 0, 15, 58, black)
            if shoot then
                shoot := false
                count := count + 1
                fork bullet (-0.01, 380, 403, count, 4, 4)
            end if
        label 10 :
            drawline (0, 0, 15, 58, black)
            gun := 9
        label 0 :
            drawline (0, 0, 60, 10, black)
            gun := 1
    end case
end drawGun
procedure drawsniper (var gun : int, var shoot : boolean)
    if count = 13 then
        count := 0
    end if
    case gun of
        label 1 :
            drawline (0, 0, 60, 10, black)
            if shoot then
                shoot := false
                fork bullet2 (-0.00002, 380, 805, count, 7, 3)
            end if
        label 2 :
            drawline (0, 0, 55, 17, black)
            if shoot then
                shoot := false
                fork bullet2 (-0.00008, 380, 805, count, 7, 3)
            end if
        label 3 :
            drawline (0, 0, 51, 26, black)
            if shoot then
                shoot := false
                fork bullet2 (-0.00015, 380, 805, count, 7, 3)
            end if
        label 4 :
            drawline (0, 0, 45, 35, black)
            if shoot then
                shoot := false
                fork bullet2 (-0.0004, 380, 805, count, 7, 3)
            end if
        label 5 :
            drawline (0, 0, 39, 40, black)
            if shoot then
                shoot := false
                fork bullet2 (-0.0008, 380, 805, count, 7, 3)
            end if
        label 6 :
            drawline (0, 0, 33, 46, black)
            if shoot then
                shoot := false
                fork bullet2 (-0.0015, 380, 805, count, 7, 3)
            end if
        label 7 :
            drawline (0, 0, 27, 50, black)
            if shoot then
                shoot := false
                fork bullet2 (-0.0025, 380, 805, count, 7, 3)
            end if
        label 8 :
            drawline (0, 0, 21, 54, black)
            if shoot then
                shoot := false
                fork bullet2 (-0.005, 380, 570, count, 7, 3)
            end if
        label 9 :
            drawline (0, 0, 15, 58, black)
            if shoot then
                shoot := false
                fork bullet2 (-0.01, 380, 403, count, 7, 3)
            end if
        label 10 :
            drawline (0, 0, 15, 58, black)
            gun := 9
        label 0 :
            drawline (0, 0, 60, 10, black)
            gun := 1
    end case
end drawsniper
%*******  spawnPlan  ***************************************************
% Purpose: To spawn planes to be shot down, they get spawned faster    *
%          the longer the game goes on                                 *
% Parameters: 1) used to determine if it is time to spawn another plane*
%             2) controls how fast the planes are spawned              *
%             3) helps keep track of which planes are active           *
% Note: you do not need to modify this procedure unless you want to    *
%       change how quickly planes are spawned or where they are spawned*
%***********************************************************************
procedure spawnPlane (var spawn, speed, planeCount : int)
    var yy : int
    spawn := spawn + 1
    if spawn > 50000 div (speed div 50 + 1) then
        spawn := 0
        speed := speed + 1
        randint (yy, 100, 340)
        fork plane (yy, planeCount)
        planeCount := planeCount + 1
        if planeCount > MAX_PLANE then
            planeCount := 1
        end if
    end if
end spawnPlane

% main program
for i : 1 .. MAX_PLANE
    planeAlive (i) := false
    planeX (i) := 0
    planeY (i) := 0
end for
updateScore (hit, miss)
loop
    if guntype = 1 then
        getInput (ch, gun, shoot)
        drawGun (gun, shoot)
        spawnPlane (spawn, speed, planeCount)
    elsif guntype = 2 then
        getInput (ch, gun, shoot)
        drawsniper (gun, shoot)
        spawnPlane (spawn, speed, planeCount)
    end if
end loop