MATLAB Answers

0

How to save score from game and then load it when second attempt is made

Asked by Ally Kassam on 18 Oct 2019
Latest activity Answered by Sulaymon Eshkabilov on 19 Oct 2019
clear all
err = 0.1; %-- error margin
figure; hold on
xlim ([0 2])
ylim ([0 2])
score = 0; %-- number of correct hits
circleval = 40;
hit = true; %- will change to 'false' if target is not hit
tic
load score
while score < 6 %-- repeat loop until number of correct hits is 6
if hit
x=rand(1) %- location of target
y=rand(1)
a = rand(1)
b = rand(1)
c = rand(1)
d = rand(1)
e = rand(1)
f = rand(1)
hl = plot(x,y,'o')
hl(1).MarkerSize = circleval; % make the markersize the size of cicrcle value
hit = false
plot (a,b,'g o',c,d,'b o',e,f,'c o') % add 3 random circles
end
[x1 y1] = ginput(1) %- location of click
plot(x1,y1, '+')
dist = sqrt((x-x1)^2+(y-y1)^2); %-- distance between target and click
if dist< err
hit = true
score = score + 1
circleval = circleval - 5; % decrease the circle to 5 less each time
end
end
title(toc)
save score
clear all
return
%Qn 4
accuracyscore = dist*2 + toc
% Qn 2
if toc <= 10
disp ('You won GOLD')
elseif (toc >= 11 && toc<= 15)
disp ('You won SILVER')
elseif (toc >= 16 && toc<= 20)
disp ('You won BRONZE')
else
toc >= 21
disp ('No medal')
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Sulaymon Eshkabilov on 19 Oct 2019
 Accepted Answer

Hi,
Now it is running as anticipated. When you start with the following intialization (score is <6), e.g.:
>> score=1; save score
Then if you run your code, it is saving the last score and loading in the second attempt.
Or I have misunderstood your question.
Good luck.

  0 Comments

Sign in to comment.