
-----------------------------------
Smarties102
Thu Jun 16, 2011 6:07 pm

SIMPLE Space Invaders Game HELP??
-----------------------------------
What is it you are trying to achieve?
For grade 10 computer science I need to create a game of space invaders.


What is the problem you are having?
I don't know how to code to return to the main menu after the aliens touch the ground because the variable is stored as an array of int.


Describe what you have tried to solve this problem
I've tried if statements and using like when yEnemies  0
assert buttonFont > 0
assert otherTitleFont > 0
assert mainTextFont > 0
assert player > 0

% Scales the pics
for i : 1 .. 5
    assert enemiesPics (i) > 0
    enemiesPics (i) := Pic.Scale (enemiesPics (i), 50, 50)
end for

% Checks to see if the mouse is hovering over a button
procedure ButtonCheck
    loop
        Mouse.Where (x, y, button)
        Font.Draw ("back", 435, 130, buttonFont, brightwhite)
        if x > 383 and x < 616 and y > 100 and y < 200 then
            Font.Draw ("back", 435, 130, buttonFont, brightblue)
            View.Update
        else
            Font.Draw ("back", 435, 130, buttonFont, blue)
            View.Update
        end if

        exit when x > 383 and x < 616 and y > 100 and y < 200 and button = 1
    end loop
end ButtonCheck

% Sets the basic layout for a level
procedure SetLevel
    drawfill (0, 0, black, brightgreen)

    counter := 1
    % Draws the enemies only if they have not been destroyed
    for rows : 1 .. level + 1
        exit when rows = 9
        for xPosition : 1 .. 641 by 70
            if enemies (xPosition div 70 + 1) = true then
                Pic.Draw (enemiesPics (rows), xPosition, 700 - (rows * 70), picMerge)
                xEnemies (counter) := xPosition
                xEnemiesOriginal (counter) := xPosition
                yEnemies (counter) := 700 - (rows * 70)
            end if
            counter += 1
        end for
    end for

    % Draws the player
    Pic.Draw (player, xPlayer, 0, picMerge)
    View.Update
end SetLevel

% Used to move enemies
procedure UpdateLevel
    drawfill (0, 0, black, brightgreen)
    for i : 1 .. numEnemies
        % Moves the enemies sideways
        if direction mod 2 = 0 then
            xEnemies (i) += 1
        else
            xEnemies (i) -= 1
        end if

        % Moves the enemies down 70 pixels if the right/leftmost enemy hits the right/left side of the screen
        if direction mod 2 = 0 then
            for decreasing j : numEnemies .. 1
                if xEnemies (j) >= maxx - 50 and enemies (j) = true then
                    for k : 1 .. numEnemies
                        yEnemies (k) -= 70
                        exit when yEnemies (j)  4
        direction := 0
        directionChange := true
        numEnemies := (level + 1) * 10
        for i : 1 .. numEnemies
            enemies (i) := true
        end for
        numDead := 0
        cls

        % Draws the player
        drawfill (0, 0, black, brightgreen)
        xPlayer := maxx div 2 - 75
        Pic.Draw (player, xPlayer, 0, picMerge)

        % Draws the level start message
        Font.Draw ("Level " + intstr (level) + " Start", 225, maxy div 2, otherTitleFont, white)
        Font.Draw ("Press any key to start", 360, maxy div 2 - 30, mainTextFont, white)

        View.Update
        Input.Pause

        % Sets up the level
        cls
        SetLevel

        % Gets user input for the controls
        loop
            Input.KeyDown (keys)

            if keys (KEY_LEFT_ARROW) then
                xPlayer -= 5

                % Ensures that the player does not go off the screen
                if xPlayer = maxx - 150 then
                    xPlayer := maxx - 150
                end if

                % Updates the player
                drawfillbox (xPlayer, 0, xPlayer + 150, 100, black)
                Pic.Draw (player, xPlayer, 0, picMerge)
                View.Update
            end if

            % Shoots
            if keys (' ') and shot = false then
                shot := true
                xShot := xPlayer + 75
                yShot := 101
                drawfilloval (xShot, yShot, 5, 5, white)
                View.Update
            end if

            % If the shot is on screen, then moves the shot
            if yShot > 100 and yShot < maxy + 5 and shot = true then
                yShot += 12
                drawfilloval (xShot, yShot, 5, 5, white)
                View.Update
            else
                shot := false
            end if

            % Checks if there is a collision
            if shot = true then
                for i : 1 .. numEnemies
                    if Math.Distance (xShot, yShot, xEnemies (i) + 25, yEnemies (i) + 25)  383 and x < 616 and y > 100 and y < 200 then
            Font.Draw ("back", 435, 130, buttonFont, brightblue)
            View.Update
        else
            Font.Draw ("back", 435, 130, buttonFont, blue)
            View.Update
        end if
        exit when x > 383 and x < 616 and y > 100 and y < 200 and button = 1
    end loop

    cls
end Instructions

% Starts the appropriate procedure
loop
    Title

    % Initializes variables used for the title animation
    xShot := 699
    titleShot := true
    titleEnemy := true

    delay (200)
    loop
        % Draws a simple title animation
        Pic.Draw (playerTitle, 700, maxy div 2 - 100, picMerge)
        if titleEnemy = true then
            Pic.Draw (enemyTitle, 100, maxy div 2 - 60, picMerge)
        end if
        if xShot < 700 and titleShot = true then
            drawfilloval (xShot + 2, maxy div 2 + 50, 10, 10, black)
            drawfilloval (xShot, maxy div 2 + 50, 10, 10, white)
            xShot -= 2
        end if
        if titleEnemy = true then
            if Math.Distance (xShot, maxy div 2 + 50, 200, maxy div 2 + 50)  50 and x < 283 and y > 100 and y < 200 and button = 1 then
            PlayGame
            exit
        elsif x > 383 and x < 616 and y > 100 and y < 200 and button = 1 then
            Instructions
            exit
        elsif x > 716 and x < 949 and y > 100 and y < 200 and button = 1 then
            exit
        elsif x > 50 and x < 283 and y > 100 and y < 200 then
            Font.Draw ("start", 84, 130, buttonFont, brightwhite)
            Font.Draw ("start", 84, 130, buttonFont, brightblue)
            View.Update
        elsif x > 383 and x < 616 and y > 100 and y < 200 then
            Font.Draw ("help", 435, 130, buttonFont, brightwhite)
            Font.Draw ("help", 435, 130, buttonFont, brightblue)
            View.Update
        elsif x > 716 and x < 949 and y > 100 and y < 200 then
            Font.Draw ("quit", 765, 130, buttonFont, brightwhite)
            Font.Draw ("quit", 765, 130, buttonFont, brightblue)
            View.Update
        else
            Font.Draw ("start", 84, 130, buttonFont, brightwhite)
            Font.Draw ("start", 84, 130, buttonFont, blue)
            Font.Draw ("help", 435, 130, buttonFont, brightwhite)
            Font.Draw ("help", 435, 130, buttonFont, blue)
            Font.Draw ("quit", 765, 130, buttonFont, brightwhite)
            Font.Draw ("quit", 765, 130, buttonFont, blue)
            View.Update
        end if
    end loop
    exit when x > 716 and x < 949 and y > 100 and y < 200
end loop
