Skip to content

Draft: Resolve "colisao/sons"

amol requested to merge amol/maindasdasda:11-colisao into main

Closes #11 (closed)

#include "raylib.h"

int main() { // Inicialização da janela const int screenWidth = 800; const int screenHeight = 600; InitWindow(screenWidth, screenHeight, "Jogo de Plataforma");

// Inicialização de áudio
InitAudioDevice();
Sound jumpSound = LoadSound("jump.wav");
Sound fallSound = LoadSound("fall.wav");
Music backgroundMusic = LoadMusicStream("background_music.mp3");
SetMusicVolume(backgroundMusic, 0.5f);
PlayMusicStream(backgroundMusic);

// Inicialização do jogador
Rectangle player = { screenWidth / 2 - 20, screenHeight / 2, 40, 40 };
Vector2 playerVelocity = { 0, 0 };
bool isJumping = false;

// Inicialização das plataformas
Rectangle platform1 = { 0, screenHeight - 40, screenWidth, 40 };
Rectangle platform2 = { screenWidth / 4, screenHeight - 80, screenWidth / 2, 40 };
bool onPlatform = false;

// Variável para controlar o áudio
bool audioEnabled = true;

SetTargetFPS(60);

while (!WindowShouldClose()) {
    // Ativar/desativar áudio com a tecla 'S'
    if (IsKeyPressed(KEY_S)) {
        audioEnabled = !audioEnabled;
        if (audioEnabled)
            ResumeMusicStream(backgroundMusic);
        else
            PauseMusicStream(backgroundMusic);
    }

    // Movimento do jogador
    if (IsKeyDown(KEY_RIGHT)) {
        playerVelocity.x = 5;
    } else if (IsKeyDown(KEY_LEFT)) {
        playerVelocity.x = -5;
    } else {
        playerVelocity.x = 0;
    }

    // Pulo do jogador
    if (IsKeyPressed(KEY_SPACE) && !isJumping) {
        playerVelocity.y = -10;
        isJumping = true;
        if (audioEnabled)
            PlaySound(jumpSound);
    }

    // Gravidade
    playerVelocity.y += 0.5;
    player.x += playerVelocity.x;
    player.y += playerVelocity.y;

    // Colisão com plataformas
    if (CheckCollisionRecs(player, platform1) || CheckCollisionRecs(player, platform2)) {
        isJumping = false;
        playerVelocity.y = 0;
        onPlatform = true;
    } else {
        onPlatform = false;
    }

    // Verificar se o jogador avançou de nível
    if (player.y > screenHeight) {
        player.y = screenHeight / 2;
        if (audioEnabled)
            PlaySound(fallSound);
    }

    // Atualização da música de fundo
    UpdateMusicStream(backgroundMusic);

    BeginDrawing();
    ClearBackground(RAYWHITE);

    // Desenhar plataformas
    DrawRectangleRec(platform1, DARKGRAY);
    DrawRectangleRec(platform2, DARKGRAY);

    // Desenhar jogador
    DrawRectangleRec(player, BLUE);

    // Desenhar status do áudio
    DrawText(audioEnabled ? "Áudio: Ligado (Pressione 'S' para desativar)" : "Áudio: Desligado (Pressione 'S' para ativar)", 10, 10, 20, WHITE);

    EndDrawing();
}

// Liberar recursos
UnloadSound(jumpSound);
UnloadSound(fallSound);
UnloadMusicStream(backgroundMusic);
CloseAudioDevice();
CloseWindow();

return 0;

}

Merge request reports