Skip to content

Instantly share code, notes, and snippets.

@Mischa-Alff
Created June 9, 2015 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mischa-Alff/665bc3bcfc8a2b9fe7a8 to your computer and use it in GitHub Desktop.
Save Mischa-Alff/665bc3bcfc8a2b9fe7a8 to your computer and use it in GitHub Desktop.
#include <SFML/Graphics.hpp>
#include "Ball.hpp"
Ball::Ball(int x, int y) : Entity(x, y, 10, 10, 4) {}
void Ball::checkCollision(int x, int y) {
;
}
void Ball::checkCollision(sf::FloatRect rect) {
if (getGlobalBounds().intersects(rect)) {
setSpeed(-getSpeed());
}
}
void Ball::update() {
move(getSpeed(), 0);
}
void Ball::update(sf::Keyboard::Key keyUp, sf::Keyboard::Key keyDown) {
;
}
#ifndef BALL
#define BALL
#include <SFML/Graphics.hpp>
#include "Bat.hpp"
#include "Entity.hpp"
class Ball : public Entity {
public:
Ball(int x, int y);
void checkCollision(sf::FloatRect rect);
void checkCollision(int x, int y);
void update();
void update(sf::Keyboard::Key keyUp, sf::Keyboard::Key keyDown);
};
#endif
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "Bat.hpp"
Bat::Bat(int x, int y) : Entity(x, y, 10, 100, 6) {}
void Bat::checkCollision(int x, int y) {
if (getX() < 0) {
setPosition(0, getY());
} else if (getX() + getWidth() > x) {
setPosition(x - getWidth(), getY());
}
if (getY() < 0) {
setPosition(getX(), 0);
} else if (getY() + getHeight() > y) {
setPosition(getX(), y - getHeight());
}
}
void Bat::update(sf::Keyboard::Key keyUp, sf::Keyboard::Key keyDown) {
if (sf::Keyboard::isKeyPressed(keyUp)) {
move(0, -4);
}
if (sf::Keyboard::isKeyPressed(keyDown)) {
move(0, 4);
}
}
void Bat::checkCollision(sf::FloatRect rect) {
}
void Bat::update() {
}
#ifndef BAT
#define BAT
#include <SFML/Graphics.hpp>
#include "Entity.hpp"
class Bat : public Entity {
public:
Bat(int x, int y);
void checkCollision(int x, int y);
void checkCollision(sf::FloatRect rect);
void update(sf::Keyboard::Key keyUp, sf::Keyboard::Key keyDown);
void update();
};
#endif
#include <SFML/Graphics.hpp>
#include "Entity.hpp"
Entity::Entity(int x, int y, int width, int height, int speed) {
setSize(sf::Vector2f(width, height));
setPosition(x, y);
this->speed = speed;
}
int Entity::getX() {
return getGlobalBounds().left;
}
int Entity::getY() {
return getGlobalBounds().top;
}
int Entity::getWidth() {
return getGlobalBounds().width;
}
int Entity::getHeight() {
return getGlobalBounds().height;
}
int Entity::getSpeed() {
return speed;
}
void Entity::setSpeed(int speed) {
this->speed = speed;
}
#ifndef ENTITY
#define ENTITY
#include <SFML/Graphics.hpp>
class Entity : public sf::RectangleShape {
public:
Entity(int x, int y, int width, int height, int speed);
virtual void update() = 0;
virtual void update(sf::Keyboard::Key keyUp, sf::Keyboard::Key keyDown) = 0;
virtual void checkCollision(sf::FloatRect rect) = 0;
virtual void checkCollision(int x, int y) = 0;
void setSpeed(int speed);
int getX();
int getY();
int getWidth();
int getHeight();
int getSpeed();
private:
int speed;
};
#endif
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Bat.hpp"
#include "Ball.hpp"
constexpr int WIDTH = 640, HEIGHT = 480;
static bool paused = false;
void draw(sf::RenderWindow * window, std::vector<Entity*> &entities) {
window->clear();
for (int i = 0; i < 3; i++) {
window->draw(*entities[i]);
}
window->display();
}
void update(sf::RenderWindow * window, std::vector<Entity*> &entities) {
sf::Event event;
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window->close();
} else if (event.type == sf::Event::LostFocus) {
paused = true;
} else if (event.type == sf::Event::GainedFocus) {
paused = false;
}
}
if (!paused) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window->close();
}
for (int i = 0; i < 2; i++) {
entities[i]->checkCollision(sf::FloatRect(WIDTH, HEIGHT, 0, 0));
entities[2]->checkCollision(entities[i]->getGlobalBounds());
}
entities[0]->update(sf::Keyboard::Comma, sf::Keyboard::O);
entities[1]->update(sf::Keyboard::Up, sf::Keyboard::Down);
entities[2]->update();
}
}
int main(int argc, char ** argv) {
sf::VideoMode mode(WIDTH, HEIGHT);
sf::RenderWindow window;
Bat bat1(10, HEIGHT / 2 - 50);
Bat bat2(WIDTH - 20, HEIGHT / 2 - 50);
Ball ball(WIDTH / 2, HEIGHT / 2);
std::vector<Entity*> entities;
entities = {&bat1, &bat2, &ball};
window.create(mode, "SAT");
window.setVerticalSyncEnabled(true);
while (window.isOpen()) {
draw(&window, entities);
update(&window, entities);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment