Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.9 kB
2
Indexable
Never
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <Windows.h>

using namespace std;
using namespace sf;

const int WIDTH = 1200;
const int HEIGHT = 1000;
const int TILE_SIZE = 32;


class Tower {
public:
    int x, y;
    int range;
    int damage;
    int cost;
    CircleShape shape;

    Tower(int x, int y, int range, int damage, int cost) {
        this->x = x;
        this->y = y;
        this->range = range;
        this->damage = damage;
        this->cost = cost;

        shape.setRadius(range);
        shape.setFillColor(Color(255, 0, 0, 100));
        shape.setOutlineThickness(2);
        shape.setOutlineColor(Color::Red);
        shape.setPosition(x, y);
        shape.setOrigin(range, range);
    }

    void draw(RenderWindow& window) {
        window.draw(shape);
    }
};

class Enemy {
public:
    double x, y;
    int health;
    double speed;
    RectangleShape shape;

    Enemy(int x, int y, int health, double speed) {
        this->x = x;
        this->y = y;
        this->health = health;
        this->speed = speed;

        shape.setSize(Vector2f(TILE_SIZE, TILE_SIZE));
        shape.setFillColor(Color::Green);
        shape.setOutlineThickness(2);
        shape.setOutlineColor(Color::Black);
        shape.setPosition(x * TILE_SIZE, y * TILE_SIZE);
    }

    void draw(RenderWindow& window) {
        window.draw(shape);
    }

    void move() {
        x += speed;
        shape.setPosition(x * TILE_SIZE, y * TILE_SIZE);
    }
};

int main() {
    
    int counter;
    RenderWindow window(VideoMode(WIDTH, HEIGHT), "Tower Defense");

    vector<Tower> towers;
    vector<Enemy> enemies;

    while (window.isOpen()) {
        Event event;
        while (window.pollEvent(event)) {
            if (event.type == Event::Closed) {
                window.close();
            }

            if (event.type == Event::MouseButtonPressed) {
                if (event.mouseButton.button == Mouse::Left) {
                    int x = event.mouseButton.x / TILE_SIZE * TILE_SIZE;
                    int y = event.mouseButton.y / TILE_SIZE * TILE_SIZE;

                    Tower tower(x, y, 20, 10, 100);
                    towers.push_back(tower);
                }
                
            }

            if () {
                Enemy enemy(0, 10, 100, 0.01);
                enemies.push_back(enemy);
            }
            
        }
        
        window.clear(Color::White);

        for (int i = 0; i < towers.size(); i++) {
            towers[i].draw(window);
        }

        for (int i = 0; i < enemies.size(); i++) {
            enemies[i].draw(window);
            enemies[i].move();
        }

        window.display();
    }

    return 0;
}