- /
-
Arkanoid
on 20 Oct 2024
- 28
- 234
- 1
- 3
- 1224
Cite your audio source here (if applicable):
drawframe(90);
Write your drawframe function below
function drawframe(f)
persistent bricks ball_x ball_y ball_dx ball_dy
if isempty(bricks)
bricks=zeros(8,16);
bricks(2:7,9:14)=randi(57,6);
[ball_x,ball_y,ball_dx,ball_dy]=deal(8,1,1,1);
end
map=colorcube;
axis([0 16 0 16]);
axis equal
axis off
next_x=ball_x+ball_dx;
next_y=ball_y+ball_dy;
border=false;
if next_x<1 || next_x>16
ball_dx=-ball_dx;
border=true;
end
if next_y<2 || next_y>16
ball_dy=-ball_dy;
border=true;
end
if ~border
b_x=bricks(ceil(next_x/2),ball_y)>0;
b_y=bricks(ceil(ball_x/2),next_y)>0;
b_xy=bricks(ceil(next_x/2),next_y)>0 && ~b_x && ~b_y;
if b_x || b_xy
ball_dx=-ball_dx;
end
if b_y || b_xy
ball_dy=-ball_dy;
end
bricks(ceil(next_x/2),ball_y)=0;
bricks(ceil(ball_x/2),next_y)=0;
bricks(ceil(next_x/2),next_y)=0;
end
ball_x=ball_x+ball_dx;
ball_y=ball_y+ball_dy;
cla;
rectangle('Position',[0,0,16,16],'EdgeColor',[0.5,0.5,0.5],'linewidth',3);
for x=1:8
for y=1:16
b=bricks(x,y);
if b
rectangle('Position',[2*x-1.8,y-1,1.8,0.8],'EdgeColor','#D0D0D0','FaceColor','#D0D0D0'); % shadow
rectangle('Position',[2*x-1.9,y-0.9,1.8,0.8],'EdgeColor',map(b,:),'FaceColor',map(b,:)); % piece
end
end
end
rectangle('Position',[ball_x-1,ball_y-1,1,1],'Curvature',[1,1],'EdgeColor','#606060','FaceColor','#606060'); % ball
rectangle('Position',[min(13,max(0,ball_x-2))+0.1,0.2,2.8,0.8],'Curvature',[0.2,0.2],'EdgeColor','b','FaceColor','b'); % base
end