Untitled
unknown
plain_text
2 years ago
3.0 kB
5
Indexable
#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:
int x, y;
int health;
int speed;
RectangleShape shape;
Enemy(int x, int y, int health, int 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 tp, ts;
SYSTEMTIME st;
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) % 2 == 0) {
Enemy enemy(1000, 0, 100, 1);
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;
}Editor is loading...