Need help with getting my function to work properly

1 次查看(过去 30 天)
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 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by