DynamicModule[{
gameflag,
shtime,
stflag=1,
shflag,
reflag=1,
coflag,
lim,
xhansya=1,
yhansya=1,
hanflag=0,
flag=0,
flag1=0,
flag2=0,
flag3=0,
flag4=0,
x=3,
y=1,
},
{
Button[restart,{stflag=1,reflag=1}],
Button[shoot,shflag=1],
Animate[
(*game*)
If[stflag==1,

ball=Disk[{3,1},0.2];

tx=RandomReal[{1,5}];
ty=RandomReal[{4,7}];

target=Disk[{tx,ty},0.5];

gameflag=0;
shflag=0;
shtime=0;
coflag=0;
lim=4;

(*Reset*)
If[reflag==1,
x=3;y=1;lim=4;
gover=Text[Style["",Large],{3,1}];
num=Text[Style[lim,Red,Large],{1,2}];
clear=Text[Style["",Large],{5,5}];

reflag=0;
];
stflag=0;
];
(*shoot*)
If[shflag==1&&gameflag==0,
shtime=t;
gameflag=1;
coflag=1;
];


(*軌道*)
If[shflag==1&&gameflag==1,
dx=Cos[rad]*xhansya*(t-shtime)*10;
dy=Sin[rad]*yhansya*(t-shtime)*10;

If[flag1==1,
x=5.8;flag1=0;];
If[flag2==1,
x=0.2;flag2=0;];
If[flag3==1,
y=7.8;flag3=0;];
If[flag4==1,
y=0.2;flag4=0;];
x+=dx;
y+=dy;
ball=Disk[{x,y},0.2];
];

If[(x+0.2<6||x-0.2>0||y+0.2<8||y-0.2>0)&&flag==1&&shflag==1,
lim-=1;flag=0;
]




(*壁にhit*)
If[(x+0.2>6||x-0.2<0||y+0.2>8||y-0.2<0)&&shflag==1&&flag==0,
flag=1;
(*反射*)
If[x+0.2>6,
If[xhansya==1&&hanflag==0,
xhansya=-1;hanflag=1;flag1=1;];
If[xhansya==-1&&hanflag==0,
xhansya=1;hanflag=1;flag1=1;];
hanflag=0;
];
If[x-0.2<0,
If[xhansya==1&&hanflag==0,
xhansya=-1;hanflag=1;flag2=1;];
If[xhansya==-1&&hanflag==0,
xhansya=1;hanflag=1;flag2=1;];
hanflag=0;
];

If[y-0.2<0,
If[yhansya==1&&hanflag==0,
yhansya=-1;hanflag=1;flag4=1];
If[yhansya==-1&&hanflag==0,
yhansya=1;hanflag=1;flag4=1];
hanflag=0;
];

If[y+0.2>8,
If[yhansya==1&&hanflag==0,
yhansya=-1;hanflag=1;flag3=1;];
If[yhansya==-1&&hanflag==0,
yhansya=1;hanflag=1;flag3=1;];
hanflag=0;
];

];
num=Text[Style[lim,Red,Large],{1,2}];
(*target hit*)
If[Sqrt[(tx-x)^2+(ty-y)^2]<=0.7,
clear=Text[Style["clear",Large],{5,5}];
shflag=0;
];
If[lim==0&&shflag==1,
gover=Text[Style["gameover",Large],{3,1}];
shflag=0;
];


shtime=t;
(*描画*)
Graphics[{ball,target,gover,clear,num},
PlotRange->{{0,6},{0,8}}],
{t,0,Infinity},
{rad,0,Pi*2},
AnimationRate->1,
AnimationRepetitions->1,
AnimationRunning->False
]}]





(MessageTemplate[Syntax, bktmop, "式\"\!\(\*RowBox[{RowBox[{\"«\", \"1\", \"»\"}], \"]\"}]\)\"には開き\"\!\(\*RowBox[{\"\\\"[\\\"\"}]\)\"がありません\!\(\*RowBox[{\"\\\"\\\"\"}]\)．", , , , , Local]);;;;