Hello, I am a student who has been learning the program for over a month. I have a question because there is a part that I can’t solve while writing the code.
It flips by 1 second after reaching the halfway point before starting the 5-second rest period. What I want is for the break to start as soon as the circle reaches the halfway point.
sca;
close all;
clear;
PsychDefaultSetup(2);
Screen('Preference', 'SkipSyncTests', 0);
screens = Screen('Screens');
screenNumber = max(screens);
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
[w, rect] = PsychImaging('OpenWindow', screenNumber, white);
[screenXpixels, screenYpixels] = Screen('WindowSize', w);
[xCenter, yCenter] = RectCenter(rect);
Screen('BlendFunction',w,'GL_SRC_ALPHA','GL_ONE_MINUS_SRC_ALPHA');
ifi = Screen('GetFlipInterval', w);
topPriorityLevel = MaxPriority(w);
Priority(topPriorityLevel);
% 1. Draw a red ellipse and a blue ellipse that move a random distance from right to right each time the screen is switched.
% Set the screen switching method directly, keyboard input, mouse input, etc.
% When the ellipse reaches the far right end or an arbitrary destination, display a result such as 'Red Win' in text.
% 2. Save the match results and the distance traveled for each ellipse in Excel or Notepad.
% When an ellipse passes the midpoint of the screen, make it move after a 5-second break. Show the timer for the remaining rest time.
% Display the distance the two ellipses have moved.
% ex) red sum: 300pixel, now: 5pixel
KbName('UnifyKeyNames');
escapeKey = KbName('ESCAPE');
leftKey = KbName('LeftArrow');
rightKey = KbName('RightArrow');
%----------------------------------------------------%
% Set circle color and size, initial position
%----------------------------------------------------%
dim = 100;
baseRect = [0 0 dim dim];
red = [1 0 0];
blue = [0 0 1];
xpos = [dim/2 dim/2];
ypos = [screenYpixels*4/8, screenYpixels*6/8];
vector = zeros(4, 2);
colors = [red', blue'];
Screen('TextSize', w, 50);
text = ['Red '; 'Blue'];
text_pos = [100 150];
vbl = Screen('Flip', w);
waitframes = 1;
waitsecs = round(waitframes/ifi);
goal_line = 1800;
time = 5;
sum = [0, 0];
exit = false;
HideCursor;
while exit == false
[keyIsDown, secs, keyCode] = KbCheck;
pixelsPerPress = randi([10 20], 1, 2);
%-----------------------------------------------%
% You win when you reach a certain point
%-----------------------------------------------%
if vector(1, 1) >= goal_line && vector(1, 2) < vector(1,1)
Win_Text = 'Red Win!';
pixelsPerPress = [0, 0];
DrawFormattedText(w, Win_Text, 'center', 'center', red);
elseif vector(1, 2) >= goal_line && vector(1, 1) < vector(1, 2)
Win_Text = 'Blue Win!';
pixelsPerPress = [0, 0];
DrawFormattedText(w, Win_Text, 'center', 'center', blue);
end
if keyCode(escapeKey)
exit = true;
elseif keyCode(leftKey)
xpos = xpos - pixelsPerPress;
sum = sum + pixelsPerPress;
elseif keyCode(rightKey)
xpos = xpos + pixelsPerPress;
sum = sum + pixelsPerPress;
end
%----------------------------------------------%
% goal post
%----------------------------------------------%
Screen('DrawLine', w, black, goal_line, 0, goal_line, screenYpixels, 1);
%----------------------------------------------------------%
% The circle's coordinate matrix and location text
%----------------------------------------------------------%
for i = 1:2
if xpos(i) < 0
xpos(i) = 0 + dim/2;
end
vector(:, i) = CenterRectOnPointd(baseRect, xpos(i), ypos(i));
text_pixel = [text(i, :) 'Sum = ', num2str(round(sum(i))) 'pixel ', text(i, :) '=', num2str(round(xpos(i)-50)) 'pixel'];
DrawFormattedText(w, text_pixel, 'center', text_pos(i), black);
end
Screen('Filloval', w, colors, vector);
vbl = Screen('Flip', w, vbl + (waitframes - 0.5) * ifi);
%-----------------------------------------------------------------------%
% Rest for 5 seconds after passing half the width of the screen
%-----------------------------------------------------------------------%
if vector(1, 1) >= screenXpixels/2 || vector (1, 2) >= screenXpixels/2
while time >= 0
Screen('FillRect', w, white);
if time > 0
DrawFormattedText(w, num2str(time), 'center', 'center', black);
end
vbl = Screen('Flip', w, vbl + (waitsecs - 0.5)*ifi);
time = time - 1;
end
end
end
%---------------------------------%
% save to excel sheet
%---------------------------------%
red_pixel = round(vector(1, 1));
blue_pixel = round(vector(1, 2));
winner = {Win_Text};
T = table(red_pixel, blue_pixel, winner);
filename = 'windata.xlsx';
writetable(T, filename);
sca;
Solved!
But if there is a way to make my code simpler, please help!
if vector(1, 1) >= screenXpixels/2 || vector (1, 2) >= screenXpixels/2
while time >= 0
Screen('FillRect', w, white);
if time > 0
DrawFormattedText(w, num2str(time), 'center', 'center', black);
end
vbl = Screen('Flip', w, vbl + (waitsecs - 0.5)*ifi);
time = time - 1;
end
end
I changed the above code as below.
if vector(1, 1) >= screenXpixels/2 || vector (1, 2) >= screenXpixels/2
while time >= 0
if time > 0
DrawFormattedText(w, num2str(time), 'center', 400, black);
end
if time < 5
waitsecs = round(waitframes/ifi);
end
vbl = Screen('Flip', w, vbl + (waitsecs - 0.5) * ifi);
time = time - 1;
end
end