2017-02-03 4 views
1

Я пытаюсь воссоздать «Понг» в MATLAB, и до сих пор мне удалось создать фигуру, нарисовать корт, нарисовать весла и нарисовать мяч , На этом этапе я пытаюсь обновить положение лопастей (перемещать их вверх и вниз) с помощью ввода с клавиатуры. Я пытаюсь использовать встроенные функции «KeyPressFcn» и «KeyReleaseFcn» MATLAB для этого, но по какой-то причине весла все еще не двигаются. Мой код ниже. Может ли кто-нибудь увидеть, что я делаю неправильно?Входы 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

Я думаю, что есть простое решение проблемы.

Звоните drawnow Функция в конце цикла while.
Вы также можете добавить команду паузы, как 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.

Проверьте следующее (полный код):

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), а они нет, но я не вижу, где разрыв между прослушивателями ключей и обновлением этих переменных в в то время как цикл. – electronicaneer

+1

Скопируйте и вставьте полный код ... Когда я нажимаю клавишу «q», красный весло поднимается вверх. – Rotem

+0

Спасибо! Итак, основные изменения, которые вы сделали, это добавить паузу, но также вы завернули всю программу функцией PongGame. Почему это работает? – electronicaneer

 Смежные вопросы

  • Нет связанных вопросов^_^