DynamicModule[{
startFlag=0,
endFlag=0
},
{
Button[Reset,ser@Write["B"];gameFlag=0;endFlag=0;],
Animate[
If[gameFlag==0,
enemyFlag=0;
gameFlag=1;
count=0;
speedCount=0;
];
(*プレイヤー*)
playerX=MousePosition["Graphics",{0,0}][[1]];
playerY=MousePosition["Graphics",{0,0}][[2]];
player=Disk[{playerX,playerY},5];
(*敵*)
(*スピードアップ*)
If[count==10&&speedCount==0,speedCount++;];
If[count==20&&speedCount==1,speedCount++;];
If[count==30&&speedCount==2,speedCount++;];
If[count==40&&speedCount==3,speedCount++;];
If[count==50&&speedCount==4,speedCount++;];
If[count==60&&speedCount==5,speedCount++;];
If[count==70&&speedCount==6,speedCount++;];
If[count==80&&speedCount==7,speedCount++;];
If[count==90&&speedCount==8,speedCount++;];
(*出現*)
If[enemyFlag==0&&endFlag==0,
enemyX=RandomInteger[{-95,95}];
enemyY=RandomChoice[{-105,105}];
If[enemyY<0,enemySpeed=0.5+speedCount*0.1,enemySpeed=-0.5-speedCount*0.1];
enemyFlag=1;
];
(*移動*)
If[enemyFlag==1&&endFlag==0,
enemyY=enemyY+enemySpeed;
enemy=Disk[{enemyX,enemyY},5];
];
(*あたり判定*)
If[enemyFlag==1&&(playerX-enemyX)^2+(playerY-enemyY)^2<100,
enemy=Null;
enemyFlag=0;
count++;
ser@Write["A"];
];
(*画面外に出て消える*)
If[enemyY≤-110&&enemyY≥110,
enemy=Null;
enemyFlag=0;
endFlag=1;
];
(*描画*)
Graphics[{player,enemy},{AspectRatio->Automatic,PlotRange->{{-100,100},{-100,100}},Frame->True,FrameTicks->None}],
{t,0,Infinity},
AnimationRate->5,
AnimationRepetitions->1]
}
]

Needs["NETLink`"];
InstallNET[];
LinkObject[]
NETTypeInfo["System.IO.Ports.SerialPort"];
ser=NETNew["System.IO.Ports.SerialPort","COM4",9600];
«NETObject[System.IO.Ports.SerialPort]»

ser@Open[]

ser@Close[]