Animate a blue box moving around the grid
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I am trying to get a blue box to go around the grid with user inputed direction such as arrowkeys but im not sure how to code this. Currently the code has a grid of red squares and th blue box travels along the red squares in sequence according to the code.
here is a copy of the code:
figure(1)
plot([0:10:100; 0:10:100], [0:10:100; 0:10:100], 'k')
hold on
%boxes along left side, 10x10 red
fill([0 10 10 0]+0, [0 0 10 10]+0, 'r')
fill([0 10 10 0]+0, [0 0 10 10]+10, 'r')
fill([0 10 10 0]+0, [0 0 10 10]+20, 'r')
fill([0 10 10 0]+0, [0 0 10 10]+30, 'r')
fill([0 10 10 0]+0, [0 0 10 10]+40, 'r')
fill([0 10 10 0]+0, [0 0 10 10]+50, 'r')
fill([0 10 10 0]+0, [0 0 10 10]+60, 'r')
fill([0 10 10 0]+0, [0 0 10 10]+70, 'r')
fill([0 10 10 0]+0, [0 0 10 10]+80, 'r')
fill([0 10 10 0]+0, [0 0 10 10]+90, 'r')
%boxes along the bottom side, 10x10 red
fill([0 10 10 0]+10, [0 0 10 10]+0, 'r')
fill([0 10 10 0]+20, [0 0 10 10]+0, 'r')
fill([0 10 10 0]+30, [0 0 10 10]+0, 'r')
fill([0 10 10 0]+40, [0 0 10 10]+0, 'r')
fill([0 10 10 0]+50, [0 0 10 10]+0, 'r')
fill([0 10 10 0]+60, [0 0 10 10]+0, 'r')
fill([0 10 10 0]+70, [0 0 10 10]+0, 'r')
fill([0 10 10 0]+80, [0 0 10 10]+0, 'r')
fill([0 10 10 0]+90, [0 0 10 10]+0, 'r')
%boxes along right side, 10x10 red
fill([0 10 10 0]+90, [0 0 10 10]+00, 'r')
fill([0 10 10 0]+90, [0 0 10 10]+10, 'r')
fill([0 10 10 0]+90, [0 0 10 10]+20, 'r')
fill([0 10 10 0]+90, [0 0 10 10]+30, 'r')
fill([0 10 10 0]+90, [0 0 10 10]+40, 'r')
fill([0 10 10 0]+90, [0 0 10 10]+50, 'r')
fill([0 10 10 0]+90, [0 0 10 10]+60, 'r')
fill([0 10 10 0]+90, [0 0 10 10]+70, 'r')
fill([0 10 10 0]+90, [0 0 10 10]+80, 'r')
fill([0 10 10 0]+90, [0 0 10 10]+90, 'r')
%boxes along the top, 10x10 red
fill([0 10 10 0]+10, [0 0 10 10]+90, 'r')
fill([0 10 10 0]+20, [0 0 10 10]+90, 'r')
fill([0 10 10 0]+30, [0 0 10 10]+90, 'r')
fill([0 10 10 0]+40, [0 0 10 10]+90, 'r')
fill([0 10 10 0]+50, [0 0 10 10]+90, 'r')
fill([0 10 10 0]+60, [0 0 10 10]+90, 'r')
fill([0 10 10 0]+70, [0 0 10 10]+90, 'r')
fill([0 10 10 0]+80, [0 0 10 10]+90, 'r')
% diagonal starting from bottom left to top right, black
fill([0 10 10 0]+10, [0 0 10 10]+10, 'k')
fill([0 10 10 0]+20, [0 0 10 10]+20, 'k')
fill([0 10 10 0]+30, [0 0 10 10]+30, 'k')
fill([0 10 10 0]+40, [0 0 10 10]+40, 'k')
fill([0 10 10 0]+50, [0 0 10 10]+50, 'k')
fill([0 10 10 0]+60, [0 0 10 10]+60, 'k')
fill([0 10 10 0]+70, [0 0 10 10]+70, 'k')
fill([0 10 10 0]+80, [0 0 10 10]+80, 'k')
% diagonal from bottom right corner to top left corner
fill([0 10 10 0]+80, [0 0 10 10]+10, 'k')
fill([0 10 10 0]+70, [0 0 10 10]+20, 'k')
fill([0 10 10 0]+60, [0 0 10 10]+30, 'k')
fill([0 10 10 0]+50, [0 0 10 10]+40, 'k')
fill([0 10 10 0]+40, [0 0 10 10]+50, 'k')
fill([0 10 10 0]+30, [0 0 10 10]+60, 'k')
fill([0 10 10 0]+20, [0 0 10 10]+70, 'k')
fill([0 10 10 0]+10, [0 0 10 10]+80, 'k')
%%
fill([0 10 10 0]+0, [0 0 10 10]+0, 'b')
hold off
grid
axis square
x = [0 10 10 0 ];
y = [0 0 10 10];
g = hgtransform;
patch('XData',x,'YData',y,'FaceColor','blue', 'Parent' ,g)
axis equal
xlim([0 100])
ylim([0 100])
pt1 = [0 0 0];
pt2 = [90 0 0];
pt3 = [90 90 0];
pt4 = [0 90 0];
pt5 = [45 45 0];
for t=linspace(0,1,1000)
g.Matrix = makehgtform('translate',pt1 + t*(pt2-pt1));
drawnow
end
for t=linspace(0,1,1000)
g.Matrix = makehgtform('translate',pt2 + t*(pt3-pt2));
drawnow
end
for t=linspace(0,1,1000)
g.Matrix = makehgtform('translate',pt3 + t*(pt4-pt3));
drawnow
end
for t=linspace(0,1,1000)
g.Matrix = makehgtform('translate',pt4 + t*(pt1-pt4));
drawnow
end
for t=linspace(0,1,1000)
g.Matrix = makehgtform('translate',pt1 + t*(pt3-pt1));
drawnow
end
for t=linspace(0,1,1000)
g.Matrix = makehgtform('translate',pt3 + t*(pt5-pt3));
drawnow
end
PS. Also any knowledge of how to shorten the first 50 or so lines of code would be appreciated
0 commentaires
Réponses (1)
Ayush Bansal
le 6 Nov 2020
Create an app using App designer. Use startupFcn callback to execte a function on starting of the app. Use KeyPressFcn callback of UIFigure properties to change the data and update it to change the position of the box. Use for loop to reduce the top lines.
0 commentaires
Voir également
Catégories
En savoir plus sur Dialog Boxes dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!