program PETFrog;

var
@use "input/key"  
@use "crt"




/* === variables ============================================================================================ */


@include "variables.ras"
		
/* === functions ============================================================================================ */

@include "functions.ras"

/* === main logic ============================================================================================ */


procedure game_loop();

begin
	
	Key::SetupScreenCodes();

	// Unit constants
	
		// Numeric Keypad
		n_key_up:=Key::K8;
        n_key_down:=Key::K5;
        n_key_left:=Key::K4;
        n_key_right:=Key::K6;

		// Regular keys        
		key_up:=Key::KW;
        key_down:=Key::KS;
        key_left:=Key::KA;
        key_right:=Key::KD;


	while (ALIVE=1 and WON_GAME=0) do
	begin

		Key::Read();

        frog_old_x:=frog_x;
        frog_old_y:=frog_y;
        if(Key::Pressed(key_up) or Key::Pressed(n_key_up)) then
        begin
            if(frog_y>1) then
            begin
                dec(frog_y);
                inc(player_score);
            end;
        end;
        
        if(Key::Pressed(key_right) or Key::Pressed(n_key_right)) then
        begin
            if(frog_x<39) then
            begin
                inc(frog_x);
                inc(player_score);
            end;
        end;
        
        if(Key::Pressed(key_down) or Key::Pressed(n_key_down)) then
        begin
            if(frog_y<24) then
            begin
                inc(frog_y);
                inc(player_score);
            end;
        end;
        if(Key::Pressed(key_left) or Key::Pressed(n_key_left)) then
        begin
            if(frog_x>0) then
            begin
                dec(frog_x);
                inc(player_score);
            end;
        end;
		
		// So different things can move at own speeds
		incrange(GAME_TICKS,1,10);
		
		// Show the obstacles in new positions
		show_obstacles();
		
		// "Collision detection"
		check_collisions();

		// Still alive?
		if(player_lives<1) then
		begin
			// Dead and didn't win :(
			ALIVE:=0;
			
		end;	



		// only undraw if different
		if(frog_x<>frog_old_x or frog_y<>frog_old_y ) then
		begin
			textat(frog_old_x,frog_old_y, #previous_tile,1);
			previous_tile:=getat(frog_x,frog_y);	
		end;	
		
		// draw the frog because things might have changed onscreen
		if(frog_y>10) then
		begin
			textat(frog_x,frog_y, #frogcharA,1);	
		end
		else
		begin
			textat(frog_x,frog_y, #frogcharB,1);	
		end;


		
		// Score and lives
		moveto(9,0,hi(#$8000));
		printdecimal(player_score,3);		
		moveto(36,0,hi(#$8000));
		printdecimal(player_lives,1);		


	end;

end;

begin

	
	is_80:=false;
	is_80:=is_80_columns();	


    if(is_80=true) then
	begin
		cursor_down();
		cursor_return();
		uppercase();
		cursor_graphic();
		basic_print(#sorry_80);
		loop();
	end;

	DefineScreen();	
	screen_char_loc:=$8000;
	screenmemory:=$8000;
	cls();

	while(1) do
	begin
		title_screen();
		init_vars();
		game_loop();
		score_screen();
	end;

	

end.

