clear all
close

f = figure;                   % creates blank figure
set(f, 'name', 'SuperPong')   % changes name to SuperPong (on the top)
set(f,'numbertitle', 'off')   % turns off 'figure #1' (on the top)
a = axes;                     % creates axis
set(a, 'plotboxaspectratio',[1 1 1])  % ensures round ball
set(a, 'xlim', [0,1])         % effectively normalizes all coordinates in axes
set(a, 'ylim', [0,1])
set(a,'visible','off')        % don't want to see the axes themselves
r = rectangle('position',[0,0,1,1]);   % the outer box
paddlePos = [.4 .05 .2 .05];
paddle = rectangle('position', paddlePos);  % create paddle
set(paddle,'facecolor','b')
ballPos = [.2 .8 .1 .1];
ball = rectangle('position',ballPos);       % create ball
set(ball, 'curvature', [1 1], 'facecolor','r')

isBallIn = @(p) p(1) > 0 && p(2) > 0 && p(1) < .9 && p(2) < .9;
bounceR = @(p) p(1) >= .9;
bounceL = @(p) p(1) <= 0;
bounceT = @(p) p(2) >= .9;
bounceB = @(p) p(2) <= 0;

ballVel = [.01 -.02 0 0];

for ii = 1:200
   ballPos = ballPos + ballVel;
   set(ball,'position', ballPos)
   if bounceB(ballPos) || bounceT(ballPos)
       ballVel(2) = -ballVel(2);
   end
   if bounceL(ballPos) || bounceR(ballPos)
       ballVel(1) = -ballVel(1);
   end
   pause(.05)
end

