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
我给它一个镜头,但没有奏效。我遇到的问题是“left_bottom”和“right_bottom”没有像应该那样递增。当我按下适当的键时,这些值应该被0.05改变,并且这将允许桨以该增量上下移动。我甚至离开了“;”在while循环中的每一个后面查看值是否发生了变化(它们在5处被初始化),但它们不是,但我没有看到关键侦听器与更新这些变量之间的断开的位置while循环。 – electronicaneer
复制并粘贴完整的代码...当我按下'q'键时,红色桨板向上移动。 – Rotem
谢谢!因此,您所做的主要更改是添加暂停,而且您还使用名为PongGame的函数来包装整个程序。这是如何使它正常工作? – electronicaneer