Code1

mail@pastecode.io avatar
unknown
plain_text
2 years ago
7.6 kB
2
Indexable
Never
#include "raylib.h"
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
#define mSizeX 100
#define mSizeY 100

#define blocksize 40
#define movesize 39



const int levelLimitX = 40;
const int levelLimitY = 80;
typedef struct gamegrid
{
    int x, y;
    bool collision;
    bool spawned;

} gamegrid;

typedef struct player
{
    Vector2 ballPosition;
    int health;
    int* pHealth;
    float* pX;
    float* pY;
    bool alive;
} player;

typedef struct enemy
{
    char* name;
    Vector2 enemyPosition;
    float* eX;
    float* eY;
    int dmgmax, hitchance;
} enemy;

gamegrid map[mSizeX][mSizeY];
gamegrid current[mSizeX][mSizeY];

void CreateMap( int x, int y);
void DrawMap(int x, int y);
void DamagePlayer(int p1X, int p1Y, int e1X, int* e1Y, int* pHp);
void PlayerMove(float* p1X, float* p1Y, int* eX, int* eY);
void EnemyMove(float* eX, float* eY, float* p1X, float* p1Y, int* pHp);
enemy CreateEnemy(int screenWidth, int screenHeight);

int main(void)
{
    srand(time(NULL));
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;
    const char* sEnemyNames[6];
    sEnemyNames[0] = "Bat";
    sEnemyNames[1] = "Wolf";
    sEnemyNames[2] = "Goblin";
    sEnemyNames[3] = "Ogre";
    sEnemyNames[4] = "Slime";
    sEnemyNames[5] = "Demon";
    //bool checkonce = true;
    


    InitWindow(screenWidth, screenHeight, "Rogue");
    
    
    enemy Enemy; 
    Enemy = CreateEnemy(screenWidth, screenHeight);
    
    
    printf("%f\n", Enemy.eX);

    gamegrid exitBlock;

    exitBlock.x = blocksize * 10 + blocksize;
    exitBlock.y = blocksize * 5 + blocksize;

    int* pExitX = &exitBlock.x;
    int* pExitY = &exitBlock.y;

    CreateMap(10, 10);
    player Player;
    Player.health = 12;
    Player.pHealth = Player.health;
    Vector2 ballPosition = { (float)screenWidth / 2, (float)screenHeight / 2 - blocksize/2 - 3 };
    Player.ballPosition = ballPosition;
    Player.pX = &Player.ballPosition.x;
    Player.pY = &Player.ballPosition.y;
    Vector2 enemyPosition = { (float)screenWidth / 4, (float)screenHeight / 4 - blocksize / 2 - 15 };
    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
       //player grid movement
        PlayerMove(Player.pX, Player.pY, pExitX, pExitY);
        EnemyMove(Enemy.eX, Enemy.eY, Player.pX, Player.pY, Player.pHealth);
     
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

        ClearBackground(RAYWHITE);

        DrawMap(10, 10);

        DrawRectangle(exitBlock.x - blocksize / 2, exitBlock.y - blocksize / 2, blocksize - 2, blocksize - 2, RED);
        //DrawText("move the ball with arrow keys", 10, 10, 20, DARKGRAY);

        DrawCircleV(Player.ballPosition, blocksize/2, MAROON);
        DrawCircleV(Enemy.enemyPosition, blocksize / 2, ORANGE);
        
        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}


void CreateMap(int x, int y)
{
    for (int i = 0; i < x; i++)
    {

        for (int b = 0; b < y; b++)
        {
            map[i][b].x = blocksize * (b + 1);
            map[i][b].y = blocksize * (i + 1);

        }

    }
}
void DrawMap(int x, int y)
{
    for (int i = 0; i < x; i++)
    {

        for (int b = 0; b < y; b++)
        {
            DrawRectangle(map[i][b].x - blocksize / 2, map[i][b].y - blocksize / 2, blocksize - 2, blocksize - 2, BLUE);
            //DrawText();
        }

    }
}
void DamagePlayer(int p1X, int p1Y, int e1X, int e1Y, int* health)
{
    if (e1X + movesize + 10 > p1X && e1Y + movesize + 10 < (p1X + blocksize) 
        || (e1X - movesize > p1X && e1X - movesize < (p1X + blocksize)) 
        || (e1Y - movesize > p1Y && e1Y - movesize < (p1Y + blocksize))
        || e1Y + movesize > p1Y && e1Y + movesize < (p1Y + blocksize))
    {
        *health = *health - rand() & 6;
    }

}
void PlayerMove(float* p1X, float* p1Y, int* eX, int* eY)
{
    bool checkonce = true;
    for (int i = 0; i < mSizeX; i++)
    {
        for (int b = 0; b < mSizeY; b++)
        {
            if (IsKeyPressed(KEY_RIGHT) && checkonce) //Player.ballPosition.x += 2.0f;
            {
                if ((*p1X + movesize + 10 > map[i][b].x && *p1X + movesize + 10 < (map[i][b].x + blocksize))
                    || (*p1X + movesize + 10 > *eX && *p1X + movesize + 10 < (*eX + blocksize)
                        && *p1Y > (float)*eY && *p1Y < (*eY + movesize)))
                {
                    *p1X += blocksize;
                    checkonce = false;
                    //EnemyMove(*p1X, *p1Y, enemyPosition.x, enemyPosition.y);
                }
            }
            if (IsKeyPressed(KEY_LEFT) && checkonce) //*p1X -= 2.0f;
            {
                if (*p1X - movesize > map[i][b].x && *p1X - movesize < (map[i][b].x + blocksize) && *p1X > levelLimitX)
                {
                    *p1X -= blocksize;
                    checkonce = false;

                }

            }
            if (IsKeyPressed(KEY_UP) && checkonce) //*p1X += 2.0f;
            {
                if (*p1Y - movesize > map[i][b].y && *p1Y - movesize < (map[i][b].y + blocksize) && *p1Y > levelLimitY)
                {
                    *p1Y -= blocksize;
                    checkonce = false;

                }
            }
            if (IsKeyPressed(KEY_DOWN) && checkonce) //*p1X += 2.0f;
            {
                if (*p1Y + movesize > map[i][b].y && *p1Y + movesize < (map[i][b].y + blocksize))
                {
                    *p1Y += blocksize;
                    checkonce = false;

                }
            }


        }
    }
    checkonce = true;
}
void EnemyMove(float* eX, float* eY, float* p1X, float* p1Y, int* pHp)
{
    printf("%f", *eX);
    if (*eX > *p1X)
    {
        *eX = *eX - blocksize;
    }
    if (*eX < *p1X)
    {
        *eX = *eX + blocksize;
    }

    if (*eY > *p1Y)
    {
        *eY = *eY - blocksize;
    }
    if (*eY < *p1Y)
    {
        *eY = *eY + blocksize;
    }
    DamagePlayer(*p1X, *p1Y, *eX, *eY, pHp);
}
enemy CreateEnemy(int screenWidth, int screenHeight)
{
    /*char* name[10];
    Vector2 enemyPosition;
    float* eX, eY;
    int dmgmax, hitchance;*/
    enemy Enemy;
    Enemy.name = "Bat";
    Enemy.enemyPosition.x = (float)screenWidth / 4;
    Enemy.enemyPosition.y = (float)screenHeight / 4 - blocksize / 2 - 15;
    Enemy.eX = &Enemy.enemyPosition.x;
    Enemy.eY = &Enemy.enemyPosition.y;
    return Enemy;
}