Need help with getting my function to work properly

조회 수: 3 (최근 30일)
Laith
Laith 2023년 10월 30일
답변: Aishwarya 2023년 11월 7일
I created a function for my hangman game that creates body parts after every incorrect guess that creates a skeleton but whenever i add call and use function in my script, the skeleton updates like its supposed to but stays minimized without popping back up while running the game. I tried "figure(1)", i tried "hold on". Is there a fix with the function or script?
I need help getting the interface to pop up on the screen after every incorrect guess instead of staying minimized
This is my function
function HangingMan(incorrectGuesses, difficulty)
D1P = cell(12, 1);
for i = 1:12
D1P{i} = imread(sprintf('D1I%d.jpg', i));
end
if difficulty == 1 && incorrectGuesses >= 1 && incorrectGuesses <= 12
image(D1P{incorrectGuesses});
end
D2P = cell(9, 1);
for i = 1:9
D2P{i} = imread(sprintf('D2I%d.jpg', i));
end
if difficulty == 2 && incorrectGuesses >= 1 && incorrectGuesses <= 9
image(D2P{incorrectGuesses});
end
D3P = cell(7, 1);
for i = 1:7
D3P{i} = imread(sprintf('D3I%d.jpg', i));
end
if difficulty == 3 && incorrectGuesses >= 1 && incorrectGuesses <= 7
image(D3P{incorrectGuesses});
end
end

답변 (1개)

Aishwarya
Aishwarya 2023년 11월 7일
Hi Laith,
As per my understanding, in the provided function, the figure window should pop up after every incorrect guess.
After reviewing the code, I would suggest to add “figure(gcf)” command before each “imagefunction to pop up the updated figure window. After making the changes, the modified function is as shown below.
function HangingMan(incorrectGuesses, difficulty)
D1P = cell(12, 1);
for i = 1:12
D1P{i} = imread(sprintf('D1I%d.jpg', i));
end
if difficulty == 1 && incorrectGuesses >= 1 && incorrectGuesses <= 12
figure(gcf);
image(D1P{incorrectGuesses});
end
D2P = cell(9, 1);
for i = 1:9
D2P{i} = imread(sprintf('D2I%d.jpg', i));
end
if difficulty == 2 && incorrectGuesses >= 1 && incorrectGuesses <= 9
figure(gcf);
image(D2P{incorrectGuesses});
end
D3P = cell(7, 1);
for i = 1:7
D3P{i} = imread(sprintf('D3I%d.jpg', i));
end
if difficulty == 3 && incorrectGuesses >= 1 && incorrectGuesses <= 7
figure(gcf);
image(D3P{incorrectGuesses});
end
end
Please refer to the below MathWorks documentation for more information about the functions used:
I hope this helps!

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by