izometrik oyunda mouse la seçilen kareye gelen npc

entry1 galeri1
    1.
  1. https://galeri.uludagsozluk.com/r/2399832/+
    1920x1080 piksel genişliğinde, siyah arkaplanda bir penceremiz olsun, bunu 15x15 piksellik kutularla karolara dönüştürüp, ızgara grid düzeni yaratalım. çizgiler 1 piksel genişliğinde beyaz olucak.

    grid düzleminde, mouse cursor, üzerinde olduğu her karoyu kırmızıya,
    yatay ve dikey eksende taradığı tüm karoları da sarıya boyasın.

    ekranda rastgele bir karoyu yeşil seçelim.

    ve bu yeşil alanın,
    mouse cursor un olduğu kırmızı karoyu her tıkladığımızda,
    en kısa yolu nasıl seçtiğini merak ediyorsanız eğer,
    cevabı çok basit.

    path-finder algoritması!

    #include <SFML/Graphics.hpp>
    #include <vector>
    #include <cmath>

    int main() {
    // 1920x1080 window
    sf::RenderWindow window(sf::VideoMode(1920, 1080), "15x15 Grid with Mouse Highlight and Character");
    window.setFramerateLimit(60);

    // Tile size
    const int tileSize = 15;
    const int tilesX = 1920 / tileSize; // 128 tiles
    const int tilesY = 1080 / tileSize; // 72 tiles

    // Grid lines
    sf::VertexArray lines(sf::Lines);
    for (int x = 0; x <= tilesX; x++) {
    lines.append(sf::Vertex(sf::Vector2f(x * tileSize, 0), sf::Color::White));
    lines.append(sf::Vertex(sf::Vector2f(x * tileSize, 1080), sf::Color::White));
    }
    for (int y = 0; y <= tilesY; y++) {
    lines.append(sf::Vertex(sf::Vector2f(0, y * tileSize), sf::Color::White));
    lines.append(sf::Vertex(sf::Vector2f(1920, y * tileSize), sf::Color::White));
    }

    // Character (green tile) starting position (random)
    srand(static_cast<unsigned>(time(0)));
    int charX = rand() % tilesX; // Random X position
    int charY = rand() % tilesY; // Random Y position
    sf::RectangleShape character(sf::Vector2f(tileSize, tileSize));
    character.setFillColor(sf::Color::Green);

    // Target position for the character (where the mouse clicks)
    int targetX = charX;
    int targetY = charY;

    // Path history (to paint with transparent blue)
    std::vector<sf::Vector2i> path;
    sf::Color transparentBlue(0, 0, 255, 100); // Transparent blue (alpha=100)

    // Game loop
    while (window.isOpen()) {
    sf::Event event;
    while (window.pollEvent(event)) {
    if (event.type == sf::Event::Closed)
    window.close();
    // Left mouse button click to set target
    if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
    sf::Vector2i mousePos = sf::Mouse::getPosition(window);
    targetX = mousePos.x / tileSize;
    targetY = mousePos.y / tileSize;
    path.clear(); // Clear previous path
    }
    }

    // Get mouse position
    sf::Vector2i mousePos = sf::Mouse::getPosition(window);
    int mouseX = mousePos.x / tileSize;
    int mouseY = mousePos.y / tileSize;

    // Move character toward target (simple straight-line movement)
    if (charX != targetX || charY != targetY) {
    // Add current position to path
    path.push_back(sf::Vector2i(charX, charY));

    // Move X direction
    if (charX < targetX) charX++;
    else if (charX > targetX) charX--;

    // Move Y direction
    if (charY < targetY) charY++;
    else if (charY > targetY) charY--;
    }

    // Clear window
    window.clear(sf::Color::Black);

    // Draw transparent yellow for row and column highlights
    sf::RectangleShape tile(sf::Vector2f(tileSize, tileSize));
    sf::Color transparentYellow(255, 255, 0, 100);
    for (int x = 0; x < tilesX; x++) {
    tile.setPosition(x * tileSize, mouseY * tileSize);
    tile.setFillColor(transparentYellow);
    window.draw(tile);
    }
    for (int y = 0; y < tilesY; y++) {
    tile.setPosition(mouseX * tileSize, y * tileSize);
    tile.setFillColor(transparentYellow);
    window.draw(tile);
    }

    // Draw the path (transparent blue)
    for (const auto& pos : path) {
    tile.setPosition(pos.x * tileSize, pos.y * tileSize);
    tile.setFillColor(transparentBlue);
    window.draw(tile);
    }

    // Draw character (green tile)
    character.setPosition(charX * tileSize, charY * tileSize);
    window.draw(character);

    // Draw mouse-highlighted tile (red)
    tile.setPosition(mouseX * tileSize, mouseY * tileSize);
    tile.setFillColor(sf::Color::Red);
    window.draw(tile);

    // Draw grid lines on top
    window.draw(lines);

    // Update window
    window.display();
    }

    return 0;
    }
    0 ...
© 2025 uludağ sözlük