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

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

% current_cursor speed
cursor_speed = 4;

% current cursor postion
x_cursor = x_mid;
y_cursor = y_mid;

% game loop
while 1
    
    [keyIsDown, secs, keyCode] = KbCheck;
    if keyCode(KbName('space')) == 1
        break;
    end
    [x y buttons] = GetMouse;
    if x > x_mid + 100   % note the 100 here is to create a center region where nothing is happening
        x_cursor = x_cursor + cursor_speed;
    end
    if x < x_mid - 100
        x_cursor = x_cursor - cursor_speed;
    end    
    if y > y_mid + 100
        y_cursor = y_cursor + cursor_speed;
    end
    if y < y_mid - 100
        y_cursor = y_cursor - cursor_speed;
    end
    
    % draw a green circle wherever the cursor is
    Screen('FillOval', window , [0 255 0], [x_cursor-5 y_cursor-5 x_cursor+5 y_cursor+5]);

    % toggle the Flip command with this one: Screen('Flip', window); to get
    % rid of the trace
    Screen('Flip', window,0,1);
    
    % just to slow it down!
    WaitSecs(0.05);
    
end

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