Contents

function  Test_Background_me ()

PARAMETERS

% matrix of plotted images
Matpl = zeros(5,9);

% Total time of game
T = 0;

% plotting sun
suncounter = zeros(5,9); % checks for sunflower, then how long until a sun pops up in that spot
SUNS = gobjects(5,9); % where the suns themselves are placed

% collecting sun
Matsun = zeros(5,9); % numerical indication of where a sun will be
Scounter = 200; %out initial value of sun

% shooting peas
V = [15 0]; % the velocity of the pea [x y]
dt = 0.01; %change in time when updating the velocity/jumps in time of the code
peacounter = zeros(1,45); % counts the seconds between each pea
peac = 0; % counts how many peas we have
peacent = [0; 0]; % initialization of x and y center positions per pea

% generating zombie
ZT = 0; % time between the death of the first zombie and spawning of next zombie
ZTL = 0; % row zombie will pop up in
zombiehealth = 5; % initial zombie health
zomber = gobjects(1,1); %initializaion of the zombie as an image
zombcent = [0; 0]; % vector of x and y center positions of zombies
VZ = [-0.5 0]; % velocity of zombie


% UI state
quitState = false; % when true, the game is quit
started = false; %the game hasn't been started yet

GRAPHICS INITIALIZATION

Create figure for background

H = figure(1);
hold on

% function to plot the field
plotfield;

% plot seed store
[SFS,PS] = plotseedstore;

title('q = quit, m = start')
% Enlarge figure to full screen.
%set(gcf, 'units','normalized','outerposition',[0 1 1 1]);

% Set up callback functions
set (SFS, 'ButtonDownFcn', @SFSCallback); %we attach SFS to the callback that lets you buy them
set (PS, 'ButtonDownFcn', @PSCallback); %PS to the callback that lets you buy that
set (H, 'KeyPressFcn', @KeyboardCallback); %Callback to start, quit, pause, resume

% set suncounter
fill([0.5 0.5 1.5 1.5],[0.5 0.17 0.17 0.5],'w');
txt = text(.87, 0.3,num2str(Scounter),'FontSize', 20);

% set score counter
Warning: PNG library warning: iCCP: extra compressed data. 
Warning: PNG library warning: iCCP: extra compressed data. 
Warning: PNG library warning: iCCP: extra compressed data. 

SIMULATION

while (~quitState)
    % resets sun counter
    txt.String = num2str(Scounter); %this makes it so that the suncounter is updated in real time

    if ~started
        pause (0.01); %jumps of the game, corresponds with dt
        continue
    end

    % check for sunflowers
        % sunflower produce sun every 7 second
            % 7 seconds from when you collect the sun
    [i,j] = find((Matpl == 1)); %if a sunflower has been planted
    for k = 1:length(i) %the amount of sunflowers you have
        if suncounter(i(k),j(k)) >= 5 % 7
            I ='Sun.png'; % sun image
            C = [j(k),i(k)]; % X position of that particular sunflower
            SUNS(i(k),j(k)) = plotimage(I,C); % sun object placement matrix
            Matsun(i(k),j(k)) = 1; % sun placement counter
            set (SUNS(i(k),j(k)), 'ButtonDownFcn', @SunCallback) %collect the sunds
            suncounter(i(k),j(k)) = 0; %reset sun time counter
        end
        suncounter(i(k),j(k)) = suncounter(i(k),j(k)) + 0.01; %updates timer for sun
    end

    % peashooters peas i is rows or y, j is columns or x
    [i,j] = find((Matpl == 2)); % finds the peashooters
    I ='Pea.png';

    for k = 1:length(i) % loops through all the peashooters
        X = [j(k),i(k)]; % intial position of the given peashooter
        % plots the pea

        if peacounter(k) >= 1 % 1.5
            peac = k;% counts how many peas we have
            peacent(:,peac) = [X(1);X(2)]; %logs pea centers
            if peacent(2,peac) > 0
                peacter(peac) = plotimage(I,X);
            end
            peacounter(k) = 0; % reset time counter
            %(must think about later for when peashooters are deleted
        end
        peacounter(k) = peacounter(k) + 0.01; %starts the timer again
    end

    for iter = 1:peac %go through every existing pea
        if peacent(:,iter) ~= 0 %if the center position is established
            if peacent(1,iter) < 9 % checks the x position
                peacent(1,iter) = update_obj(peacent(1,iter),V); %continue forward
            elseif peacent(1,iter) >= 9
                peacent(:,iter) = X; %reset to initial positon
            end
        end
        if (peacent(1,iter) ~= 0) && isgraphics(zomber)
            if peacent(2,iter) == zombcent(2,1) %making sure the zonbie and pea are in the same row
                if comparepz(peacent(1,iter),zombcent(1,1)) %checking to see if they're in the same colun
                    peacent(:,iter) = X;%reset to initial position
                end
            end

        end
    end

    if zombiehealth > 0 && isgraphics(zomber) && zombcent(1,1) >= 0.5
        zombcent(1,1) = update_obj(zombcent(1,1),VZ); %keeps moving if it isn't at the end of the board
    elseif zombiehealth > 0 && isgraphics(zomber) && zombcent(1,1) < 0.5
        title('You Lose') % you lose
        quitState = true; % quits out the game
    elseif zombiehealth == 0 && isgraphics(zomber) % kills the zobie
        delete(zomber)
        ZT = 0; %resets the timer
    end


    % zombies
    if ZT == 0 % if the timer hasn't started
        ZT = T + 10*rand(1); % pick time until zombie
        ZTL = randi(5); % pick a row
    elseif T >= ZT && T <= ZT + 0.01 %(T == ZT)
        Iz ='BZombie.png';
        X = [9,ZTL]; % position of 9 and the row you picked
        %zomber = plotimage(Iz,X); %plot the image right
        zombcent = [X(1);X(2)]; %center positon of zombie is
        zombiehealth = 5; %health starts at 5
    end


    for i = 1:peac
        drawobj(peacter(i),peacent(:,i))
    end
%     drawobj(zombcent,zomber)

    pause (0.01) %puts in pause
    T = T + 0.01; %updates runtime of game
end
Invalid or deleted object.

Error in Test_Background_me (line 67)
    txt.String = num2str(Scounter); %this makes it so that the suncounter is updated in real time

Helper Functions

    function x=update_obj(x,V)
        % Pea only need one update, move.
        x = x + V(1)*dt;
    end

    function col = comparepz(pea,zombie)
        col = false;
        if pea >= zombie - 0.5
            col = true;
            zombiehealth = zombiehealth - 1;
        end
    end

    function drawobj(obj,X)
        if X(1) > 0
            obj.XData = [X(1)-0.5, X(1)+0.5];
        end

    end

%     function plotobj(obj,X)
%         if isgraphics(obj)
%             if jiter == A(2)
%                 obj.XData = [X(1,1)-0.5, X(1,1)+0.5];
%             else
%                 obj.XData = [X(1,jiter)-0.5, X(1,jiter)+0.5];
%             end
%         end
%     end

DRAWING

CALLBACKS

    function SFSCallback(~,~)
        I ='Sunflower.png';

        title('Click on seed to cancel')

        coordinates_input = ginput(1);

        C = round(coordinates_input);

        % check ifvalid
        if (C(2) < 1 || C(2) > 5 || C(1) < 1 || C(1) > 9)
        elseif (Scounter < 50)
            title('Not enough Sun')
        elseif Matpl(C(2),C(1)) == 0
            plotimage(I,C)
            Scounter = Scounter -50;
            Matpl(C(2),C(1)) = 1;
        end
        title('')
    end

    function PSCallback(~,~)
        I ='Peashooter.png';

        title('Click on seed to cancel')

        coordinates_input = ginput(1);

        C = round(coordinates_input);

        % check ifvalid
        if (C(2) < 1 || C(2) > 5 || C(1) < 1 || C(1) > 9)
        elseif (Scounter < 100)
            title('Not enough Sun') % does not work yet
        elseif Matpl(C(2),C(1)) == 0
            plotimage(I,C)
            Scounter = Scounter -100;
            Matpl(C(2),C(1)) = 2;
        end
        title('')
    end
    function KeyboardCallback(~,eventdata)
        % Callback function to handle the keypress events
        switch (eventdata.Character)
            case 'q'
                close(H)
                quitState = true;
            case 'm'
                started = true;
            case 'p'
                title('paused')
                uiwait()
            case 'r'
                title('')
                uiresume()
            otherwise
        end
    end
    function SunCallback(obj,~)
        delete(obj)
        Scounter = Scounter + 25;
    end
end