let AAA=0;
let xxx=0;
let yyy=0;
let zzz=0;
sub BackGroundLoop
{
		if(StageState!=Menu && BGDrawing==1)
		{
			if(StageProgress==1)
			{
				SetTexture(csd~"img\st1bg1.png");		//画像の貼り付け
				SetGraphicRect(0+bgX, 0+bgY  ,2400+bgX, 3000 + bgY);	//描画矩形の設定
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
			SetAlpha(stage1alpha1);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 0, -300);				//実際に表示

				SetFog(200,500,180,180,225);
				SetTexture(csd~"img\st1bg1.png");		//画像の貼り付け
				SetGraphicRect(0-bgX2, 0+bgY2  ,2400-bgX2, 3000 + bgY2);	//描画矩形の設定				//実際に表示
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
			SetAlpha(stage1alpha2);

				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0,0, -300);

				SetViewTo(0,-60,50);
				SetViewFrom(600+ViewFromX,90,40+BossAdjust);
				SetFog(200,500,200,200,250);
			if(GetKeyState(VK_RIGHT) == KEY_PUSH || GetKeyState(VK_RIGHT) == KEY_HOLD)
			{
				AAA+=1;
			}
			if(GetKeyState(VK_LEFT) == KEY_PUSH || GetKeyState(VK_LEFT) == KEY_HOLD)
			{
				AAA-=1;
			}
			if(stage1alpha3>0)
			{
				SetTexture(csd~"img\cloud.png");		//画像の貼り付け
				SetGraphicRect(0+bgX3, 0+bgY3  ,2400+bgX3, 3000 + bgY3);	//描画矩形の設定
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
				SetAlpha(stage1alpha3);
				SetRenderState(ALPHA);
				SetColor(255,255,255);
				DrawGraphic3D(0,0, -150);
			}

			}
/////////////////////////////////////////////////////////////////////////////////////////////////////
			if(StageProgress==2)
			{
				SetTexture(csd~"img\st2bg1.png");		//画像の貼り付け
				SetGraphicRect(0+bgX, 0+bgY  ,2400+bgX, 3000 + bgY);	//描画矩形の設定
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
				SetAlpha(stage2alpha1);
				SetViewTo(0,-60,50);
				SetViewFrom(500,90,45);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 0, -300);				//実際に表示

				SetViewFrom(600+ViewFromX,90,40+BossAdjust);
			//	SetFog(200,500,200,200,250);

			if(stage2alpha2>0)
			{
				SetTexture(csd~"img\st2bg2.png");		//画像の貼り付け
				SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);	//描画矩形の設定
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
				SetAlpha(stage2alpha2);
				SetRenderState(ALPHA);
				SetViewTo(0,-60,50);
				SetColor(200,255,255);
				DrawGraphic3D(0,0, -200);
			}

			}

			if(StageProgress==3)
			{
				SetTexture(csd~"img\st3bg1.png");		//画像の貼り付け
				SetGraphicRect(0+bgX, 0+bgY  ,2400+bgX, 3000 + bgY);	//描画矩形の設定
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
				SetAlpha(stage3alpha1);
			//	SetAlpha(0);
				SetViewTo(0,-60,50);
				SetViewFrom(500,90,45);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 0, -300);				//実際に表示
				SetViewFrom(600+ViewFromX,90,25+BossAdjust);

			//	SetFog(200,500,200,200,250);

				SetTexture(csd~"img\cloud2.png");		//画像の貼り付け
				SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);	//描画矩形の設定
				SetGraphicAngle(90, 0, 45);
				SetGraphicScale(2,2);
				SetAlpha(stage3alpha2);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 200, -300);				//実際に表示

				SetTexture(csd~"img\cloud2.png");		//画像の貼り付け
				SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);	//描画矩形の設定
				SetGraphicAngle(90, 0, -45);
				SetGraphicScale(2,2);
				SetAlpha(stage3alpha2);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 200, -300);				//実際に表示

			}
			if(StageProgress==4)
			{

				SetFog(0,25000,stage4fogRGB[0],stage4fogRGB[1],stage4fogRGB[2]);

				SetTexture(csd~"img\st4bg2.png");		//画像の貼り付け
				SetGraphicRect(0, 0  ,640, 580);	//描画矩形の設定
				SetGraphicAngle(90, 0, 0-stage4bg2zangle);
				SetGraphicScale(stage4bg2scale,stage4bg2scale);
				SetAlpha(stage4alpha2);
				SetColor(255,255,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 800, 0);
				if(stage4alpha1>0)
				{
				SetTexture(csd~"img\st4bg1.png");		//画像の貼り付け
				SetGraphicRect(0+bgX, 0+bgY  ,2400+bgX, 3000 + bgY);	//描画矩形の設定
				SetGraphicAngle(-90, 0-65,0 );
				SetGraphicScale(3,3);
				SetAlpha(stage4alpha1);
				SetColor(255,255,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(30, 0, 0);				//実際に表示

				SetTexture(csd~"img\st4bg1.png");		//画像の貼り付け
				SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);	//描画矩形の設定
				SetGraphicAngle(90, 0+180-65, 0);
				SetGraphicScale(3,3);
				SetAlpha(stage4alpha1);
				SetColor(255,255,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(-50, 0, 0);
				}
				SetViewTo(0,-60+stage4bgfromz,50);
				SetViewFrom(500-stage4bgfromz*2.5,90-stage4bgfromy,-90);

				SetTexture(csd~"img\st4bg1.png");		//画像の貼り付け
				SetGraphicRect(0+bgX, 0+bgY  ,2400+bgX, 3000 + bgY);	//描画矩形の設定
				SetGraphicAngle(0+stage4bgfromz, 0-65,0);
				SetGraphicScale(3,3);
				SetAlpha(stage4alpha3);
				SetColor(255,255,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(30, 0, 0);				//実際に表示

				SetTexture(csd~"img\st4bg1.png");		//画像の貼り付け
				SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);	//描画矩形の設定
				SetGraphicAngle(180-stage4bgfromz, 0+180-65, 0);
				SetGraphicScale(3,3);
				SetAlpha(stage4alpha3);
				SetColor(255,255,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(-50-stage4bgLeftLayerX, 0, 0);
			}

		}
}

task BGGraphicRect
{
	if(StageProgress==1)
	{
	stage1alpha3=0;
	descent(let i in 0..240)
	{
		if(stage1alpha1<=80)
		{
			stage1alpha1+=80/60;
		}
		if(stage1alpha2<=120)
		{
			stage1alpha2+=120/60;
		}
		if(stage1alpha3<=40)
		{
			stage1alpha3+=20/60;
		}
		if(absolute(bgX)>=640){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=640){bgX2=0;}
		if(absolute(bgY2)>=480){bgY2=0;}
		if(absolute(bgY3)>=512){bgY3=0;}
		if(ViewFromX>0)
		{
			ViewFromX-=300/240;
		}
	bgX += 0.5;
	bgY += 3+18*i/240;
	bgX2 += 0.5;
	bgY2 += 3+18*i/240;
	bgY3 += 3+18*i/240;

	yield;
	}
	stage1scale3=1;	
	}
	while(StageProgress==1)
	{
		if(absolute(bgX)>=640){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=640){bgX2=0;}
		if(absolute(bgY2)>=480){bgY2=0;}
		if(absolute(bgY3)>=512){bgY3=0;}

			if(GetCommonDataDefault("STAGE1MIDBOSS",false))
			{
				if(StageProgress==1)
				{
				bgX += 0.5;
				bgY += 1+2*stage1BGCount/30;
				bgX2 += 0.5;
				bgY2 += 1+2*stage1BGCount/30;
				bgY3 += 1+2*stage1BGCount/30;
				}
				if(stage1BGCount>0)
				{
					stage1BGCount-=1;
				}		
				if(BossAdjust>-15)
				{
					BossAdjust-=0.1;
				}
			}
			else if(GetCommonDataDefault("STAGE1BOSS",false))
			{
				bgX += 0.5;
				bgY += 1+2*stage1BGCount/30;
				bgX2 += 0.5;
				bgY2 += 1+2*stage1BGCount/30;
				bgY3 += 1+2*stage1BGCount/30;
				if(ViewFromX<100)
				{
				ViewFromX+=100/120;
				}
				if(stage1BGCount>0)
				{
					stage1BGCount-=1;
				}
				if(BossAdjust>-15)
				{
					BossAdjust-=0.1;
				}
			}	
			else 
			{
				bgX += 0.5;
				bgY += 3;
				bgX2 += 0.5;
				bgY2 += 3;
				bgY3 += 3;
				if(stage1BGCount<30)
				{
					stage1BGCount+=1;
				}
				if(BossAdjust<0)
				{
				BossAdjust+=0.1;
				}
			}
		if(StageChange){break;}
/*
	bgX+=BGWideRectWithPlayerMove(1);
	bgX2-=BGWideRectWithPlayerMove(1);
	bgX3+=BGWideRectWithPlayerMove(1);

	bgY+=BGLongRectWithPlayerMove(1);
	bgY2+=BGLongRectWithPlayerMove(1);
	bgY3+=BGLongRectWithPlayerMove(1);
*/
		yield;
	}

	while(StageChange)
	{
		if(absolute(bgX)>=640){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=640){bgX2=0;}
		if(absolute(bgY2)>=480){bgY2=0;}
		if(absolute(bgY3)>=512){bgY3=0;}

				bgX += 0.5;
				bgY += 1;
				bgX2 += 0.5;
				bgY2 += 1;
				bgY3 += 1;
				if(ViewFromX<100)
				{
				ViewFromX+=100/120;
				}
				if(BossAdjust>-15)
				{
					BossAdjust-=0.1;
				}

		if(stage1alpha1>0)
		{
			stage1alpha1-=80/240;
		}
		if(stage1alpha2>0)
		{
			stage1alpha2-=120/240;
		}
		yield;
	}

	bgX=0;
	bgX2=300;
	bgY=0;
	bgY2=300;
	bgY3=300;
	BossAdjust=0;
	ViewFromX=0;

	let st2bg1angle=0;
	let MidBossEscape=false;

	if(StageProgress==2)
	{
	while(stage2alpha1<120)
	{
		stage2alpha1+=120/120;
		bgX += 1.0*cos(st2bg1angle);
		bgY += 1+1.0*cos(st2bg1angle);
		bgX2 += 1.0*cos(st2bg1angle);
		bgY2 += 1+1.0*cos(st2bg1angle);
		yield;
	}
	}
	while(StageProgress==2)
	{
		if(absolute(bgX)>=480){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=478){bgX2=0;}
		if(absolute(bgY2)>=478){bgY2=0;}

			if(GetCommonDataDefault("STAGE2MIDBOSS",false))
			{
				bgX += 1.0*cos(st2bg1angle);
				bgY += 1+1.0*cos(st2bg1angle);
				bgX2 += 1.0*cos(st2bg1angle);
				bgY2 += 1+1.0*cos(st2bg1angle);

				if(st2bg1angle<90)
				{
					st2bg1angle+=0.5;
				}
				MidBossEscape=true;
			}
			else if(GetCommonDataDefault("STAGE2BOSS",false))
			{
				bgX += 1.0*cos(st2bg1angle);
				bgY += 1+1.0*cos(st2bg1angle);
				bgX2 += 1.0*cos(st2bg1angle);
				bgY2 += 1+1.0*cos(st2bg1angle);

				if(st2bg1angle>90)
				{
					st2bg1angle-=0.5;
				}

			}	
			else 
			{
				if(MidBossEscape)
				{
				bgX += 1.0*cos(st2bg1angle);
				bgY += 1-1.0*cos(st2bg1angle);
				bgX2 += 1.0*cos(st2bg1angle);
				bgY2 += 1-1.0*cos(st2bg1angle);
				}
				else
				{
				bgX += 1.0*cos(st2bg1angle);
				bgY += 1+1.0*cos(st2bg1angle);
				bgX2 += 1.0*cos(st2bg1angle);
				bgY2 += 1+1.0*cos(st2bg1angle);
				}
				if(st2bg1angle<180 && MidBossEscape)
				{
					st2bg1angle+=0.5;
				}
			}
		if(StageChange){break;}
		yield;
	}

	while(StageChange)
	{
		if(absolute(bgX)>=480){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=478){bgX2=0;}
		if(absolute(bgY2)>=478){bgY2=0;}

				bgX += 1.0*cos(st2bg1angle);
				bgY += 1+1.0*cos(st2bg1angle);
				bgX2 += 1.0*cos(st2bg1angle);
				bgY2 += 1+1.0*cos(st2bg1angle);

		if(stage2alpha1>0)
		{
			stage2alpha1-=160/240;
		}
		if(stage2alpha2>0)
		{
			stage2alpha2-=120/240;
		}
		yield;
	}

	MidBossEscape=false;
	let st3bgXspeed=7;
	stage3alpha2=60;
	if(StageProgress==3)
	{
	while(stage3alpha1<200)
	{
		stage3alpha1+=200/120;
	//	bgX += 1.0*cos(st2bg1angle);
		bgY += 7;
	//	bgX2 += 1.0*cos(st2bg1angle);
		bgY2 -= 7;
		if(absolute(bgX)>=480){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=400){bgX2=0;}
		if(absolute(bgY2)>=400){bgY2=0;}

		yield;
	}
	}
	while(StageProgress==3)
	{
		if(absolute(bgX)>=480){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=400){bgX2=0;}
		if(absolute(bgY2)>=400){bgY2=0;}

			if(GetCommonDataDefault("STAGE3MIDBOSS",false))
			{
			//	bgX += 1.0*cos(st2bg1angle);
				bgY += st3bgXspeed;
			//	bgX2 += 1.0*cos(st2bg1angle);
				bgY2 -= st3bgXspeed;

				if(st2bg1angle<90)
				{
					st2bg1angle+=0.5;
				}
				if(st3bgXspeed>1)
				{
				st3bgXspeed-=0.05;
				}
				MidBossEscape=true;
			}
			else if(GetCommonDataDefault("STAGE3BOSS",false))
			{
			//	bgX += 1.0*cos(st2bg1angle);
				bgY += st3bgXspeed;
			//	bgX2 += 1.0*cos(st2bg1angle);
				bgY2 -= st3bgXspeed;

				if(st3bgXspeed>1)
				{
				st3bgXspeed-=0.05;
				}
				if(BossAdjust>-20)
				{
					BossAdjust-=0.1;
				}
			}	
			else 
			{
			//	bgX += 1.0*cos(st2bg1angle);
				bgY += st3bgXspeed;
			//	bgX2 += 1.0*cos(st2bg1angle);
				bgY2 -= st3bgXspeed;
				if(st2bg1angle<180 && MidBossEscape)
				{
					st2bg1angle+=0.5;
				}
				if(st3bgXspeed<7)
				{
				st3bgXspeed+=0.05;
				}
				if(MidBossEscape==true)
				{
					if(stage3alpha1>100)
					{
						stage3alpha1-=100/1500;
					}
				}
			}
		if(StageChange){break;}
		yield;
	}

	while(StageChange)
	{
		if(absolute(bgX)>=480){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=400){bgX2=0;}
		if(absolute(bgY2)>=400){bgY2=0;}

			//	bgX += 1.0*cos(st2bg1angle);
				bgY += st3bgXspeed;
			//	bgX2 += 1.0*cos(st2bg1angle);
				bgY2 -= st3bgXspeed;

		if(stage3alpha1>0)
		{
			stage3alpha1-=100/240;
		}
		if(stage3alpha2>0)
		{
			stage3alpha2-=60/240;
		}
		yield;
	}
	if(StageProgress==4)
	{
		if(StagePhaseProgress<=0 || StagePractice==false)
		{
			loop(1620)
			{
				if(stage4alpha1<120)
				{
					stage4alpha1+=120/120;
				}
				bgY += 5;
			bgY2 -= 5;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				yield;
			}
			loop(480)
			{
				stage4bgfromy+=0.1;
				stage4alpha1-=120/480;
				stage4alpha3+=255/480;
				bgY += 5;
				bgY2 -= 5;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				yield;
			}
		}
		if(StagePhaseProgress==1 && StagePractice==true)
		{
			Stage4BGPhase1;
		}
		if(StagePhaseProgress<=1 || StagePractice==false)
		{
			loop
			{
				stage4bgfromy+=0.1;
				bgY += 5;
				bgY2 -= 5;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				yield;
				if(GetCommonDataDefault("STAGE4MIDBOSS",false)){break;}
			}

			loop
			{
				stage4bgfromy+=0.1;
				bgY += 5;
				bgY2 -= 5;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				if(stage4fogRGB[1]>205)
				{
				stage4fogRGB[1]=stage4fogRGB[1]-0.2;
				stage4fogRGB[2]=stage4fogRGB[2]-0.2;
				}
				if(!GetCommonDataDefault("STAGE4MIDBOSS",false)){break;}
				yield;
			}
		}
		if(StagePhaseProgress==2 && StagePractice==true)
		{
			Stage4BGPhase2;
		}
		if(StagePhaseProgress<=2 || StagePractice==false)
		{
			Adjuststage4bgfromy=stage4bgfromy%360;
				
			loop(900)
			{
				stage4bgfromy+=0.1;
				stage4bgfromy+=(290-Adjuststage4bgfromy)/4400;
				bgY += 5;
					bgY2 -= 5;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				if(stage4fogRGB[1]<255)
				{
				stage4fogRGB[1]=stage4fogRGB[1]+0.2;
				stage4fogRGB[2]=stage4fogRGB[2]+0.2;
				}
				yield;
			}
		}
		if(StagePhaseProgress==2 && StagePractice==true)
		{
			Stage4BGPhase2;
		}
		if(StagePhaseProgress==3 && StagePractice==true)
		{
			Stage4BGPhase3;
		}
			loop
			{
				stage4bg2zangle+=1;
				stage4bgfromy+=0.1;
				stage4bgfromy+=(290-Adjuststage4bgfromy)/4400;
				bgY += 5;
				bgY2 -= 5;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				if(stage4fogRGB[1]>205)
				{
					stage4fogRGB[1]=stage4fogRGB[1]-0.1;
				}
				if(stage4alpha2<50)
				{
					stage4alpha2+=0.02;
				}
				if(stage4bg2scale<2)
				{
					stage4bg2scale+=2/2500;
				}
				if(GetCommonDataDefault("STAGE4BOSS",false)){break;}
				yield;
			}
		loop(150)
		{
			stage4bg2zangle+=1;
			stage4bgfromy+=0.1;
			if(stage4bgfromz<90)
			{
			stage4bgfromz+=0.8;
			stage4bgLeftLayerX+=8.0;
			}
			bgY += 5;
			bgY2 -= 5;
			if(absolute(bgX)>=400){bgX=0;}
			if(absolute(bgY)>=400){bgY=0;}
			if(absolute(bgX2)>=400){bgX2=0;}
			if(absolute(bgY2)>=400){bgY2=0;}
			yield;
		}
		let fromzplus=0.5;
		stage4bgLeftLayerX/=2;
		loop(240)
		{
			stage4bg2zangle-=1;
			stage4bgfromy-=0.1;
			if(stage4bgfromz<180)
			{
			stage4bgfromz+=fromzplus;
			stage4bgLeftLayerX-=fromzplus*10/2;
			}
			if(fromzplus>1)
			{
				fromzplus-=0.5/120;
			}
			bgY += 5;
			bgY2 -= 5;
			if(absolute(bgX)>=400){bgX=0;}
			if(absolute(bgY)>=400){bgY=0;}
			if(absolute(bgX2)>=400){bgX2=0;}
			if(absolute(bgY2)>=400){bgY2=0;}
			yield;
		}

		loop
		{
			stage4bg2zangle-=1;
			stage4bgfromy-=0.1;
			bgY += 5;
			bgY2 -= 5;
			if(absolute(bgX)>=400){bgX=0;}
			if(absolute(bgY)>=400){bgY=0;}
			if(absolute(bgX2)>=400){bgX2=0;}
			if(absolute(bgY2)>=400){bgY2=0;}
			if(StageProgress!=4){break;}
			if(StageChange){break;}
			yield;
		}
	}

	while(StageChange)
	{
		stage4bg2zangle-=1;
		stage4bgfromy-=0.1;
		bgY += 5;
		bgY2 -= 5;
		if(absolute(bgX)>=400){bgX=0;}
		if(absolute(bgY)>=400){bgY=0;}
		if(absolute(bgX2)>=400){bgX2=0;}
		if(absolute(bgY2)>=400){bgY2=0;}

		if(stage4alpha1>0)
		{
			stage4alpha1-=255/240;
		}
		if(stage4alpha2>0)
		{
			stage4alpha2-=50/240;
		}
		if(stage4alpha3>0)
		{
			stage4alpha3-=255/240;
		}
		yield;
	}

	while(StageProgress==5)
	{
		if(stage5alpha1<=100)
		{
			stage5alpha1+=100/240;
		}
		if(stage5alpha2<=150)
		{
			stage5alpha2+=150/240;
		}
			bgX += 1;
			if (bgX >= 6000)
			{
				bgX = 0;
			}
			bgX2 += 1;
			if (bgX >= 6000)
			{
				bgX2 = 0;
			}
				bgrt += 1;
				if(bgrt > 512){bgrt = bgrt - 512;}
				if(bgrt <   0){bgrt = bgrt + 512;}
		yield;
	}
	while(StageProgress>=6)
	{
		if(stage6alpha1<=120)
		{
			stage6alpha1+=120/240;
		}
		if(stage6alpha2<=80)
		{
			stage6alpha2+=80/240;
		}
			bgX += 1;
			if (bgX >= 6000)
			{
				bgX = 0;
			}
			bgX2 += 1;
			if (bgX >= 6000)
			{
				bgX2 = 0;
			}
				bgrt += 1;
				if(bgrt > 512){bgrt = bgrt - 512;}
				if(bgrt <   0){bgrt = bgrt + 512;}
		yield;
	}
}
	task BGStage1MidBossEnd
	{
	while(stage1alpha3>0)
	{
		stage1alpha3-=40/2000;
		yield;
	}
	}

	task Stage1End
	{

	}

	task BGStage2MidBossEnd
	{

	}

	task Stage2Bgshift
	{
		loop(240)
		{
		stage2alpha2+=120/240;
		yield;
		}
	}

	task Stage2End
	{

	}


	task Stage3End
	{

	}

	task BGStage3MidBossEnd
	{

	}

	task Stage4End
	{

	}

	task BGStage4MidBossEnd
	{

	}

	task Stage4BGPhase1
	{
		loop(120)
		{
			stage4alpha3+=255/120;
			yield;
		}
	}

	task Stage4BGPhase2
	{
		loop(120)
		{
			stage4alpha3+=255/120;
			yield;
		}
	}

	task Stage4BGPhase3
	{
		stage4fogRGB[1]=205;
		stage4bg2scale=2;
		stage4alpha2=0;
		loop(120)
		{
			stage4alpha3+=255/120;
			yield;
		}
		loop(120)
		{
			stage4alpha2+=50/120;
			yield;
		}
	}

function BGLongRectWithPlayerMove(let BGLongMove)
{
	if((KeyStateUp=="PUSH" || KeyStateUp=="HOLD"))
	{
		if(KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD")
		{
			return(-BGLongMove*2^0.5);
		}
		else if(KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD")
		{
			return(-BGLongMove*2^0.5);
		}
		else
		{
			return(-BGLongMove);
		}
	}
	else if((KeyStateDown=="PUSH" || KeyStateDown=="HOLD"))
	{
		if(KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD")
		{
			return(+BGLongMove*2^0.5);
		}
		else if(KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD")
		{
			return(+BGLongMove*2^0.5);
		}
		else
		{
			return(+BGLongMove);
		}
	}
}

function BGWideRectWithPlayerMove(let BGWideMove)
{
	if((KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD"))
	{
		if(KeyStateUp=="PUSH" || KeyStateUp=="HOLD")
		{
			return(-BGWideMove*2^0.5);
		}
		else if(KeyStateDown=="PUSH" || KeyStateDown=="HOLD")
		{
			return(-BGWideMove*2^0.5);
		}
		else
		{
			return(-BGWideMove);
		}
	}
	else if((KeyStateRight=="PUSH" || KeyStateRight=="HOLD"))
	{
		if(KeyStateUp=="PUSH" || KeyStateUp=="HOLD")
		{
			return(BGWideMove*2^0.5);
		}
		else if(KeyStateDown=="PUSH" || KeyStateDown=="HOLD")
		{
			return(BGWideMove*2^0.5);
		}
		else
		{
			return(BGWideMove);
		}
	}
}