2017-02-03 67 views
1

我想重新创建在MATLAB中的“庞”,到目前为止,我已经能够生成图,画一个球场,画出桨,并画出球。此时,我正试图通过键盘输入更新桨的位置(上下移动它们)。我试图利用MATLAB的内置“KeyPressFcn”和“KeyReleaseFcn”功能来完成这项工作,但由于某些原因,桨板仍然不动。我的代码如下。任何人都可以看到我做错了什么?MATLAB的键盘输入没有更新“庞”游戏

% 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 

回答

1

我认为这是一个简单的解决方案。

在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 
+0

我给它一个镜头,但没有奏效。我遇到的问题是“left_bottom”和“right_bottom”没有像应该那样递增。当我按下适当的键时,这些值应该被0.05改变,并且这将允许桨以该增量上下移动。我甚至离开了“;”在while循环中的每一个后面查看值是否发生了变化(它们在5处被初始化),但它们不是,但我没有看到关键侦听器与更新这些变量之间的断开的位置while循环。 – electronicaneer

+1

复制并粘贴完整的代码...当我按下'q'键时,红色桨板向上移动。 – Rotem

+0

谢谢!因此,您所做的主要更改是添加暂停,而且您还使用名为PongGame的函数来包装整个程序。这是如何使它正常工作? – electronicaneer