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

2 ビュー (過去 30 日間)
Ally Kassam
Ally Kassam 2019 年 10 月 18 日
回答済み: Sulaymon Eshkabilov 2019 年 10 月 19 日
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

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 10 月 19 日
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 件)

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by