DynamicModule[{
y,
life,
prelife,
nextlife,
count,
screen,
difficulty=2,
stFlag1,
stFlag2,
stFlag3,
missFlag,
appTime1,
appTime2,
appTime3,
missTime,
rem,
itFlag=0
},
{Animate[
If[difficulty==3,{a1,a2}={0.2,0.4},{a1,a2}={0.3,0.6}];
b0=Switch[difficulty,1,0.2,2,0.4,3,0.8];
c1=a1-0.1;c2=a2-0.1;
speed=If[difficulty==1,0.8,1];

If[itFlag==0,
screen=0;
stFlag1=0;
stFlag2=0;
stFlag3=0;
appTime1=0;
appTime2=0;
appTime3=0;
itFlag=2;
];

If[itFlag==1,
y=0;
life=10;
prelife=10;
nextlife=10;
count=0;
screen=1;
stFlag1=0;
stFlag2=0;
stFlag3=0;
missFlag=0;
appTime1=0;
appTime2=0;
appTime3=0;
missTime=0;
rem=6.8;
itFlag=2;
];

If[stFlag1==0&&(screen==0||screen==1),
appTime1=t;
difft1=t-appTime1;
rate1=RandomReal[{speed-0.2,speed+0.2}];
std1=RandomReal[If[screen==1,{-3+a1,3-a1},{-4,4}]];
centerX1=10-rate1 difft1;
centerY1=std1;
sikaku1={Yellow,Rectangle[{centerX1-a2,centerY1-a1},{centerX1+a2,centerY1+a1}]};
stFlag1=1;
];

If[stFlag1==1&&(screen==0||screen==1),
difft1=t-appTime1;
amp=3-(Abs[std1]+a1);
ampmax=Switch[difficulty,1,0.5,2,1,3,1];
centerX1=10-rate1 difft1;
centerY1=std1+RandomReal[{0,Min[amp,ampmax]}] Sin[difft1];
sikaku1={Yellow,Rectangle[{centerX1-a2,centerY1-a1},{centerX1+a2,centerY1+a1}]};
];

If[stFlag2==0&&(screen==0||screen==1),
appTime2=t;
difft2=t-appTime2;
rate2=RandomReal[{1.5 speed-0.2,1.5 speed+0.2}];
b=RandomReal[{b0-0.1,b0+0.1}];
centerX2=10-rate2 difft2;
centerY2=RandomReal[If[screen==1,{-3+b,3-b},{-4,4}]];
sikaku2=Rectangle[{centerX2-b,centerY2-b},{centerX2+b,centerY2+b}];
stFlag2=1;
];

If[stFlag2==1&&(screen==0||screen==1),
difft2=t-appTime2;
centerX2=10-rate2 difft2;
sikaku2=Rectangle[{centerX2-b,centerY2-b},{centerX2+b,centerY2+b}];
];

If[((life<=4&&screen==1)||screen==0)&&stFlag3==0,
appTime3=t;
difft3=t-appTime3;
centerX3=10-0.5difft3;
centerY3=RandomReal[If[screen==1,{-3+c1,3-c1},{-4,4}]];
sikaku3={Green,Rectangle[{centerX3-c2,centerY3-c1},{centerX3+c2,centerY3+c1}]};
stFlag3=1;
];

If[((life<=4&&screen==1)||screen==0)&&stFlag3==1,
difft3=t-appTime3;
centerX3=10-0.5 difft3;
sikaku3={Green,Rectangle[{centerX3-c2,centerY3-c1},{centerX3+c2,centerY3+c1}]};
];

If[life>4&&screen==1,
sikaku3={};
stFlag3=0;
];

If[centerX1+a2<-1.7&&screen==0,
stFlag1=0;
];

If[centerX2+b<-1.7&&screen==0,
stFlag2=0;
];

If[centerX3+a2<-1.7&&screen==0,
stFlag3=0;
];

If[(centerX1-a2<0.2)&&(y-0.2<centerY1+a1)&&(y+0.2>centerY1-a1)&&screen==1,
count++;
stFlag1=0;
];

If[centerX1+a2<0&&screen==1,
prelife=nextlife;
nextlife-=2;
stFlag1=0;
If[missFlag==0,missTime=t];
missFlag=1;
];

If[centerX2+b<0&&screen==1,
stFlag2=0;
];

If[(centerX2-b<0.2)&&(y-0.2<centerY2+b)&&(y+0.2>centerY2-b)&&screen==1,
prelife=nextlife;
nextlife-=3;
stFlag2=0;
If[missFlag==0,missTime=t];
missFlag=1;
];

If[centerX3+c2<0&&screen==1,
stFlag3=0;
];

If[(centerX3-c2<0.2)&&(y-0.2<centerY3+c1)&&(y+0.2>centerY3-c1)&&screen==1,
prelife=nextlife;
nextlife+=2.5;
stFlag3=0;
If[missFlag==0,missTime=t];
missFlag=1;
];

If[missFlag==1&&screen==1,
diflifeSign=Sign[nextlife-prelife];
life+=0.1diflifeSign (t-missTime);
rem1=-1.2+0.8life;
rem2=-1.2+0.8nextlife;
If[ diflifeSign (life-nextlife)<0,rem=rem1,rem=rem2;life=nextlife;missFlag=0];
If[rem1<=-1.2,
overTime=t;
screen=2];
];

If[screen==2,
difft=t-overTime;
maru1=Circle[{0,y},0.2];
If[(difft>1.5&&difft<2.5)||(difft>3.5&&difft<4.5)||(difft>5.5&&difft<6.5)||(difft>7.5&&difft<8.5)||difft>9.5,maru1={}];
If[difft>12,overTime=t;screen=3];
];

rule={{Opacity[0.75],White,Rectangle[{-1.8,-4.1},{10.1,4.1}]},Text[Style["説明",20],{4.15,3}],Text[Style["・円を左の4つのボタンを押して上下に動かす                         \n・右から流れる黄色の長方形に円を当てると得点になる         \n・体力は左上の緑色のバーで確認でき、最初は10になっている\n・黄色の長方形を取り損ねると体力が2減る                        \n・黒色の正方形に当たると体力が3減る                              \n・体力の少ない時に流れる緑色の長方形に円を当てると、     \n得点にならないが体力が2.5増える                                  \n・体力が0になるとゲームオーバーになる                                  ",13],{4.15,0.3}]};
easy1={{Blue,Rectangle[{0.2,-3.4},{1.8,-2.6}]},Text[Style["Easy",White,20],{1,-3}]};
easy2={{EdgeForm[Blue],White,Rectangle[{0.2,-3.4},{1.8,-2.6}]},Text[Style["Easy",Blue,20],{1,-3}]};
normal1={{Blue,Rectangle[{3,-3.4},{5.3,-2.6}]},Text[Style["Normal",White,20],{4.15,-3}]};
normal2={{EdgeForm[Blue],White,Rectangle[{3,-3.4},{5.3,-2.6}]},Text[Style["Normal",Blue,20],{4.15,-3}]};
hard1={{Blue,Rectangle[{6.5,-3.4},{8.1,-2.6}]},Text[Style["Hard",White,20],{7.3,-3}]};
hard2={{EdgeForm[Blue],White,Rectangle[{6.5,-3.4},{8.1,-2.6}]},Text[Style["Hard",Blue,20],{7.3,-3}]};

maru=Circle[{0,y},0.2];
lifebar={Rectangle[{-1.2,3.3},{6.8,3.7}],{Green,Rectangle[{-1.2,3.3},{rem,3.7}]}};
score=Text[Style["Score:"<>ToString[count],20],{8.5,3.5}];
btn={Rectangle[{-1.7,0},{-0.2,1.5}],Rectangle[{-1.7,1.5},{-0.2,3}],Rectangle[{-1.7,-1.5},{-0.2,0}],Rectangle[{-1.7,-3},{-0.2,-1.5}]};
up1={Mouseover[{White,btn[[1]]},{LightGray,btn[[1]]}],{Red,Opacity[0.65],Polygon[{{-0.95,1.3},{-1.25,0.9},{-1.09,0.9},{-1.09,0.2},{-0.81,0.2},{-0.81,0.9},{-0.65,0.9}}]}};
up2={Mouseover[{White,btn[[2]]},{LightGray,btn[[2]]}],{Red,Polygon[{{-1.325,2.8},{-1.6,2.4},{-1.45,2.4},{-1.45,1.7},{-1.2,1.7},{-1.2,2.4},{-1.05,2.4}}]},
{Red,Polygon[{{-0.575,2.8},{-0.85,2.4},{-0.7,2.4},{-0.7,1.7},{-0.45,1.7},{-0.45,2.4},{-0.3,2.4}}]}};
down1={Mouseover[{White,btn[[3]]},{LightGray,btn[[3]]}],{Blue,Opacity[0.65],Polygon[{{-0.95,-1.3},{-1.25,-0.9},{-1.09,-0.9},{-1.09,-0.2},{-0.81,-0.2},{-0.81,-0.9},{-0.65,-0.9}}]}};
down2={Mouseover[{White,btn[[4]]},{LightGray,btn[[4]]}],{Blue,Polygon[{{-1.325,-2.8},{-1.6,-2.4},{-1.45,-2.4},{-1.45,-1.7},{-1.2,-1.7},{-1.2,-2.4},{-1.05,-2.4}}]},
{Blue,Polygon[{{-0.575,-2.8},{-0.85,-2.4},{-0.7,-2.4},{-0.7,-1.7},{-0.45,-1.7},{-0.45,-2.4},{-0.3,-2.4}}]}};
line={Line[{{-0.2,3},{-0.2,-3}}],Line[{{-1.7,3},{10,3}}],Line[{{-1.7,-3},{10,-3}}],{Thin,Line[{{-1.7,0},{-0.2,0}}]},{Thin,Line[{{-1.7,1.5},{-0.2,1.5}}]},{Thin,Line[{{-1.7,-1.5},{-0.2,-1.5}}]}};
restart1={{Blue,Rectangle[{0.1,-3.8},{1.9,-3.2}]},Text[Style["やり直す",White,15],{1,-3.5}]};
restart2={{EdgeForm[Blue],White,Rectangle[{0.1,-3.8},{1.9,-3.2}]},Text[Style["やり直す",Blue,15],{1,-3.5}]};
title1={{Blue,Rectangle[{4.3,-3.8},{8.2,-3.2}]},Text[Style["タイトル画面に戻る",White,15],{6.25,-3.5}]};
title2={{EdgeForm[Blue],White,Rectangle[{4.3,-3.8},{8.2,-3.2}]},Text[Style["タイトル画面に戻る",Blue,15],{6.25,-3.5}]};

gameover={Rectangle[{-1.8,-4.1},{10.1,4.1}],Text[Style["GameOver",White,Large],{4.15,1.5}],Text[Style["Score:"<>ToString[count],Yellow,15],{4.15,0.5}]};
If[(t-overTime>0&&t-overTime<1)||(t-overTime>2&&t-overTime<3)||(t-overTime>4&&t-overTime<5)||(t-overTime>6&&t-overTime<7),gameover[[3]]={}];

options={AspectRatio->0.7,PlotRange->{{-1.7,10},{-4,4}}};

Graphics[
Switch[screen,
0,{sikaku3,sikaku2,sikaku1,rule,Button[Mouseover[easy1,easy2],{difficulty=1,itFlag=1}],Button[Mouseover[normal1,normal2],{difficulty=2,itFlag=1}],Button[Mouseover[hard1,hard2],{difficulty=3,itFlag=1}]},
1,{maru,sikaku3,sikaku2,sikaku1,lifebar,score,Button[up1,If[y<=2.6,y+=0.2]],Button[up2,If[y<=1.4,y+=1.4,y=2.8]],Button[down1,If[y>=-2.6,y-=0.2]],Button[down2,If[y>=-1.4,y-=1.4,y=-2.8]],line,Button[Mouseover[restart1,restart2],itFlag=1],Button[Mouseover[title1,title2],itFlag=0]},
2,{maru1,sikaku3,sikaku2,sikaku1,lifebar[[1]],score,{White,btn},line},
3,{gameover,Mouseover[Translate[restart1,{1,2.5}],Button[Translate[restart2,{1,2.5}],itFlag=1]],Mouseover[Translate[title1,{-1,2.5}],Button[Translate[title2,{-1,2.5}],itFlag=0]]}],
options],{t,0,Infinity},AnimationRate->3,AnimationRepetitions->1]
}
]