Help with a Project for school.
Afficher commentaires plus anciens
I am not looking for a total answer for the problem. I have written some code and it is not working like it should. The basic problem is to make a simple pinball program. The outside limits are at x=30 to x=70 and y=10 to y=110. There is a function that has been provided for us, but i am not adding it to the code here just to simplify it. So, the ball starts on the line of y=10 and either bounces to the left or right and is supposed to reverse direction as soon as it hits the other walls. As soon as the ball hits y=10 then it stops.
hold on;
%This is the staring points x(1)=50; y(1)=10; %Direction in which the ball will move %right 1 %left -1 dx=1; dy=1; x_dir=[-1 1]; y_dir=[-1 1]; xdir=x_dir(randi([1,2],1)); ydir=y_dir(randi([1,2],1));
%while y(j)>=10 means game over, the other if statements are meant to bounce the ball off of that line.
j=1; while y(j)>=10; x(j+1)=x(j)+dx*xdir; y(j+1)=y(j)+dy*ydir; if x(j+1)<=70; xdir=-1; ydir=y_dir(randi([1,2],1)); end if x(j+1)>=30; xdir=1; ydir=y_dir(randi([1,2],1)); end if y(j+1)>=110; xdir=x_dir(randi([1,2],1)); ydir=-1; end
plot(x,y,'bo');
j=j+1;
end
hold off
I think my problem is at line 34. Which is: y(j+1)=y(j)+dy*ydir;
This line is saying the the y line can be either a negative direction or positive direction, but when the program starts it should be going in the positive y direction. When I changed it to: y(j+1)=y(j)1
the program would freeze. I am really just learning this program and I will be using it throughout my time at college. I would really like to know what I have done and what I need to do just to make the program at least start correctly.
Thank you,
Bruce Headley
Réponses (2)
Bruce Headley
le 22 Mar 2011
Walter Roberson
le 22 Mar 2011
if j == 1; ydir = 1; end
then continue on with your
y(j+1)=y(j)+dy*ydir;
1 commentaire
Bruce Headley
le 23 Mar 2011
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!