MATLAB 键盘输入未在 "Pong" 游戏上更新

MATLAB Keyboard inputs not updating on "Pong" game

我正在尝试在 MATLAB 中重新创建 "Pong",到目前为止,我已经能够生成图形、绘制球场、绘制球拍和绘制球。此时,我正在尝试通过键盘输入更新桨的位置(上下移动)。我正在尝试利用 MATLAB 的内置 "KeyPressFcn" 和 "KeyReleaseFcn" 功能来执行此操作,但由于某种原因,桨仍然没有移动。我的代码如下。谁能看出我做错了什么?

% Create court figure, make title, set axis, draw middle dotted line and
% top/bottom lines, turn axis off
court = figure;
set(court, 'Color', 'black', 'toolbar', 'none', 'menubar', 'none');
title('ENGR 122 PONG', 'FontSize', 18, 'Color', 'w');
axis([0 14 0 12]);
line([7 7],[0 12],'LineStyle',':','LineWidth',1,'Color','yellow');
line([0 14], [12 12], 'LineStyle', '-', 'LineWidth', 3, 'Color', 'yellow');
line([0 14], [0 0], 'LineStyle', '-', 'LineWidth', 3, 'Color', 'yellow');
axis off

% Initialize inputs for left and right paddle
left_input = 0;
right_input = 0;

% Initialize ball on court
hold on
ball = plot(7, 6, 'w.', 'MarkerSize', 15);

% Initialize paddles on court, set speed
left_bottom = 5;
left_height = 2;
right_bottom = 5;
right_height = 2;
left_paddle = line([1 1], [left_bottom (left_bottom + left_height)], 'LineWidth', 5, 'Color', 'red');
right_paddle = line([13 13], [right_bottom (right_bottom + right_height)], 'LineWidth', 5, 'Color', 'blue');

% Initialize score on screen
left_score = 0;
right_score = 0;
draw_left_score = text(2, 10, num2str(left_score), 'FontSize', 25, 'Color', 'yellow');
draw_right_score = text(12, 10, num2str(right_score), 'FontSize', 25, 'Color', 'yellow');

% While neither player has scored 10 points yet
while (left_score < 10 || right_score < 10)
    % Update left and right paddle values
    left_bottom = updateLeft(left_input, left_bottom)
    right_bottom = updateRight(right_input, right_bottom);

    % Set Key listeners to figure
    set(court, 'KeyPressFcn', @keyDown, 'KeyReleaseFcn', @keyUp);

    % Update left and right paddle positions on figure
    set(left_paddle, 'YData', [left_bottom (left_bottom + left_height)]);
    set(right_paddle, 'YData', [right_bottom (right_bottom + right_height)]);

end

% Function listening for keys being pressed
function keyDown(source, event)
    if event.Key == 'q'
        left_input = 1;
    end
    if event.Key == 'a'
        left_input = -1;
    end
    if event.Key == 'o'
        right_input = 1;
    end
    if event.Key == 'l'
        right_input = -1;
    end
end

% Function listening for keys being released
function keyUp(source, event)
    if event.Key == 'q'
        left_input = 0;
    end
    if event.Key == 'a'
        left_input = 0;
    end
    if event.Key == 'o'
        right_input = 0;
    end
    if event.Key == 'l'
        right_input = 0;
    end   
end

% Function updating left paddle
function left_bottom = updateLeft(left_input, left_bottom)
    if left_input == 1
        left_bottom = left_bottom + .05;
    elseif left_input == -1
        left_bottom = left_bottom - .05;
    end
end

% Function updating right paddle
function right_bottom = updateRight(right_input, right_bottom)
    if right_input == 1
        right_bottom = right_bottom + .05;
    elseif right_input == -1
        right_bottom = right_bottom - .05;
    end
end

我认为这个问题有一个简单的解决方案。

在 while 循环结束时调用 drawnow 函数。
您还可以添加暂停命令,如 pause(0.01)

% While neither player has scored 10 points yet
while (left_score < 10 || right_score < 10)
    % Update left and right paddle values
    left_bottom = updateLeft(left_input, left_bottom);
    right_bottom = updateRight(right_input, right_bottom);

    % Set Key listeners to figure
    set(court, 'KeyPressFcn', @keyDown, 'KeyReleaseFcn', @keyUp);

    % Update left and right paddle positions on figure
    set(left_paddle, 'YData', [left_bottom (left_bottom + left_height)]);
    set(right_paddle, 'YData', [right_bottom (right_bottom + right_height)]);

    drawnow
end

问题是 Matlab 在执行紧循环时会阻塞所有事件。 添加 drawnowpause,使 Matlab 响应事件。


问题也可能与变量有关范围规则
确保主函数的 end 语句位于文件的最后一行代码。

检查以下(完整代码):

function PongGame()
court = figure;
set(court, 'Color', 'black', 'toolbar', 'none', 'menubar', 'none');
title('ENGR 122 PONG', 'FontSize', 18, 'Color', 'w');
axis([0 14 0 12]);
line([7 7],[0 12],'LineStyle',':','LineWidth',1,'Color','yellow');
line([0 14], [12 12], 'LineStyle', '-', 'LineWidth', 3, 'Color', 'yellow');
line([0 14], [0 0], 'LineStyle', '-', 'LineWidth', 3, 'Color', 'yellow');
axis off

% Initialize inputs for left and right paddle
left_input = 0;
right_input = 0;

% Initialize ball on court
hold on
ball = plot(7, 6, 'w.', 'MarkerSize', 15);

% Initialize paddles on court, set speed
left_bottom = 5;
left_height = 2;
right_bottom = 5;
right_height = 2;
left_paddle = line([1 1], [left_bottom (left_bottom + left_height)], 'LineWidth', 5, 'Color', 'red');
right_paddle = line([13 13], [right_bottom (right_bottom + right_height)], 'LineWidth', 5, 'Color', 'blue');

% Initialize score on screen
left_score = 0;
right_score = 0;
draw_left_score = text(2, 10, num2str(left_score), 'FontSize', 25, 'Color', 'yellow');
draw_right_score = text(12, 10, num2str(right_score), 'FontSize', 25, 'Color', 'yellow');

% While neither player has scored 10 points yet
while (left_score < 10 || right_score < 10)
    % Update left and right paddle values
    left_bottom = updateLeft(left_input, left_bottom);
    right_bottom = updateRight(right_input, right_bottom);

    % Set Key listeners to figure
    set(court, 'KeyPressFcn', @keyDown, 'KeyReleaseFcn', @keyUp);

    % Update left and right paddle positions on figure
    set(left_paddle, 'YData', [left_bottom (left_bottom + left_height)]);
    set(right_paddle, 'YData', [right_bottom (right_bottom + right_height)]);

    drawnow
end

% Function listening for keys being pressed
function keyDown(source, event)
    if event.Key == 'q'
        left_input = 1;
    end
    if event.Key == 'a'
        left_input = -1;
    end
    if event.Key == 'o'
        right_input = 1;
    end
    if event.Key == 'l'
        right_input = -1;
    end
end

% Function listening for keys being released
function keyUp(source, event)
    if event.Key == 'q'
        left_input = 0;
    end
    if event.Key == 'a'
        left_input = 0;
    end
    if event.Key == 'o'
        right_input = 0;
    end
    if event.Key == 'l'
        right_input = 0;
    end   
end

% Function updating left paddle
function left_bottom = updateLeft(left_input, left_bottom)
    if left_input == 1
        left_bottom = left_bottom + .05;
    elseif left_input == -1
        left_bottom = left_bottom - .05;
    end
end

% Function updating right paddle
function right_bottom = updateRight(right_input, right_bottom)
    if right_input == 1
        right_bottom = right_bottom + .05;
    elseif right_input == -1
        right_bottom = right_bottom - .05;
    end
end

end