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);
				SetViewTo(0,-60,50);
				SetViewFrom(500,90,45);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 0, -300);				//実際に表示

				SetFog(200,500,200,200,250);
				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);
				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(stage1alpha3>0)
			{
				SetTexture(csd~"img\cloud.png");		//画像の貼り付け
				SetGraphicRect(0, 0+bgY3  ,2400, 3000 + bgY3);	//描画矩形の設定
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
				SetAlpha(stage1alpha3);
				SetRenderState(ALPHA);
				SetViewTo(0,-60,50);
				SetColor(255,255,255);
				DrawGraphic3D(0,0, 0);
			}

			}
/////////////////////////////////////////////////////////////////////////////////////////////////////
			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);
			}

			}
		}
}

task BGGraphicRect
{
	if(StageProgress==1)
	{
	stage1alpha3=0;
	descent(let i in 0..300)
	{
		if(stage1alpha1<=80)
		{
			stage1alpha1+=80/60;
		}
		if(stage1alpha2<=120)
		{
			stage1alpha2+=120/60;
		}
		if(stage1alpha3<=80)
		{
			stage1alpha3+=40/60;
		}
			if (bgX >= 640)
			{
				bgX = 0;
			}
			if (bgY >= 480)
			{
				bgY = 0;
			}

			if (bgX2 >= 640)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 480)
			{
				bgY2 = 0;
			}
			if (bgY3 >= 512)
			{
				bgY3 = 0;
			}
		if(ViewFromX>0)
		{
			ViewFromX-=300/240;
		}
	bgX += 0.5;
	bgY += 1+18*i/300;
	bgX2 += 0.5;
	bgY2 += 1+18*i/300;
	bgY3 += 1+18*i/300;
	yield;
	}
	stage1scale3=1;	
	}
	while(StageProgress==1)
	{
			if (bgX >= 640)
			{
				bgX = 0;
			}
			if (bgY >= 480)
			{
				bgY = 0;
			}

			if (bgX2 >= 640)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 480)
			{
				bgY2 = 0;
			}
			if (bgY3 >= 512)
			{
				bgY3 = 0;
			}
			if(GetCommonDataDefault("STAGE1MIDBOSS",false))
			{
				if(StageProgress==1)
				{
				bgX += 0.5;
				bgY += 1;
				bgX2 += 0.5;
				bgY2 += 1;
				bgY3 += 1;
				}
				if(BossAdjust>-15)
				{
					BossAdjust-=0.1;
				}
			}
			else if(GetCommonDataDefault("STAGE1BOSS",false))
			{
				bgX += 0.5;
				bgY += 1;
				bgX2 += 0.5;
				bgY2 += 1;
				bgY3 += 1;
				if(ViewFromX<100)
				{
				ViewFromX+=100/120;
				}
				if(BossAdjust>-15)
				{
					BossAdjust-=0.1;
				}
			}	
			else 
			{
				bgX += 0.5;
				bgY += 2;
				bgX2 += 0.5;
				bgY2 += 2;
				bgY3 += 2;
				if(BossAdjust<0)
				{
				BossAdjust+=0.1;
				}
			}
		if(StageChange){break;}
		yield;
	}

	while(StageChange)
	{
			if (bgX >= 640)
			{
				bgX = 0;
			}
			if (bgY >= 480)
			{
				bgY = 0;
			}

			if (bgX2 >= 640)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 480)
			{
				bgY2 = 0;
			}
			if (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;

	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 (bgX >= 480 || bgX <= -480)
			{
				bgX = 0;
			}
			if (bgY >= 480 || bgY <= -480)
			{
				bgY = 0;
			}

			if (bgX2 >= 480 || bgX2 <= -480)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 480 || bgY2 <= -480)
			{
				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 (bgX >= 480 || bgX <= -480)
			{
				bgX = 0;
			}
			if (bgY >= 480 || bgY <= -480)
			{
				bgY = 0;
			}

			if (bgX2 >= 480 || bgX2 <= -480)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 480 || bgY2 <= -480)
			{
				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;
	}

	while(StageProgress==3)
	{
		if(stage3alpha<=150)
		{
			stage3alpha+=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==4)
	{
			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==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-=80/2000;
		yield;
	}
	}

	task Stage1End
	{

	}

	task BGStage2MidBossEnd
	{

	}

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

	task Stage2End
	{

	}