2017-02-03 18 views
1

MATLABで "Pong"を再作成しようとしていますが、これまでは図を生成し、裁判所を描き、パドルを描き、ボールを描くことができました。この時点で、私はパドルの位置を(キーボードを上下に動かして)キーボード入力で更新しようとしています。私はこれを行うために、MATLABの組み込み "KeyPressFcn"と "KeyReleaseFcn"機能を利用しようとしていますが、何らかの理由でパドルが動いていません。私のコードは以下の通りです。誰でも私が間違っていることを見ることができますか?MATLABキーボード入力が "Pong"ゲームで更新されない

% 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がすべてのイベントをブロックするという問題があります。 drawnowまたはpauseを追加すると、Matlabはイベントに応答します。


問題も変数スコープ規則に関連している可能性があります。
end main関数のステートメントがファイルの最後のコード行にあることを確認してください。

は、次の(完全なコード)をチェックしてください:

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ずつ変更されます。これにより、パドルがその増分で上下に移動することができます。私は ";" (それぞれ5で初期化されています)、そうではありませんが、キーリスナーとその中のそれらの変数の更新がどこにあるか分かりませんwhileループ。 – electronicaneer

+1

完全なコードをコピーアンドペーストします。「q」キーを押すと、赤いパドルが上に移動します。 – Rotem

+0

ありがとう!だから主な変更はポーズを追加することでしたが、PongGameという関数でプログラム全体をラップしました。どのようにそれが正常に動作するようになりますか? – electronicaneer

関連する問題