Hi,
I'm creating a street intersection that has changing lights and moving cars and pedestrians but I'm struggling to get the cars/pedestrians to move within my program without leaving its copies in its path. I've tried using a while loop but it isn't doing what I'm looking for. The picture is a bit of my code and what it looks like when I run it. My traffic lights are in a for loop and I've placed my while loop to move the cars within the for loop.
An example of one sequence of my traffic lights with my while loop for two of my cars.
%%-lower right corner light------------------------
rectangle('Position', [0+64 0+30 2 4],'Curvature', 0.2,'FaceColor', 'k')axis equalhold on;format long g; format compact;darkGray = [0.2, 0.2, 0.2];green = [0, 132, 80] / 255;yellow = [239, 183, 0] / 255;red = [184, 29, 19] / 255;pos = [0.5+64 2.75+30 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)pos = [0.5+64 1.5+30 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)pos = [0.5+64 0.25+30 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', red)%%-upper left corner ---------------------------------
rectangle('Position', [0+35 0+64 2 4],'Curvature', 0.2,'FaceColor', 'k')axis equalhold on;format long g; format compact;darkGray = [0.2, 0.2, 0.2];green = [0, 132, 80] / 255;yellow = [239, 183, 0] / 255;red = [184, 29, 19] / 255;pos = [0.5+35 2.75+64 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', red)pos = [0.5+35 1.5+64 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)pos = [0.5+35 0.25+64 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)%%-lower left corner light-------------------------------
rectangle('Position', [0+30 0+35 4 2],'Curvature', 0.2,'FaceColor', 'k')axis equalhold on;format long g; format compact;darkGray = [0.2, 0.2, 0.2];green = [0, 132, 80] / 255;yellow = [239, 183, 0] / 255;red = [184, 29, 19] / 255;pos = [2.75+30 0.5+35 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', green)pos = [1.5+30 0.5+35 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)pos = [0.25+30 0.5+35 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)%%-upper right corner light--------------------------
rectangle('Position', [0+65 0+65 4 2],'Curvature', 0.2,'FaceColor', 'k')axis equalhold on;format long g; format compact;darkGray = [0.2, 0.2, 0.2];green = [0, 132, 80] / 255;yellow = [239, 183, 0] / 255;red = [184, 29, 19] / 255;pos = [2.75+65 0.5+65 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)pos = [1.5+65 0.5+65 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', darkGray)pos = [0.25+65 0.5+65 1 1];rectangle('Position', pos, 'Curvature',[1 1], 'FaceColor', green)k=0;while k<=90 k=k+1; rectangle('FaceColor','red','Edgecolor','red','Position',[55 k 3 7]) pause(0.1)endk=65;while k<=65 && k>=0 k=k-1; rectangle('FaceColor','magenta','EdgeColor','magenta','Position',[43 k 4 8]) pause(0.1)endpause(5)
Best Answer