Help!! Projectile motion plot
5 ビュー (過去 30 日間)
古いコメントを表示
am currently having trouble with my script outputing the scores when the user has scored a point by hitting the board on the plot with a ball. Any help will be greatly appreciated.
(script)
h=figure;
score = 0;
clf(h);
AxesH = axes('NextPlot','add');
while (1)
play = menu('Would you like to play?: ', 'Yes', 'No');
switch play
case (1)
b = input('Please input angle value: ');
if (b >= 0) && (b <=90)
[x,y] = HDTask1function_f(b);
z = rand()*3.5;
j = z+0.5;
hndl2=plot([z j],[0 0],'LineWidth',5);
hold on
set(gca,'Xlim',[0 4])
set(gca,'Ylim',[0 4])
hndl=plot(x(1),y(1),'r.','MarkerSize',50);
for ii=2:length(x)
drawnow;
set(hndl,'XData', x(ii), 'YData', y(ii));
pause(0.0025);
if y(ii:end) < 0
y(ii:end) = 0;
pause(0.5);
if x(1,ii) > z && x(1,ii) < j
score = score +1;
fprintf('Your current score is: %4.2', num2str(score));
end
break
end
end
else
disp('Invalid input. Please input an angle in the range of [0 - 90]');
end
case(2)
disp('Thankyou for visiting')
break;
end
end
(function)
function [x,y]=HDTask1function_f(a)
x0=0;
y0=1.8;
v0=4;
g=9.8;
t=0:0.01:5;
x=x0+v0*cos(a*(pi./180)).*t;
y=y0+v0*sin(a*(pi./180)).*t-(g.*t.^2)/2;
end
8 件のコメント
Ameer Hamza
2020 年 4 月 22 日
Aaron, There is no point in flagging Rik's comment. I don't see how that will help you in solving your problem. I have removed these flags since you are clearly pointless.
Rik
2020 年 4 月 22 日
Now I'm convinced you are trolling me. How is spending time to help you a form of bullying? I'm done with you. I do this for fun. From now on you don't have to expect any form of help from me.
回答 (1 件)
Rik
2020 年 4 月 22 日
Despite your rudeness (i.e. contiuously flagging my comments) I decided to help you. So below you will find your code with some minor edits and with some comments for places where you need to do some edits.
By making it a function you give mlint the opportunity to give you helpful advice. Make sure all warnings are resolved.
function MyGame
h=figure;
score = 0;
%clf(h);
%the figure was just created, so you don't need to clear it
AxesH = axes('NextPlot','add','Parent',h);
% ^^^^^^^^^^^ add this to make it explicit
while (1)
play = menu('Would you like to play?: ', 'Yes', 'No');
switch play
case (1)
b = input('Please input angle value: ');
if (b >= 0) && (b <=90)
[x,y] = HDTask1function_f(b);
z = rand()*3.5;
j = z+0.5;
hndl2=plot([z j],[0 0],'LineWidth',5,'Parent',handle_to_parent_axis);
% ^^^^^ ^^^^^^^^^^^^ add a parent object here
% where are you using hndl2 for? you could use it to delete the target plate, but you don't
% shouldn't these three lines be outside of the loop (or in some logic making sure they only run once)? This way the target moves every time the game runs.
% hold on
% hold on is not needed, because you already have NextPlot set to add
set(gca,'Xlim',[0 4])
set(gca,'Ylim',[0 4])
% these two line are part of the setup and only need to run once
hndl=plot(x(1),y(1),'r.','MarkerSize',50);
for ii=2:length(x)
drawnow;
set(hndl,'XData', x(ii), 'YData', y(ii));
pause(0.0025);
if y(ii:end) < 0
y(ii:end) = 0;
pause(0.5);
if x(1,ii) > z && x(1,ii) < j
score = score +1;
fprintf('Your current score is: %4.2f', num2str(score));
% ^ this was missing (and why is this displayed with two decimals? if the score is an integer you could use %d instead)
end
break
end
end
else
disp('Invalid input. Please input an angle in the range of [0 - 90]');
end
case(2)
disp('Thankyou for visiting')
break;
end
end
end
function [x,y]=HDTask1function_f(a)
x0=0;
y0=1.8;
v0=4;
g=9.8;
t=0:0.01:5;
x=x0+v0*cos(a*(pi./180)).*t;
y=y0+v0*sin(a*(pi./180)).*t-(g.*t.^2)/2;
end
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!