Raylib - Moving objects














































Raylib - Moving objects



/*
 To make a game more fun, the game should have some user interaction ie. control the movements of the character etc. 

So here's a code for moving the objects or shapes in raylib.
*/

#include"raylib.h"

int main(){
    
    //screen dimensions
    int windowHeight = 500;
    int windowWidth = 1000;
    
    
    InitWindow(windowWidth,windowHeight,"YOUR NEW GAME");
    SetTargetFPS(120);
    
    int circle_x = 200;
    int circle_y = 150;
    int circle_radius = 100;
    
    int pole_x = 0;
    int pole_y = 0;
    int pole_height = 50;
    int pole_width = 100;
    
    int poleDirection = 10;
    
    while(WindowShouldClose()==false){
        BeginDrawing();
        
        //draws a circle
        DrawCircle(circle_x,circle_y,circle_radius,RED);
        
        //moving the circle when a key is pressed
        if(IsKeyDown(KEY_S) && circle_y < windowHeight){
            circle_y += 10;
        }
        if(IsKeyDown(KEY_D) && circle_x < windowWidth){
            circle_x += 10;
        }
        if(IsKeyDown(KEY_W) && circle_y > 0){
            circle_y -= 10;
        }
        if(IsKeyDown(KEY_A) && circle_x > 0){
            circle_x -= 10;
        }
        
        //draws a rectangle
        DrawRectangle(pole_x,pole_y,pole_width,pole_height,BLUE);
        
        //moving the rectangle continuously in the screen
        pole_x += poleDirection;
        if(pole_x > windowWidth-pole_width || pole_x < 0){
            poleDirection = -poleDirection;
        }
        
        //draws a square where the mouse is clicked
        int s_x;
        int s_y;
        int s_width = 50;
        int s_height = 50;
        
        if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
         
            s_x = GetMouseX();
            s_y = GetMouseY();
           
        }
        DrawRectangle(s_x,s_y,s_width,s_height,GREEN);
        
        
        ClearBackground(LIGHTGRAY);
        EndDrawing();
    }
    
}





Comments