How do I get a my script to show my display function 'You gained a point' when my ball hits the randomly placed board

12 vues (au cours des 30 derniers jours)
figure;
AxesH = axes('NextPlot','add');
play = input('Welcome. Do you want to play? (yes or no): ','s');
if strcmp(play,'yes') || strcmp(play,'Yes')
b = input('Please input angle value: ');
[x,y] = HDTask1function_f(b);
z = rand()*3;
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.025);
if y(ii:end) < 0
y(ii:end) = 0;
break
if x(1,:) < (z+0.5)
if x(1,:) > z
disp('You gained a point')
end
end
end
end
end
if strcmp(play,'no') || strcmp(play,'No')
disp('I always thought you weren''t cool enough to play')
end
Function file:
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

Réponses (1)

Geoff Hayes
Geoff Hayes le 21 Avr 2020
Aaron - look closely at the code in your for loop
for ii=2:length(x)
drawnow;
set(hndl,'XData', x(ii), 'YData', y(ii));
pause(0.025);
if y(ii:end) < 0
y(ii:end) = 0;
break
if x(1,:) < (z+0.5)
if x(1,:) > z
disp('You gained a point')
end
end
end
end
The break is called before the check on the x position (to see if the ball lands on the rectangle)....so we want to break after we do the check. Also, the x(1,:) is an array of elements so comparing them all against z+0.5 is (I think) not what you want. You probably just need to check the x(1,ii)) position and ensure that it is greater than z AND less than z+0.5. Try the following
if y(ii) < 0
y(ii) = 0; % <------ is this assignment necessary?
if x(ii) < (z+0.5) && x(ii) > z
disp('You gained a point')
end
break
end

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by