PROGRAM smb;

global

speedmax=5;
speed;
color_index[5]=80,112,144,176,208,240 ;
background;
suit_col[3];
bumped;
dist;
struct score
hits;

end

private
i; j;  k;

BEGIN

    load_fpg("smb.fpg");
    set_mode(m320x200);
    background=new_map(320,200,0,0,210);

    put_screen(0,background);

    scenary(160);

    player();
    for(i=0; i<=4; i+=1)
    cloud((i mod 3)*100+rand(0,100),rand(20,100),101,rand(1,2));
    end
    LOOP

        FRAME;
    END

END

process scenary(x0)
private
copied;


begin
priority=-1000;
z=-100;
y=192;
graph=100;
x=x0;
loop
x-=speed;
if(x<=0)
    x+=320;
    end
if(x>320)
    x-=320;
    end


frame;

end
end



process cloud(x,y,graph,myspeed)
begin
size=50;
z=100;
loop
x-=speed;
if(x<-100)
    cloud(320+rand(0,200),rand(20,150),101,rand(1,2));

break;
end
frame;
end
end



PROCESS player()

PRIVATE
stunting;
animcount = 0;
jumping = FALSE;
//_________________________
sliding = false;
rolling =false;
//_________________________

jump_offset = 0;
jump_power = 8;
//_________________________
roll_offset=0;
roll_power=8;
//_________________________
slide_offset=0;
slide_power=6;

graph0=1;
key_down[2];
key_[2] = _z, _x, _c;
i;
c_id;
vy;
BEGIN


    x = 60;
    y = 175;
    z=100;
    graph0 = 1+suit_col;
        suit_col=0;



    levelmaster();
    LOOP


      if(speed<speedmax)
        speed+=1;
      end
      if(bumped==true)
         speed=-2*speedmax;
         bumped=false;
      end
        dist+=speed;
        write_int(0,10,10,0,&dist);
        write_int(0,10,20,0,&score.hits);

        for(i=0; i<=2; i+=1)

        if(key(key_[i]))
            if(key_down[i]==0)
                key_down[i]=1;
            else
                if(key_down[i]==1)
                    key_down[i]=2;
                end
            end
        else
            key_down[i]=0;
        end
       end



        if(key_down[0]==1 or key_down[1]==1 or key_down[2]==1)

        if(key(key_[0]))
            suit_col=0;
            if(key(key_[1]))
                suit_col=10;
            else
                if(key(key_[2]))
                    suit_col=50;
                end
            end
        else
            if(key(key_[1]))
                suit_col=20;
                if(key(key_[2]))
                    suit_col=30;
                end
            else
                if(key(key_[2]))
                    suit_col=40;
                end
            end
        end

        end



        animcount = animcount + 1;

        IF ( animcount > 3 )
            animcount = 0;

            graph0 = graph0 + 1;

            IF( graph0 == 5)
                graph0 = 2;
            END
        END

        IF ( KEY(_LEFT) )
            x = x - 1;
            flags = 1;
        END

        /*
        IF ( KEY(_RIGHT) )
            x = x + 1;
            flags = 0;
        END
        */

        stunting=rolling or jumping or sliding;

        IF ( KEY(_up) && stunting == 0 )

            jumping = TRUE;
            jump_offset = -jump_power;

        END

        IF ( jumping == TRUE )

            vy =jump_offset;
            y+=vy;
            jump_offset = jump_offset + 1;

            graph0 = 5;

            IF ( jump_offset > jump_power and y=>175)
                jumping = FALSE;
                graph0 = 1;
                y=175;
            END
        END

        if(key(_right) and stunting==0)
            rolling=true;
            roll_offset=-roll_power;
        end

        if(rolling==true)
            vy = roll_offset/2;
            y+=vy;
            roll_offset = roll_offset + 1;
            angle-=360000/(2*roll_power+1);

            graph0 = 5;

            IF ( roll_offset > roll_power )
                rolling= FALSE;
                graph0 = 1;
            END
        end



        if(key(_down) and stunting==0)
            sliding=true;
            slide_offset=-slide_power;
        end

        if(sliding==true)
        graph0=3;
            vy=-slide_offset/3;
            y+=vy;
            angle-=(slide_offset*5000);
            slide_offset = slide_offset + 1;

           //m graph = 5;

            IF ( slide_offset > slide_power )
                sliding= FALSE;
                graph0 = 1;
                angle=0;
            END
        end


        graph=graph0+suit_col;



        c_id=collision(type pipe);
            if(c_id)
                if(x<c_id.x)
                    speed=-speedmax*2;
                else
                    if(sliding)
                        angle-=(slide_offset*5000);
                    else
                        y-=vy;
                    end

                end
            end
        c_id=collision(type lid);

            if(c_id)
                if(x<c_id.x)
                    speed=-speedmax*2;
                else
                    if(sliding)
                        angle-=(slide_offset*5000);
                    else
                        y-=vy;
                    end
                end
            end

        c_id=collision(type ext);

            if(c_id)
                if(x<c_id.x)
                    speed=-speedmax*2;
                else
                    if(sliding)
                        angle-=(slide_offset*5000);
                    else
                        y-=vy;
                    end
                end
            end


        FRAME;
    END

END



process levelmaster();
private
lastpost;
spacing=150;
begin

    loop
        if(dist/spacing>lastpost)
            lastpost=dist/spacing;
            pipe(rand(0,2),rand(0,5));

        end

frame;
end
end



process pipe(height,color)


private
gap=60;

begin
x=340;
y=176;
graph=60+color;
size=100;
switch(height)
    case 0:
      gap=58;
    end
    case 1:
        y+=8;
        gap=50;
    end
    case 2:
        y+=24;
        gap=40:
    end
default: end
end




loop

x-=speed;

laser(x,y-gap/2,color,gap,z+1);


lid(x,y-gap,80+color,z);



if(x<-40)

    break;
end

frame;
end

end


process lid(x,y,graph,z);
private
i;
begin
from i=0 to 2;
ext(x,y-17-i*8,graph-10,z+i);
end
frame;
end

process ext(x,y,graph,z);
begin
frame;
end




process laser(x,y,color,gap,z)
private
col;

begin
col=color_index[color];
flags=4;
graph=new_map(24,gap,12,gap/2,col);
frame;
if(collision(type player))

    if(color*10<>suit_col)
    bumped=true;
    end
end
unload_map(graph);
end

















