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