Magic game Scroll code :)

Hello people, I bring you a code that I have not yet seen in the processing forum, it is a great help to make video scrolling games or a huge help to make cyclic textures effects completely adaptable to all the needs you may have.

Let’s enjoy it :wink:

CODE:

/*
    
    [This code is part of gameLibZero for Processing 3.x.x]
    
    17/04/2019 - By Luis lopez martinez..
    
    + This code shows how it makes a complex scroll parallax layer..
    + This code is free for all use, enjoy it.
    
    
*/
int st = 0;
scroll id;
//--------------------------------------------------------------------
void setup(){
    size(1280, 720, P2D);
    frameRate = 60;
}
//--------------------------------------------------------------------
void draw(){
    background(0);
    switch(st){
        case 0:
            id = new scroll(loadImage("background.png"));
            id.x = 640;
            id.y = 360;
            id.sizeX = 20;
            id.sizeY = 20;
            st = 10;
            break;
        case 10:
            // draw texts on screen..
            // screenDrawText( textFont, textSize, text, centerCode, x, y, color, opacity 0..255<-(byte notation)  );
            screenDrawText(null, 18, "KEYS TO WORK WITH THIS OBJECT", CENTER, 640, 15, 255, 255);
            screenDrawText(null, 16, "W A S D -> Tiling control", RIGHT, 30, 40, 255, 255);
            screenDrawText(null, 16, "I K O L -> Size axis control", RIGHT, 30, 60, 255, 255);
            screenDrawText(null, 16, "UP DOWN LEFT RIGHT -> Texture offset control", RIGHT, 300, 40, color(255, 64, 64), 255);
            screenDrawText(null, 16, "M + [UP DOWN LEFT RIGHT] -> Texture position control", RIGHT, 300, 60, color(64, 64, 255), 255);
            screenDrawText(null, 16, "U J -> XMIRROR YMIRROR control", RIGHT, 750, 40, 255, 255);
            screenDrawText(null, 16, "E R -> Texture rotation control", RIGHT, 750, 60, 255, 255);
            screenDrawText(null, 16, "SPACE -> RESET MATRIX", CENTER, 640, 100, RED, 255);
            screenDrawText(null, 16, "PShape scroll controller - Luis lopez martinez - Barcelona - SPAIN - 2019.", CENTER, 640, 700, YELLOW, 255);
            // reset effects..
            if(key(_SPACE)){
                id.tiling.x = 1;
                id.tiling.y = 1;
                id.offset.x = 0;
                id.offset.y = 0;
                id.angle = 0;
                id.sizeX = 20;
                id.sizeY = 20;
                id.x = 640;
                id.y = 360;
            }
            
            // tiling control..
            if(key(_A)){
                id.tiling.x -= 0.1;
            }
            if(key(_D)){
                id.tiling.x += 0.1;
            }
            if(key(_W)){
                id.tiling.y -= 0.1;
            }
            if(key(_S)){
                id.tiling.y += 0.1;
            }
            
            // pshape size with separated axis..
            if(key(_I)){
               id.sizeX++;
            }
            if(key(_K)){
               id.sizeX--;
            }
            if(key(_O)){
               id.sizeY++;
            }
            if(key(_L)){
               id.sizeY--;
            }
            
            // texture mirrorin control with separated axis..
            if(key(_U)){
                id.xmirror = true;
            }else{
                id.xmirror = false;
            }
            if(key(_J)){
                id.ymirror = true;
            }else{
                id.ymirror = false;
            }
            
            
            if(key(_M)){
                // texture offset control..
                if(key(_LEFT)){
                    id.x -= 10;
                }
                if(key(_RIGHT)){
                    id.x += 10;
                }
                
                if(key(_UP)){
                    id.y -= 10;
                }
                if(key(_DOWN)){
                    id.y += 10;
                }
            }else{
                // texture offset control..
                if(key(_LEFT)){
                    id.offset.x -= 10;
                }
                if(key(_RIGHT)){
                    id.offset.x += 10;
                }
                
                if(key(_UP)){
                    id.offset.y -= 10;
                }
                if(key(_DOWN)){
                    id.offset.y += 10;
                }
            }
            
            
            // rotation control..
            if(key(_E)){
                id.angle++;
            }
            if(key(_R)){
                id.angle--;
            }
            
            // draw shape with all FX..
            id.render();
            break;
    }
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
//--------------------------------------------------------------------
class scroll {
    int st = 0;
    PImage gr;
    PShape s = null;
    float size = 100;
    float sizeX = 100;
    float sizeY = 100;
    float x = 0;
    float y = 0;
    float angle = 0;
    PVector offset = new PVector(0, 0);
    PVector tiling = new PVector(1, 1);
    boolean xmirror = false;
    boolean ymirror = false;
    //......................................
    scroll(PImage gr) {
        this.gr = gr;
    }
    //......................................
    void render() {

        // configure transformation matrix..
        shapeMode(CENTER);
        textureWrap(REPEAT);

        // create a pshape..
        s = createShape();
        s.setTexture(gr);
        s.disableStyle();
        s.beginShape();
        s.vertex(0, 0,                     offset.x*tiling.x, offset.y*tiling.y);
        s.vertex(gr.width, 0,              (gr.width+offset.x)*tiling.x, 0+(offset.y)*tiling.y);
        s.vertex(gr.width, gr.height,      (gr.width+offset.x)*tiling.x, (gr.height+offset.y)*tiling.y);
        s.vertex(0, gr.height,             offset.x*tiling.x, (gr.height+offset.y)*tiling.y);
        s.endShape(CLOSE);

        // apply pshape fx translate, rotate and scale..
        translate(x, y);
        rotate(radians(-angle));
        if(size==100){
            scale( xmirror ? -sizeX/100.0 : sizeX/100.0, ymirror ? -sizeY/100.0 : sizeY/100.0);
        }else{
            scale( xmirror ? -size/100.0 : size/100.0, ymirror ? -size/100.0 : size/100.0);
        }
        shape(s);
    }
    //......................................
    //......................................
    //......................................
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// multi-key controller..
// -------------------------------------
final int _UP =     38;
final int _DOWN =   40;
final int _LEFT =   37;
final int _RIGHT =  39;
final int _SPACE =  32;
final int _ESC =    27;
final int _ENTER =  10;
final int _F1 =     112;
final int _F2 =     113;
final int _F3 =     114;
final int _F4 =     115;
final int _F5 =     116;
final int _F6 =     117;
final int _F7 =     118;
final int _F8 =     119;
final int _F9 =     120;
final int _F10 =    121;
final int _PRINT =  154;
final int _BLOQ =   145;
final int _PAUSE =  19;
final int _PLUS  =  139;
final int _MINUS =  140;
final int _DEL   =  147;
final int _A =      65;
final int _B =      66;
final int _C =      67;
final int _D =      68;
final int _E =      69;
final int _F =      70;
final int _G =      71;
final int _H =      72;
final int _I =      73;
final int _J =      74;
final int _K =      75;
final int _L =      76;
final int _M =      77;
final int _N =      78;
final int _O =      79;
final int _P =      80;
final int _Q =      81;
final int _R =      82;
final int _S =      83;
final int _T =      84;
final int _U =      85;
final int _V =      86;
final int _W =      87;
final int _X =      88;
final int _Y =      89;
final int _Z =      90;

boolean[] keys = new boolean[256];
//.............................................................................................................................................................
void keyPressed() {
    keys[keyCode] = true;
}
//.............................................................................................................................................................
void keyReleased() {
    keys[keyCode] = false;
}
//.............................................................................................................................................................
final boolean key(int code) {
    return keys[code];
}
//.............................................................................................................................................................
//--------------------------------------------------------------------
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// simple text controller..
final void screenDrawText(PFont fnt_, int size, String text, int cod, float x, float y, color col, float alpha) {  
    pushMatrix();
    if (fnt_ != null) {
        textFont(fnt_);
    }
    textSize(size);
    if (cod==CENTER) {
        textAlign(CENTER, CENTER);
    }
    if (cod==RIGHT) {
        textAlign(LEFT, CENTER);
    }
    if (cod==LEFT) {
        textAlign(RIGHT, CENTER);
    }
    fill(col, alpha);
    text(text, x, y);
    popMatrix();
}
//--------------------------------------------------------------------
// simple color collection..
color LIGHTGRAY = color( 200, 200, 200, 255 );        // Light Gray
color GRAY      = color( 130, 130, 130, 255 );        // Gray
color DARKGRAY  = color( 80, 80, 80, 255 );           // Dark Gray
color YELLOW    = color( 253, 249, 0, 255 );          // Yellow
color GOLD      = color( 255, 203, 0, 255 );          // Gold
color ORANGE    = color( 255, 161, 0, 255 );          // Orange
color PINK      = color( 255, 109, 194, 255 );        // Pink
color RED       = color( 230, 41, 55, 255 );          // Red
color MAROON    = color( 190, 33, 55, 255 );          // Maroon
color GREEN     = color( 0, 228, 48, 255 );           // Green
color LIME      = color( 0, 158, 47, 255 );           // Lime
color DARKGREEN = color( 0, 117, 44, 255 );           // Dark Green
color SKYBLUE   = color( 102, 191, 255, 255 );        // Sky Blue
color BLUE      = color( 0, 121, 241, 255 );          // Blue
color DARKBLUE  = color( 0, 82, 172, 255 );           // Dark Blue
color PURPLE    = color( 200, 122, 255, 255 );        // Purple
color VIOLET    = color( 135, 60, 190, 255 );         // Violet
color DARKPURPLE= color( 112, 31, 126, 255 );         // Dark Purple
color BEIGE     = color( 211, 176, 131, 255 );        // Beige
color BROWN     = color( 127, 106, 79, 255 );         // Brown
color DARKBROWN = color( 76, 63, 47, 255 );           // Dark Brown
color WHITE     = color( 255, 255, 255, 255 );        // White
color BLACK     = color( 0, 0, 0, 255 );              // Black
color BLANK     = color( 0, 0, 0, 0 );                // Transparent
color MAGENTA   = color( 255, 0, 255, 255 );          // Magenta
color RAYWHITE  = color( 245, 245, 245, 255 );        // Ray White

If you liked it, please comment!

1 Like

Nice! great work mate! :smiley:

Thank´s!
This code is a part of gameLibZero for Processing 3.x.

In a few days a publish the project “gameLibZero”, this is a complete game engine for processing. :wink:

2 Likes