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.
