clc;
clear all;
close all;
rng('shuffle');
ListenChar(2);
HideCursor();

% open a drawing window
Screen('Preference', 'SkipSyncTests', 1); 
[window, window_size] = Screen('OpenWindow', 0, [204 255 255], [],32,2);
x_mid = window_size(3)/2;
y_mid = window_size(4)/2;

% text_colour
text_colour = [0 0 0];

% grass colour
grass_colour = [0 153 0];

% missle colour
mis_col = [96 96 96];

% current_cursor speed
mis_speed = 5;

% current cursor postion
x_mis = x_mid;
y_mis = y_mid+300;

% define cursor size
x_size = 3;
y_size = 7;

% the boom
exp_start = 1;
exp_end = 100;

Screen(window,'TextFont','Arial');
Screen(window,'TextSize',40);
DrawFormattedText(window, ['READY TO LAUNCH'],x_mid-200, 'center', text_colour,[],[],[],2);
% draw a green circle wherever the cursor is
Screen('FillRect', window , grass_colour, [window_size(1) y_mid+300 window_size(3) window_size(4)]);
Screen('FillRect', window , mis_col, [x_mis-x_size y_mis-y_size x_mis+x_size y_mis+y_size]);
Screen('Flip',window);

% game loop
while 1
    
    [keyIsDown, secs, keyCode] = KbCheck;
    if keyCode(KbName('space')) == 1
        break;
    end
    
end

WaitSecs(1);

Screen(window,'TextFont','Arial');
Screen(window,'TextSize',40);
DrawFormattedText(window, ['5'],'center', 'center', text_colour,[],[],[],2);
Screen('FillRect', window , grass_colour, [window_size(1) y_mid+300 window_size(3) window_size(4)]);
Screen('FillRect', window , mis_col, [x_mis-x_size y_mis-y_size x_mis+x_size y_mis+y_size]);
Screen('Flip',window);

WaitSecs(1);

Screen(window,'TextFont','Arial');
Screen(window,'TextSize',40);
DrawFormattedText(window, ['4'],'center', 'center', text_colour,[],[],[],2);
Screen('FillRect', window , grass_colour, [window_size(1) y_mid+300 window_size(3) window_size(4)]);
Screen('FillRect', window , mis_col, [x_mis-x_size y_mis-y_size x_mis+x_size y_mis+y_size]);
Screen('FillRect', window , [255 0 0], [x_mis-x_size y_mis+y_size-1 x_mis+x_size y_mis+y_size]);
Screen('Flip',window);

WaitSecs(1);

Screen(window,'TextFont','Arial');
Screen(window,'TextSize',40);
DrawFormattedText(window, ['3'],'center', 'center', text_colour,[],[],[],2);
Screen('FillRect', window , grass_colour, [window_size(1) y_mid+300 window_size(3) window_size(4)]);
Screen('FillRect', window , mis_col, [x_mis-x_size y_mis-y_size x_mis+x_size y_mis+y_size]);
Screen('FillRect', window , [255 0 0], [x_mis-x_size-1 y_mis+y_size-2 x_mis+x_size+1 y_mis+y_size]);
Screen('Flip',window);

WaitSecs(1);

Screen(window,'TextFont','Arial');
Screen(window,'TextSize',40);
DrawFormattedText(window, ['2'],'center', 'center', text_colour,[],[],[],2);
Screen('FillRect', window , grass_colour, [window_size(1) y_mid+300 window_size(3) window_size(4)]);
Screen('FillRect', window , mis_col, [x_mis-x_size y_mis-y_size x_mis+x_size y_mis+y_size]);
Screen('FillRect', window , [255 0 0], [x_mis-x_size-1 y_mis+y_size-2 x_mis+x_size+1 y_mis+y_size+2]);
Screen('Flip',window);

WaitSecs(1);

Screen(window,'TextFont','Arial');
Screen(window,'TextSize',40);
DrawFormattedText(window, ['1'],'center', 'center', text_colour,[],[],[],2);
Screen('FillRect', window , grass_colour, [window_size(1) y_mid+300 window_size(3) window_size(4)]);
Screen('FillRect', window , mis_col, [x_mis-x_size y_mis-y_size x_mis+x_size y_mis+y_size]);
Screen('FillRect', window , [255 0 0], [x_mis-x_size-1 y_mis+y_size-2 x_mis+x_size+1 y_mis+y_size+3]);
Screen('Flip',window);

WaitSecs(1);

while 1
    
    [x y buttons] = GetMouse;
    y_mis = y_mis - mis_speed;
    
    Screen('FillRect', window , grass_colour, [window_size(1) y_mid+300 window_size(3) window_size(4)]);
    Screen('FillRect', window , mis_col, [x_mis-x_size y_mis-y_size x_mis+x_size y_mis+y_size]);
    Screen('FillRect', window , [255 0 0], [x_mis-x_size-1 y_mis+y_size-2 x_mis+x_size+1 y_mis+y_size+3]);
    Screen('Flip',window);
    
    % just to slow it down!
    %WaitSecs(0.03);
    
    if y_mis < y_mid - 200
        break
    end
    
end

for counter = 1:100
    Screen('FillRect', window , grass_colour, [window_size(1) y_mid+300 window_size(3) window_size(4)]);
    Screen('FillRect', window , mis_col, [x_mis-x_size y_mis-y_size x_mis+x_size y_mis+y_size]);
    Screen('FillRect', window , [255 0 0], [x_mis-x_size-1 y_mis+y_size-2 x_mis+x_size+1 y_mis+y_size+3]);
    Screen('FillOval', window , [255 0 0], [x_mis-exp_start y_mis-exp_start x_mis+exp_start y_mis+exp_start]);
    if counter > 30
        Screen('FillOval', window , [255 100 0], [x_mis+50-exp_start y_mis-exp_start x_mis+50+exp_start y_mis+exp_start]);
    end
    if counter > 50
        Screen('FillOval', window , [255 200 0], [x_mis-40-exp_start y_mis-40-exp_start x_mis-40+exp_start y_mis-40+exp_start]);
    end
    exp_start = exp_start + 1;
    Screen('Flip',window);
end

WaitSecs(3);

Screen('CloseAll');
ListenChar(0);
ShowCursor();