Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.4 kB
1
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;
int x_en, y_en,health;

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);
    }
    int tower_attach(int x_en, int y_en) {
        float dx = x_en - x;
        float dy = y_en - y;
        float distance = sqrt(dx * dx + dy * dy);
        if (distance < range) {
            return damage;
        }
        return 0;
    }
    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);
    }
    void damage(int damage) {
        health -= damage;
    }
};


int main() {
    Tower TOWER(1400,450,300,0,0);
    SYSTEMTIME st;
    int ts, tp;
    GetLocalTime(&st);
    tp = st.wSecond;
    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);
                }

            }
            GetLocalTime(&st);
            ts = st.wSecond;
            if (ts-tp == 1 || tp-ts == 1) {
                Enemy enemy(0, 13, 70, 0.003);
                enemies.push_back(enemy);
                tp = ts;
                
            }

        }

        window.clear(Color::White);
        TOWER.draw(window);
        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;
}