As promised, here’s the code for my Juggling game.

There are four files that you have to create: ‘main.m’, ‘plot_background.m’, ‘ball.m’, and ‘curs.m’.

Also, unlike the YouTube movie, there are two balls you have to juggle, so you might want to comment out the second ball. 🙂

Index

main.m

close all
clear

% Initialization
mouse_loc = get(0, 'pointerlocation');  % Get the mouse/cursor location
h = figure;                             % Create a figure
walls = [0 500 0 500];                  % Set the walls of the figure
set(gca, 'units', 'pixels')             % Set the units as pixels
count = 0;                              % Hit counter
hi_count = 0;                           % High score counter
time = 1000;                            % Time

% Initialize objects
ball_1 = ball();                        % Create a ball
ball_2 = ball();                        % Create another ball
cursor = curs();                        % Create a cursor object

done = false;                           % Loop while we are not done
while ~done
    
    % Update the cursor's position
    cursor = cursor.update;
    
    % Update the balls' position
    [ball_1 count hi_count] = ...
        ball_1.update(cursor, walls, count, hi_count);
    [ball_2 count hi_count] = ...
        ball_2.update(cursor, walls, count, hi_count);
    
    % Plot objects
    cursor.plot
    hold on
    plot_background
    ball_1.plot
    ball_2.plot
    hold off
    
    % Set figure properties
    axis(walls);
    title({['Hits = ' int2str(count)]; ...
        ['Hi = ' int2str(hi_count)]; ...
        num2str(time)})
    
    % Decrement the time
    time = time - 1;
    
    % Set the stop condition
    if time < 0
        done = true;
    end
    
    % Pause so we can see movement
    pause(0.001)
    
end

% Game over, close the figure
close

% Display your score
disp(['Your Score this game:  ' int2str(hi_count)])

Return to index

Plot_Background

bg1_x = [walls(1) walls(1)+25 walls(1)+25 walls(1) walls(1)];
bg1_y = [walls(4)-25 walls(4)-25 walls(4) walls(4) walls(4)-25];

bg2_x = [walls(2)-25 walls(2) walls(2) walls(2)-25 walls(2)-25];
bg2_y = [walls(4)-25 walls(4)-25 walls(4) walls(4) walls(4)-25];

plot(bg1_x, bg1_y, 'k-')
plot(bg2_x, bg2_y, 'k-')
plot(walls(1:2), [100 100], 'k-')

plot(walls(2)/2, walls(4), 'ko', 'MarkerFaceColor', 'k', 'MarkerSize', 8)

Return to index

ball

classdef ball
    properties
        x_pos
        y_pos
        x_upd
        y_upd
        coll_t
        on
    end
    
    methods
        function b = ball()
            if rand() < 0.5
                b.x_upd = floor(rand()*10);
            else
                b.x_upd = -1*floor(rand()*10);
            end

            if rand() < 0.5
                b.y_upd = floor(rand()*10);
            else
                b.y_upd = -1*floor(rand()*10);
            end

            b.x_pos = 250;
            b.y_pos = 250;
            b.coll_t = 0;
            b.on = true;
        end
        
        function [b count hi_count] = update(b, c, walls, count, hi_count)
            
            b.x_pos = b.x_pos + b.x_upd;
            b.y_pos = b.y_pos + b.y_upd - 0.05*b.coll_t^2;
            b.coll_t = b.coll_t + 1;
            
            % Wall collision
            if (b.x_pos > walls(2)) || (b.x_pos < walls(1))
                
                if b.x_pos < walls(1)
                    b.x_pos = walls(1);
                end
                
                if b.x_pos > walls(2)
                    b.x_pos = walls(2);
                end
                
                b.x_upd = -0.85*b.x_upd;
            end
            
            if (b.y_pos > walls(4)) || (b.y_pos < 100)
                
                if b.y_pos > walls(4)
                    b.y_pos = walls(4);
                end
                
                if b.y_pos < 100
                    b.y_pos = 100;
                    
                    if count > hi_count
                        hi_count = count;
                    end
                    
                    count = 0;
                end
                
                if abs(b.y_upd) > 2
                    b.y_upd = -0.85*b.y_upd;
                else
                    b.y_upd = 0;
                end
                
                b.coll_t = 0;
            end
            
            if b.y_pos == 100
                b.x_upd = b.x_upd*.95;
            end
            
            % Teleport Hax
            if (b.x_pos < walls(1)+25) && (b.y_pos > walls(4)-25)
                b.x_pos = walls(2)/2;
                b.y_pos = walls(4)-5;
            end
            
            if (b.x_pos > walls(2)-25) && (b.y_pos > walls(4)-25)
                b.x_pos = walls(2)/2;
                b.y_pos = walls(4)-5;
            end
            
            % Cursor collision
            x_delt = b.x_pos - c.x_pos;
            y_delt = b.y_pos - c.y_pos;
            dist = sqrt(x_delt^2 + y_delt^2);
            
            if dist < 64
                b.x_upd = x_delt*10/dist;
                b.y_upd = y_delt*10/dist;
                b.coll_t = 0;
                count = count + 1;
            end
            
        end
        
        function b = collision(b, c, walls)
            
            b.x_pos = b.x_pos + b.x_upd;
            b.y_pos = b.y_pos + b.y_upd;
            
            if (b.x_pos > walls(2)) || (b.x_pos < walls(1))
                b.x_upd = -1 * b.x_upd;
            end
            
            if (b.y_pos > walls(4))
                b.y_upd = -1 * b.y_upd;
            end
            
            if (b.y_pos < walls(3))
                b.on = false;
            end
            
            x_delt = b.x_pos - c.x_pos;
            y_delt = b.y_pos - c.y_pos;
            dist = sqrt(x_delt^2 + y_delt^2);
            
            if dist < 25
                b.x_upd = x_delt*10/dist;
                b.y_upd = y_delt*10/dist;
            end
            
        end
        
        function plot(b)
            if b.on
                plot(b.x_pos, b.y_pos, 'ko', 'markerfacecolor', 'y')
            end
        end
    end
end

Return to index

Curs

classdef curs
    properties
        x_pos
        y_pos
    end
    
    methods
        function b = curs()
            
            mouse_loc = get(0, 'pointerlocation');
            fig_pos = get(gcf, 'position');
            
            b.x_pos = mouse_loc(1)-fig_pos(1)-55;
            b.y_pos = mouse_loc(2)-fig_pos(2)+15;
            
        end
        
        function b = update(b)
            
            mouse_loc = get(0, 'pointerlocation');
            fig_pos = get(gcf, 'position');
            
            b.x_pos = mouse_loc(1)-fig_pos(1)-55;
            b.y_pos = mouse_loc(2)-fig_pos(2)+15;
            
            if b.x_pos < 0
                b.x_pos = 0;
            elseif b.x_pos > 500
                b.x_pos = 500;
            end
            
            if b.y_pos > 500
                b.y_pos = 500;
            end
            
        end
        
        function plot(b)
            plot(b.x_pos, b.y_pos, 'ko', 'markerfacecolor', 'r', ...
                'markersize', 64)
        end
    end
end

Return to index

Advertisements