• Remix
  • Share
  • New Entry

on 11 Oct 2024
  • 19
  • 146
  • 0
  • 3
  • 1991
Cite your audio source here (if applicable):
drawframe(1);
Write your drawframe function below
function drawframe(f)
persistent lY lX eF wT sT elT wR
if isempty(lY)
lX=[1,3,5,7,9,11]-1; % Ladybug X positions
lY=zeros(1,length(lX)); % Initial Y positions for ladybugs
eF=false; % End flag
wT=0; % Winning ladybug index
sT=tic; % Start time
elT=0; % Elapsed time
wR=40; % World record
end
clf; % Clear figure
hold on;
% Draw the track
drT();
% Draw ladybugs
for i=1:length(lY)
f2(lX(i),lY(i),f); % Draw ladybug
end
% Move ladybugs forward
lY=lY+rand(1,length(lY))*0.2;
% Check if any ladybug has crossed the finish line
if ~eF
elT=toc(sT); % Calculate elapsed time
for i=1:length(lY)
if lY(i)>9 % Finish line at Y=9
eF=true;
wT=i;
break;
end
end
end
% Display running clock
text(6,10.5,sprintf('Time: %.2f sec',elT),'FontSize',20,'Color','w','FontWeight','bold','HorizontalAlignment','center');
if eF
% Winning ladybug celebration
f2(lX(wT),lY(wT)+0.5*sin(f*0.3),f); % Jumping effect
text(lX(wT),9.7,'Win!','FontSize',18,'Color','y','FontWeight','bold');
% Display winning time and compare with world record
winT=elT;
text(6,10.5,sprintf('Winning Time: %.2f sec',winT),'FontSize',14,'Color','g','FontWeight','bold','HorizontalAlignment','center');
% Check for new world record
if winT<wR
text(6,10.8,'NEW WORLD RECORD!!','FontSize',22,'Color','r','FontWeight','bold','HorizontalAlignment','center');
end
end
drawnow; % Update figure
end
function drT()
% Draw track background
fill([-1,-1,12,12],[-1,10,10,-1],[0.8,0.4,0.4]); % Track color
fill([-1,12,12,-1],[-1.5,-1.5,0,0],'g'); % Bottom green ground
fill([-1,12,12,-1],[9.9,9.9,11.4,11.4],'g'); % Top green ground
line([-1,12],[0,0],'Color','r','LineWidth',4); % Start line
% Finish line
line([-1,12],[9.9,9.9],'Color','r','LineWidth',4); % Finish line
% Draw track lines and labels
for i=1:6
line([i*2-1,i*2-1],[0,10],'Color','k','LineWidth',2); % Track lines
end
% Set axes limits
xlim([-1,12]);
ylim([-1.5,11.4]); % Extended limit
axis off; % Remove axis
end
function f2(x,y,f)
rH=0.2; % Ladybug head radius
rB=0.3; % Ladybug body radius
th=linspace(0,2*pi,100);
xH=rH*cos(th)+x; % Head position
yH=rH*sin(th)+rB+y; % Head position
fill(xH,yH,'k','LineWidth',2); % Draw head
% Draw body
a=0.2;b=0.3;
xB=a*cos(th)+x;
yB=b*sin(th)+y;
fill(xB,yB,'k','LineWidth',2);
% Draw elytra, antennae, eyes, and legs
f3(x,y);
f4(x,y);
f5(x,y);
f6(x,y,f);
end
function f3(x,y)
rE=0.3; % Elytra radius
thR=linspace(-pi/2,pi/2,100);
thL=linspace(pi/2,3*pi/2,100);
xR=rE*cos(thR)+x;
yR=rE*sin(thR)+y;
xL=rE*cos(thL)+x;
yL=rE*sin(thL)+y;
fill(xR,yR,'r',xL,yL,'r'); % Elytra
end
function f4(x,y)
line([-0.05,0.05]+x,[0.25,0.5]+y,'Color','k','LineWidth',2); % Antennae
end
function f5(x,y)
scatter([0.1,-0.1]+x,[0.3,0.3]+y,20,'w','filled'); % Eyes
scatter([0.05,-0.05]+x,[0.3,0.3]+y,10,'k','filled'); % Pupils
end
function f6(x,y,f)
lO=0.1*sin(f*0.1); % Leg offset
% Right legs
x1=[0.2,0.3,0.4]+x;
y1=[0.1+lO,0.2+lO,0.3+lO]+y;
x2=[0.2,0.3,0.4]+x;
y2=[0+lO,-0.1+lO,0+lO]+y;
% Left legs
x1_l=[-0.2,-0.3,-0.4]+x;
y1_l=[0.1+lO,0.2+lO,0.3+lO]+y;
x2_l=[-0.2,-0.3,-0.4]+x;
y2_l=[0+lO,-0.1+lO,0+lO]+y;
% Plot legs
plot(x1,y1,'k-',x2,y2,'k-',...
x1_l,y1_l,'k-',x2_l,y2_l,'k-','LineWidth',2);
end
Movie
Audio
Remix Tree