Help with a Project for school.

8 Ansichten (letzte 30 Tage)
Bruce Headley
Bruce Headley am 22 Mär. 2011
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

Antworten (2)

Bruce Headley
Bruce Headley am 22 Mär. 2011
hold on
%These are the staring points
x(1)=50;
y(1)=10;
dx=1;
dy=1;
%Direction in which the ball will move right 1 left -1
x_dir=[-1 1];
%Direction in which the ball will move up 1 down -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 just realized that the code was jumbled up so I fixed it to make it easier to read.
Thanks again.

Walter Roberson
Walter Roberson am 22 Mär. 2011
if j == 1; ydir = 1; end
then continue on with your
y(j+1)=y(j)+dy*ydir;
  1 Kommentar
Bruce Headley
Bruce Headley am 23 Mär. 2011
I tried adding that statement in between the:
while y(j)>=10;
x(j+1)=x(j)+dx*xdir;
and then in between:
x(j+1)=x(j)+dx*xdir;
y(j+1)=y(j)+dy*ydir;
and I got the same results. Either it starts and stop right then or it goes out of bounds for some reason.
Thank you

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by