// title screen
procedure TitleScreen();
var
	titleText : string = (214,201,195);
	hewcoText : string = (200,197,215,195,207,28,213,203,63,50,48,49,57);
	rockText  : string = (210,207,195,203,28,207,204,193,63,49,57,56,50);
	titlegfx : array[] of byte = (
		$0a,$06,$0b,$02,$10,$00,$00,$10,$00,$00,$10,$00,$00,$00,$00,$00,$00,
		$08,$00,$09,$08,$09,$05,$0b,$09,$05,$0b,$09,$0a,$07,$0b,$0a,$06,$03,
		$08,$00,$09,$08,$09,$00,$08,$09,$00,$08,$09,$09,$06,$0d,$08,$00,$00,
		$08,$00,$09,$08,$09,$00,$08,$09,$00,$08,$09,$09,$00,$00,$08,$00,$00,
		$0e,$00,$04,$0e,$0c,$06,$0d,$0c,$06,$0d,$04,$0c,$06,$03,$0e,$00,$01
	);
begin

	screenmemory := $1e14;//addresstable(scrm,1,2);
	printstring(titleText,0,11);
	screenmemory := $1e27;//screenmemory + @SCREEN_WIDTH;
	p1:= $9627;//addresstable(clrm,1,3);
	r:=0;
	fori i:=0 to 4 do
	begin
		fori t:=0 to 16 do
		begin
			screenmemory[t] := titlegfx[r];
			p1[t] := GREEN;
			r:=r+1;
		end;
		screenmemory := screenmemory + @SCREEN_WIDTH;
		p1 := p1 + @SCREEN_WIDTH;
	end;
	screenmemory := $1f7f; //addresstable(scrm,3,20);
	printstring(rockText,0,13);
	screenmemory := $1fa5; //addresstable(scrm,3,22);
	printstring(hewcoText,0,13);

	screenmemory := $1efd; //addresstable(scrm,6,12);
	printdecimal(score,5);
	screenmemory[6] := 48;
	
end;

procedure InstructionScreen();
var // actual text letters are in the reverse range (in ROM) so referring to those instead
	textInst0: array[] of byte =(137,142,147,148,146,149,131,148,137,143,142,147, 78);

	textInst1: array[] of byte =(131,140,133,129,146,0,133,129,131,136,0,141,129,154,133, 18);
	textInst2: array[] of byte =(130,133,134,143,146,133,0,148,137,141,133,147,0,149,144, 56);

	textInst3: array[] of byte =(132,143,142,167,148,0,140,133,148,0,142,137,130,130,140,133,146, 23);
	textInst4: array[] of byte =(130,137,148,133,0,137,148,147,133,140,134, 54);

	textInst5: array[] of byte =(142,137,130,130,140,133,146,0,132,143,133,147,0,142,143,148, 20);
	textInst6: array[] of byte =(147,148,143,144,0,129,148,0,131,143,146,142,133,146,147, 57);

	textInst7: array[] of byte =(142,137,130,130,140,133,146,0,135,146,143,151,147, 20);
	textInst8: array[] of byte =(151,136,133,142,0,137,148,0,133,129,148,147, 96);

	textInstd: array[] of byte =(150,137,131,0,150,133,146,147,137,143,142, 20);
	textInste: array[] of byte =(130,153,0,129,142,132,153,0,136, 254);

begin
	if p1[i] = 254 then return(); // finished
	if p1[i] <> 0 then
		if p1[i] < 100 then
		begin
			p2 := p2 + p1[i];
			r := 0;
			i := i + 1;
		end;
	p2[r] := p1[i];
	i:=i+1;
	r:=r+1;
end;

procedure TitleSequence();
var
	scrnNo : byte;
	scrnDelay : byte;
begin
	scrnNo := 0;
	
	while 1=1 do
	begin
	
		ClearTheScreen();
		scrnNo := scrnNo + 1;
		if scrnNo = 3 then scrnNo := 1;
		if scrnNo = 1 then TitleScreen();

		p1 := textInst0;
		p2 := 7680;
		i := 0; // p1 index
		r := 3; // p2 index (screen)

		numberOfRastersToWait := 15;
		scrnDelay := 0;
		while scrnDelay <> 200 do
		begin
			if scrnNo = 2 then InstructionScreen();
			if scrnNo = 1 then AnimateNibblerBody();
			
			ReadJoystick();
			if (JoyResult & @JOY_FIRE = @JOY_FIRE) then return();
			if (^@KEYB <> 64) then return();
			
			WaitForNRasters(); // uses s var
			scrnDelay := scrnDelay + 1;
		end;

	end;
end;

// Animate Nibbler appearing in level
procedure GameStarting();
begin

	SetupPlayer();
	numberOfRastersToWait := 30;

	// grid
	r := 0;
	while r < 6 do
	begin
		
		tail[r] := @GRID;
		tailc[r] := WHITE;
		r := r + 1;
		SOUND2_REGISTER := 170 + (r*4);
		
		WaitForNRasters();
	end;
	SOUND3_REGISTER := 220;
	WaitForNRasters();
	SOUND3_REGISTER := 0;
	
	// snake
	r := 0;
	while r < 6 do
	begin
		
		tail[r] := @BODYRIGHT;
		tailc[r] := BLUE;
		if r = 0 then tail[r] := @TAILRIGHT;
		if r = 5 then tail[r] := @HEADRIGHT;
		r := r + 1;
		SOUND2_REGISTER := 175 + (r*4);
		
		WaitForNRasters();
	end;
	SOUND3_REGISTER := 230;
	WaitForNRasters();
	SOUND3_REGISTER := 0;

	// colour
	r := 0;
	while r < 6 do
	begin
		
		tailc[r] := GREEN;
		if r = 0 then tailc[r] := YELLOW;
		r := r + 1;
		SOUND2_REGISTER := 180 + (r*4);
		
		WaitForNRasters();
	end;
	SOUND2_REGISTER := 0;
	SOUND3_REGISTER := 240;
	WaitForNRasters();
	SOUND3_REGISTER := 0;
	
end;

// wave complete bonus
procedure CountBonus();
begin
	DrawUIBonus();
	while (time <> 0) do
	begin
		time := time - 1;
		score := score + 1;
		DrawUIScore();
		DrawUITime();
		SOUND3_REGISTER := 150;
		numberOfRastersToWait := 5;
		WaitForNRasters();
		SOUND3_REGISTER := 0;
		WaitForNRasters();
	end;
end;

// move tail to head
procedure LoseLife();
begin
	SOUNDFX_REGISTER := 170;

	tailWait := 0;
	numberOfRastersToWait := 10;
	r := 0;
	while (r = 0) do
	begin
		MoveNibblerTail(); // not using r variable in this procedure
		WaitForNRasters();
		//r:=0;
		if lo(head) = lo(tail) then
			if hi(head) = hi(tail) then r:=1;
	end;
	head[0] := 0;
	lives := lives - 1;
	DrawUILives();
	SOUNDFX_REGISTER := 0;

end;


// scroll the screen down out of view
procedure ScrollScreenDown();
begin
	numberOfRastersToWait := 10;
	r := 1;
	fori i := 1 to 20 do 
	begin
		VSCROLL_REGISTER := peek(VSCROLL_REGISTER, 0) + r ;
		r:=r+1;
		WaitForNRasters();
	end;
end;

// scroll the screen up, back into view (only call if already scrolled down)
procedure ScrollScreenUp();
begin
	numberOfRastersToWait := 10;
	r := 20;
	fori i := 1 to 20 do 
	begin
		VSCROLL_REGISTER := peek(VSCROLL_REGISTER, 0) - r;
		r:=r-1;
		WaitForNRasters();
	end;
end;