Działający obiekt pionka + utworzenie klasy planszy
This commit is contained in:
43
inc/board.hh
Normal file
43
inc/board.hh
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#ifndef BOARD_HH
|
||||||
|
#define BOARD_HH
|
||||||
|
|
||||||
|
/* Plik zawiera implementację planszy(board) do gry w warcaby. */
|
||||||
|
|
||||||
|
class Board
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
// Tablica pionków
|
||||||
|
// Pawn*** board
|
||||||
|
public:
|
||||||
|
|
||||||
|
// Konstruktor inicjujący planszę
|
||||||
|
Board() {}
|
||||||
|
|
||||||
|
// Utwórz nowy pionek na zadanej pozycji
|
||||||
|
Pawn& createPawn(Vector position);
|
||||||
|
|
||||||
|
// Pobierz pionek z określonej pozycji
|
||||||
|
Pawn& getPawn(Vector position);
|
||||||
|
|
||||||
|
// Przesuń pionek na określoną pozycję(target)
|
||||||
|
void setPawnPosition(Vector position, Vector target);
|
||||||
|
|
||||||
|
// Przesuń pionek o określony wektor
|
||||||
|
void movePawn(Vector position, Vector vector);
|
||||||
|
|
||||||
|
// Czy ruch jest możliwy (czy dana pozycja jest osiągalna)
|
||||||
|
bool isMovementPossible(Vector position, Vector target);
|
||||||
|
|
||||||
|
// Usuń pionek z określonej pozycji
|
||||||
|
Pawn deletePawn(Vector position);
|
||||||
|
|
||||||
|
// Rysuj planszę wraz z pionkami
|
||||||
|
void draw(sf::RenderWindow window);
|
||||||
|
|
||||||
|
// Destruktor
|
||||||
|
~Board();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -8,6 +8,9 @@
|
|||||||
// Długość boku pojedynczego kafelka(który jest kwadratem)
|
// Długość boku pojedynczego kafelka(który jest kwadratem)
|
||||||
const int TILE_SIZE = 64;
|
const int TILE_SIZE = 64;
|
||||||
|
|
||||||
|
// Odpowiednie przeskalowanie pionka, aby zachowany był margines względem krawędzi kafelka
|
||||||
|
const int PAWN_MARGIN = 5;
|
||||||
|
|
||||||
// Przedefiniowanie sf::Vector2f na RealVector
|
// Przedefiniowanie sf::Vector2f na RealVector
|
||||||
typedef sf::Vector2f RealVector;
|
typedef sf::Vector2f RealVector;
|
||||||
|
|
||||||
|
|||||||
@@ -25,13 +25,17 @@ struct Vector
|
|||||||
|
|
||||||
// Operator dodawania wektorów
|
// Operator dodawania wektorów
|
||||||
Vector operator+(const Vector& v) { return Vector(x+v.x, y+v.y); }
|
Vector operator+(const Vector& v) { return Vector(x+v.x, y+v.y); }
|
||||||
|
Vector& operator+=(const Vector& v) { return *this = *this + v; }
|
||||||
|
|
||||||
// Operator odejmowania wektorów
|
// Operator odejmowania wektorów
|
||||||
Vector operator-(const Vector& v) { return Vector(x-v.x, y-v.y); }
|
Vector operator-(const Vector& v) { return Vector(x-v.x, y-v.y); }
|
||||||
|
Vector& operator-=(const Vector& v) { return *this = *this - v; }
|
||||||
|
|
||||||
// Mnożenie wektora przez liczbę
|
// Mnożenie wektora przez liczbę
|
||||||
Vector operator*(int a) { return Vector(a*v.x, a*v.y); }
|
Vector operator*(int a) { return Vector(a*x, a*y); }
|
||||||
Vector operator*(float a) { return Vector(a*v.x, a*v.y); }
|
Vector operator*(float a) { return Vector(a*x, a*y); }
|
||||||
|
Vector operator*=(int a) { return *this = *this*a; }
|
||||||
|
Vector operator*=(float a) { return *this = *this*a; }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
* Nie definiuje ona jeszcze żadnych kształtów czy kolorów, jest więc zatem klasą
|
* Nie definiuje ona jeszcze żadnych kształtów czy kolorów, jest więc zatem klasą
|
||||||
* w zasadzie czysto abstrakcyjną. */
|
* w zasadzie czysto abstrakcyjną. */
|
||||||
|
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
#include "def.hh"
|
#include "def.hh"
|
||||||
#include "misc.hh"
|
#include "misc.hh"
|
||||||
@@ -20,17 +20,22 @@ private:
|
|||||||
Vector position;
|
Vector position;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
Object(Vector _position) : position(_position) {}
|
||||||
|
|
||||||
|
// Wirtualny destruktor
|
||||||
|
virtual ~Object() {}
|
||||||
|
|
||||||
// Pobierz pozycję obiektu
|
// Pobierz pozycję obiektu
|
||||||
Vector getPosition() const { return position; }
|
Vector getPosition() const { return position; }
|
||||||
|
|
||||||
// Ustaw pozycję obiektu
|
// Ustaw pozycję obiektu
|
||||||
void setPosition(const Vector& _position) { position = _position }
|
void setPosition(const Vector& _position) { position = _position; }
|
||||||
|
|
||||||
// Przesuń obiekt o wektor
|
// Przesuń obiekt o wektor
|
||||||
void move(const Vector& vector) {}
|
void move(const Vector& vector) { position += vector; }
|
||||||
|
|
||||||
// Rysuj obiekt
|
// Rysuj obiekt
|
||||||
void draw(sf::RenderWindow window) = 0;
|
virtual void draw(sf::RenderWindow& window) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
41
inc/pawn.hh
Normal file
41
inc/pawn.hh
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#ifndef PAWN_HH
|
||||||
|
#define PAWN_HH
|
||||||
|
|
||||||
|
/* Plik implementuje klasę pionka(kamienia) jako pochodną klasy Object */
|
||||||
|
|
||||||
|
#include <SFML/Window.hpp>
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
#include "def.hh"
|
||||||
|
#include "object.hh"
|
||||||
|
|
||||||
|
// Typ wyliczeniowy definiujący możliwe kolory
|
||||||
|
enum Color
|
||||||
|
{
|
||||||
|
CL_WHITE,
|
||||||
|
CL_BLACK
|
||||||
|
};
|
||||||
|
|
||||||
|
// Klasa reprezentująca pionek
|
||||||
|
class Pawn : public Object
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
// Kolor pionka
|
||||||
|
Color color;
|
||||||
|
public:
|
||||||
|
|
||||||
|
// Konstruktor tworzący pionek na zadanej pozycji startowej i o określonym kolorze
|
||||||
|
Pawn(Vector _position, Color _color) : Object(_position), color(_color) {}
|
||||||
|
|
||||||
|
// Pobierz kolor
|
||||||
|
Color getColor() const { return color; }
|
||||||
|
|
||||||
|
// Rysuj pionek na obiekcie window
|
||||||
|
void draw(sf::RenderWindow& window);
|
||||||
|
|
||||||
|
// Destruktor
|
||||||
|
~Pawn() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
1
src/board.cpp
Normal file
1
src/board.cpp
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include "../inc/board.hh"
|
||||||
17
src/main.cpp
17
src/main.cpp
@@ -3,8 +3,25 @@
|
|||||||
|
|
||||||
#include "../inc/def.hh"
|
#include "../inc/def.hh"
|
||||||
#include "../inc/misc.hh"
|
#include "../inc/misc.hh"
|
||||||
|
#include "../inc/object.hh"
|
||||||
|
#include "../inc/pawn.hh"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
sf::ContextSettings settings;
|
||||||
|
settings.antialiasingLevel = 8;
|
||||||
|
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Warcaby", sf::Style::Default, settings);
|
||||||
|
Pawn pawn(Vector(0, 0), CL_BLACK);
|
||||||
|
while(window.isOpen())
|
||||||
|
{
|
||||||
|
sf::Event event;
|
||||||
|
while(window.pollEvent(event))
|
||||||
|
{
|
||||||
|
if(event.type == sf::Event::Closed) window.close();
|
||||||
|
}
|
||||||
|
window.clear(sf::Color(255, 255, 255));
|
||||||
|
pawn.draw(window);
|
||||||
|
window.display();
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/pawn.cpp
Normal file
16
src/pawn.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include "../inc/pawn.hh"
|
||||||
|
|
||||||
|
void Pawn::draw(sf::RenderWindow& window)
|
||||||
|
{
|
||||||
|
// Tworzymy nowy kształt koła o promieniu równym połowie długości boku kafelka i odjęciu dwóch marginesów
|
||||||
|
sf::CircleShape pawn(TILE_SIZE/2 - PAWN_MARGIN*2);
|
||||||
|
|
||||||
|
// Ustawiamy jego pozycję na pozycję zwróconą przez getPosition() uwzględniając marginesy
|
||||||
|
pawn.setPosition(getPosition().getRealVector() + sf::Vector2f(PAWN_MARGIN, PAWN_MARGIN));
|
||||||
|
|
||||||
|
// Wypełniamy odpowiednim kolorem
|
||||||
|
pawn.setFillColor((color==CL_WHITE)?sf::Color::White:sf::Color::Black);
|
||||||
|
|
||||||
|
// Rysujemy na obiekcie window
|
||||||
|
window.draw(pawn);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user