DynamicModule[
{
xbar,
xball,
yball,
rball,
spdx,
spdy,
lx,
ly,
ihit,
hit,
score,
uptime,
stflag,
endflag,
resflag =0,
testscore=0,

bflag1,bflag2,bflag3,bflag4,bflag5,bflag6,bflag7,bflag8,bflag9,bflag10,bflag11,bflag12,bflag13,bflag14,bflag15,bflag16,bflag17,bflag18,bflag19,bflag20
},
{
Button[left,If[xbar>5,xbar=xbar-2,xbar=3.5]],
Button[right,If[xbar<15,xbar=xbar+2,xbar=16.5]],
Button[reset,resflag=0],
Animate[
If[resflag==0,
xbar=10;
yball=4;
spdx=0;
spdy=0;
lx=0;
ly=0;
ihit=0;
hit=0;
score=0;
uptime=0;
stflag=0;
endflag=0;
resflag=1;
testscore=0;

bflag1=0;
	bflag2=0;
	bflag3=0;
	bflag4=0;
	bflag5=0;
	bflag6=0;
	bflag7=0;
	bflag8=0;
	bflag9=0;
	bflag10=0;
	bflag11=0;
	bflag12=0;
	bflag13=0;
	bflag14=0;
	bflag15=0;
	bflag16=0;
	bflag17=0;
	bflag18=0;
	bflag19=0;
	bflag20=0;
];
(*球の初期位置設定*)
If[stflag ==0&&endflag==0,
uptime = t;
xball=RandomReal[{8,12}];
yball=8;
rball=0.45;
ball =Disk[{xball,yball},rball];
stflag=1;
];

(*球の動きを記述*)
If[stflag==1&&endflag==0,
xball=xball+spdx;
yball=yball+spdy;
ball = Disk[{xball,yball},rball];
];
(*球が出現してから最初にバーに当たるまでの動きの処理*)
If[stflag==1&&endflag==0&&ihit==0,
spdy=-0.2;
If[10-xball>0,spdx=0.1,spdx=-0.1];

];
(*球のヒット判定を記述*)

(*バーの上面との跳ね返り*)
If[(xbar-3<xball)&&(xbar+3>xball)&&(4.55>yball)&&(4<yball)&&endflag==0,
spdy=-spdy;
ihit=ihit+1;
];
(*バーの側面との跳ね返り*)
If[(xball==xbar+3.5)||(xball==xbar-3.5)&&(4.55>yball)&&(4<yball)&&endflag==0,
spdx=-spdx;
];

(*壁との跳ね返り*)
If[(xball<0.45||xball>19.55)&&endflag==0,spdx=-spdx;];
(*天井との跳ね返り*)
If[yball>19.55&&endflag==0,spdy=-spdy;];
(*球が画面下に落ちた時*)
If[yball<0&&endflag==0,
ball=Null;
endflag=1;
];

(*ブロックの設定*)

If[bflag1==0,
block1=Rectangle[{0,17},{4,19}];
];
If[(0.45<xball)&&(xball<3.55)&&(16.55<yball)&&(yball<19.45)&&(endflag==0)&&(bflag1==0),
lx=xball;
ly=yball;
block1=Null;
hit=hit+1;
bflag1=1;
If[(0.45<lx)&&(lx<3.55),
spdy=-spdy,
If[(16.55<ly)&&(ly<19.45),
spdx=-spdx;
];
];

If[!(0.45<lx)&&(lx<3.55)&&(16.55<ly)&&(ly<19.45),
spdx=-spdx;
spdy=-spdy;
];
];

If[bflag2==0,block2=Rectangle[{4,17},{8,19}];];
If[(4.45<xball)&&(xball<7.55)&&(16.55<yball)&&(yball<19.45)&&(endflag==0)&&(bflag2==0),lx=xball;
ly=yball;
block2=Null;
hit=hit+1;
bflag2=1;
If[(4.45<lx)&&(lx<7.55),spdy=-spdy,If[(16.55<ly)&&(ly<19.45),spdx=-spdx;];];

If[!(4.45<lx)&&(lx<7.55)&&(16.55<ly)&&(ly<19.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag3==0,block3=Rectangle[{8,17},{12,19}];];
If[(8.45<xball)&&(xball<11.55)&&(16.55<yball)&&(yball<19.45)&&(endflag==0)&&(bflag3==0),lx=xball;
ly=yball;
block3=Null;
hit=hit+1;
bflag3=1;
If[(8.45<lx)&&(lx<11.55),spdy=-spdy,If[(16.55<ly)&&(ly<19.45),spdx=-spdx;];];

If[!(8.45<lx)&&(lx<11.55)&&(16.55<ly)&&(ly<19.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag4==0,block4=Rectangle[{12,17},{16,19}];];
If[(12.45<xball)&&(xball<15.55)&&(16.55<yball)&&(yball<19.45)&&(endflag==0)&&(bflag4==0),lx=xball;
ly=yball;
block4=Null;
hit=hit+1;
bflag4=1;
If[(12.45<lx)&&(lx<15.55),spdy=-spdy,If[(16.55<ly)&&(ly<19.45),spdx=-spdx;];];

If[!(12.45<lx)&&(lx<15.55)&&(16.55<ly)&&(ly<19.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag5==0,block5=Rectangle[{16,17},{20,19}];];
If[(16.45<xball)&&(xball<19.55)&&(16.55<yball)&&(yball<19.45)&&(endflag==0)&&(bflag5==0),lx=xball;
ly=yball;
block5=Null;
hit=hit+1;
bflag5=1;
If[(16.45<lx)&&(lx<19.55),spdy=-spdy,If[(16.55<ly)&&(ly<19.45),spdx=-spdx;];];

If[!(16.45<lx)&&(lx<19.55)&&(16.55<ly)&&(ly<19.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag6==0,block6=Rectangle[{0,15},{4,17}];];
If[(0.45<xball)&&(xball<3.55)&&(14.55<yball)&&(yball<17.45)&&(endflag==0)&&(bflag6==0),lx=xball;
ly=yball;
block6=Null;
hit=hit+1;
bflag6=1;
If[(0.45<lx)&&(lx<3.55),spdy=-spdy,If[(14.55<ly)&&(ly<17.45),spdx=-spdx;];];

If[!(0.45<lx)&&(lx<3.55)&&(14.55<ly)&&(ly<17.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag7==0,block7=Rectangle[{4,15},{8,17}];];
If[(4.45<xball)&&(xball<7.55)&&(14.55<yball)&&(yball<17.45)&&(endflag==0)&&(bflag7==0),lx=xball;
ly=yball;
block7=Null;
hit=hit+1;
bflag7=1;
If[(4.45<lx)&&(lx<7.55),spdy=-spdy,If[(14.55<ly)&&(ly<17.45),spdx=-spdx;];];

If[!(4.45<lx)&&(lx<7.55)&&(14.55<ly)&&(ly<17.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag8==0,block8=Rectangle[{8,15},{12,17}];];
If[(8.45<xball)&&(xball<11.55)&&(14.55<yball)&&(yball<17.45)&&(endflag==0)&&(bflag8==0),lx=xball;
ly=yball;
block8=Null;
hit=hit+1;
bflag8=1;
If[(8.45<lx)&&(lx<11.55),spdy=-spdy,If[(14.55<ly)&&(ly<17.45),spdx=-spdx;];];

If[!(8.45<lx)&&(lx<11.55)&&(14.55<ly)&&(ly<17.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag9==0,block9=Rectangle[{12,15},{16,17}];];
If[(12.45<xball)&&(xball<15.55)&&(14.55<yball)&&(yball<17.45)&&(endflag==0)&&(bflag9==0),lx=xball;
ly=yball;
block9=Null;
hit=hit+1;
bflag9=1;
If[(12.45<lx)&&(lx<15.55),spdy=-spdy,If[(14.55<ly)&&(ly<17.45),spdx=-spdx;];];

If[!(12.45<lx)&&(lx<15.55)&&(14.55<ly)&&(ly<17.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag10==0,block10=Rectangle[{16,15},{20,17}];];
If[(16.45<xball)&&(xball<19.55)&&(14.55<yball)&&(yball<17.45)&&(endflag==0)&&(bflag10==0),lx=xball;
ly=yball;
block10=Null;
hit=hit+1;
bflag10=1;
If[(16.45<lx)&&(lx<19.55),spdy=-spdy,If[(14.55<ly)&&(ly<17.45),spdx=-spdx;];];

If[!(16.45<lx)&&(lx<19.55)&&(14.55<ly)&&(ly<17.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag11==0,block11=Rectangle[{0,13},{4,15}];];
If[(0.45<xball)&&(xball<3.55)&&(12.55<yball)&&(yball<15.45)&&(endflag==0)&&(bflag11==0),lx=xball;
ly=yball;
block11=Null;
hit=hit+1;
bflag11=1;
If[(0.45<lx)&&(lx<3.55),spdy=-spdy,If[(12.55<ly)&&(ly<15.45),spdx=-spdx;];];

If[!(0.45<lx)&&(lx<3.55)&&(12.55<ly)&&(ly<15.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag12==0,block12=Rectangle[{4,13},{8,15}];];
If[(4.45<xball)&&(xball<7.55)&&(12.55<yball)&&(yball<15.45)&&(endflag==0)&&(bflag12==0),lx=xball;
ly=yball;
block12=Null;
hit=hit+1;
bflag12=1;
If[(4.45<lx)&&(lx<7.55),spdy=-spdy,If[(12.55<ly)&&(ly<15.45),spdx=-spdx;];];

If[!(4.45<lx)&&(lx<7.55)&&(12.55<ly)&&(ly<15.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag13==0,block13=Rectangle[{8,13},{12,15}];];
If[(8.45<xball)&&(xball<11.55)&&(12.55<yball)&&(yball<15.45)&&(endflag==0)&&(bflag13==0),lx=xball;
ly=yball;
block13=Null;
hit=hit+1;
bflag13=1;
If[(8.45<lx)&&(lx<11.55),spdy=-spdy,If[(12.55<ly)&&(ly<15.45),spdx=-spdx;];];

If[!(8.45<lx)&&(lx<11.55)&&(12.55<ly)&&(ly<15.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag14==0,block14=Rectangle[{12,13},{16,15}];];
If[(12.45<xball)&&(xball<15.55)&&(12.55<yball)&&(yball<15.45)&&(endflag==0)&&(bflag14==0),lx=xball;
ly=yball;
block14=Null;
hit=hit+1;
bflag14=1;
If[(12.45<lx)&&(lx<15.55),spdy=-spdy,If[(12.55<ly)&&(ly<15.45),spdx=-spdx;];];

If[!(12.45<lx)&&(lx<15.55)&&(12.55<ly)&&(ly<15.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag15==0,block15=Rectangle[{16,13},{20,15}];];
If[(16.45<xball)&&(xball<19.55)&&(12.55<yball)&&(yball<15.45)&&(endflag==0)&&(bflag15==0),lx=xball;
ly=yball;
block15=Null;
hit=hit+1;
bflag15=1;
If[(16.45<lx)&&(lx<19.55),spdy=-spdy,If[(12.55<ly)&&(ly<15.45),spdx=-spdx;];];

If[!(16.45<lx)&&(lx<19.55)&&(12.55<ly)&&(ly<15.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag16==0,block16=Rectangle[{0,11},{4,13}];];
If[(0.45<xball)&&(xball<3.55)&&(10.55<yball)&&(yball<13.45)&&(endflag==0)&&(bflag16==0),lx=xball;
ly=yball;
block16=Null;
hit=hit+1;
bflag16=1;
If[(0.45<lx)&&(lx<3.55),spdy=-spdy,If[(10.55<ly)&&(ly<13.45),spdx=-spdx;];];

If[!(0.45<lx)&&(lx<3.55)&&(10.55<ly)&&(ly<13.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag17==0,block17=Rectangle[{4,11},{8,13}];];
If[(4.45<xball)&&(xball<7.55)&&(10.55<yball)&&(yball<13.45)&&(endflag==0)&&(bflag17==0),lx=xball;
ly=yball;
block17=Null;
hit=hit+1;
bflag17=1;
If[(4.45<lx)&&(lx<7.55),spdy=-spdy,If[(10.55<ly)&&(ly<13.45),spdx=-spdx;];];

If[!(4.45<lx)&&(lx<7.55)&&(10.55<ly)&&(ly<13.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag18==0,block18=Rectangle[{8,11},{12,13}];];
If[(8.45<xball)&&(xball<11.55)&&(10.55<yball)&&(yball<13.45)&&(endflag==0)&&(bflag18==0),lx=xball;
ly=yball;
block18=Null;
hit=hit+1;
bflag18=1;
If[(8.45<lx)&&(lx<11.55),spdy=-spdy,If[(10.55<ly)&&(ly<13.45),spdx=-spdx;];];

If[!((8.45<lx)&&(lx<11.55)&&(10.55<ly)&&(ly<13.45)),spdx=-spdx;
spdy=-spdy;];];

If[bflag19==0,block19=Rectangle[{12,11},{16,13}];];
If[(12.45<xball)&&(xball<15.55)&&(10.55<yball)&&(yball<13.45)&&(endflag==0)&&(bflag19==0),lx=xball;
ly=yball;
block19=Null;
hit=hit+1;
bflag19=1;
If[(12.45<lx)&&(lx<15.55),spdy=-spdy,If[(10.55<ly)&&(ly<13.45),spdx=-spdx;];];

If[!(12.45<lx)&&(lx<15.55)&&(10.55<ly)&&(ly<13.45),spdx=-spdx;
spdy=-spdy;];];

If[bflag20==0,block20=Rectangle[{16,11},{20,13}];];
If[(16.45<xball)&&(xball<19.55)&&(10.55<yball)&&(yball<13.45)&&(endflag==0)&&(bflag20==0),lx=xball;
ly=yball;
block20=Null;
hit=hit+1;
bflag20=1;
If[(16.45<lx)&&(lx<19.55),spdy=-spdy,If[(10.55<ly)&&(ly<13.45),spdx=-spdx;];];

If[!(16.45<lx)&&(lx<19.55)&&(10.55<ly)&&(ly<13.45),spdx=-spdx;
spdy=-spdy;];];

If[endflag==0,
(*ゲームオーバー前の処理*)
(*バーのグラフィックス*)
bar=Rectangle[{xbar-3,3},{xbar+3,4}];
point=Text[Style[hit,25],{5,1}];
message1=Text[Style[Score,20],{2,1}];
message2=Null;
message3=Null;
];
If[endflag==1,
(*ゲームオーバー後の処理*)
ball=Null;
bar=Null;
point=Null;
message1=Text[Style[GAME OVER,Large],{10,10}];
message2=Text[Style[Score,Large],{10,8}];
message3=Text[Style[hit,Large],{10,6}];
];
If[endflag==2,
ball=Null;
bar=Null;
point=Null;
message1=Text[Style[Congratulations!,Large],{10,10}];
message2=Text[Style[Score,Large],{10,8}];
message3=Text[Style[hit,Large],{10,6}];
];
(*画面設定*)
options={AspectRatio->Automatic,PlotRange->{{0,20},{0,20}}};
(*グラフィック描画処理*)

Graphics[
{
{EdgeForm[Thick],Red,block1},
{EdgeForm[Thick],Red,block2},
{EdgeForm[Thick],Red,block3},
{EdgeForm[Thick],Red,block4},
{EdgeForm[Thick],Red,block5},

{EdgeForm[Thick],Yellow,block6},
{EdgeForm[Thick],Yellow,block7},
{EdgeForm[Thick],Yellow,block8},
{EdgeForm[Thick],Yellow,block9},
{EdgeForm[Thick],Yellow,block10},

{EdgeForm[Thick],Green,block11},
{EdgeForm[Thick],Green,block12},
{EdgeForm[Thick],Green,block13},
{EdgeForm[Thick],Green,block14},
{EdgeForm[Thick],Green,block15},

{EdgeForm[Thick],Blue,block16},
{EdgeForm[Thick],Blue,block17},
{EdgeForm[Thick],Blue,block18},
{EdgeForm[Thick],Blue,block19},
{EdgeForm[Thick],Blue,block20},


{EdgeForm[Thick],White,bar},
{EdgeForm[Thick],White,ball},
point,
message1,
message2,
message3
},
options
],
{t,0,Infinity},
AnimationRate->5,
AnimationRepetitions->1
]
}
]