close all;
clear;
screens = Screen('Screens');
screenNumber = max(screens);
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
[window, windowRect] = Screen('OpenWindow', screenNumber, black);
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
ifi = Screen('GetFlipInterval', window);
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);
KbName('UnifyKeyNames');
escapeKey = KbName('ESCAPE');
upKey = KbName('UpArrow');
downKey = KbName('DownArrow');
leftKey = KbName('LeftArrow');
rightKey = KbName('RightArrow');
[xCenter, yCenter] = RectCenter(windowRect);
dim = round(screenYpixels / 6);
baseRect = [0 0 dim dim];
rectColor = [255 0 0];
squareX = xCenter;
squareY = yCenter;
pixelsPerPress = 10;
vbl = Screen('Flip', window);
waitframes = 1;
exitDemo = false;
ListenChar(-1);
while exitDemo == false
[keyIsDown,secs, keyCode] = KbCheck;
if keyCode(escapeKey)
exitDemo = true;
elseif keyCode(leftKey)
squareX = squareX - pixelsPerPress;
elseif keyCode(rightKey)
squareX = squareX + pixelsPerPress;
elseif keyCode(upKey)
squareY = squareY - pixelsPerPress;
elseif keyCode(downKey)
squareY = squareY + pixelsPerPress;
end
if squareX < 0
squareX = 0;
elseif squareX > screenXpixels
squareX = screenXpixels;
end
if squareY < 0
squareY = 0;
elseif squareY > screenYpixels
squareY = screenYpixels;
end
centeredRect = CenterRectOnPointd(baseRect, squareX, squareY);
Screen('FillRect', window, rectColor, centeredRect);
vbl = Screen('Flip', window, vbl + (waitframes - 0.5) * ifi);
end
ListenChar(1);
sca;