Initial commit

This commit is contained in:
2023-05-06 18:33:58 +02:00
commit c9500a5548
18 changed files with 790 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include "raylibCpp.h"
#include <vector>
#include "Vec2.h"
#include "Settings.h"
class Board {
public:
Board(Vec2<int> screenPos, Vec2<int> widthHeight, int padding);
void DrawBorder() const;
void Draw() const;
int GetWidth() const;
int GetHeight() const;
private:
int width;
int height;
const int padding;
Vec2<int> size;
Vec2<int> screenPos;
};
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include <string>
#include "Board.h"
#include "Snake.h"
class Game {
public:
Game(int width, int height, int fps, std::string title);
Game(const Game& other) = delete;
Game& operator=(const Game& other) = delete;
~Game() noexcept;
bool GameShouldClose() const;
void Tick();
private:
void Update();
void Draw();
Board board;
Snake snake;
};
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "Vec2.h"
namespace settings
{
//Board settings
inline constexpr Vec2<int> gridSize{30, 15};
inline constexpr Vec2<int> gridPos{50, 50};
inline constexpr int gridCellSize = 30;
inline constexpr int gridInnerPadding = 2;
inline constexpr int gridBorderWidth = 15;
//Window settings
inline constexpr int fps = 60;
}
+72
View File
@@ -0,0 +1,72 @@
#pragma once
#include "raylibCpp.h"
#include <vector>
#include <list>
#include <string>
#include "Vec2.h"
#include "Settings.h"
class Snake {
public:
enum Direction {
Up,
Down,
Left,
Right,
None
};
private:
class Body {
public:
Body(Vec2<int> pos);
void draw(Color color = WHITE) const;
void move(Vec2<int> pos) { this->pos = pos; };
Vec2<int> getPos() const { return this->pos; }
static void setSize(Vec2<int> size) { Body::size = size; }
private:
Vec2<int> pos;
static Vec2<int> size;
Snake::Direction nextPart;
};
class Food {
public:
Food();
void draw() const;
void newPosition();
Vec2<int> getPosition() { return this->pos; }
private:
Vec2<int> pos;
Vec2<int> size;
};
public:
Snake();
void update();
void draw() const;
void setDirection(Direction dir);
private:
void move();
void eat();
Direction dir = Direction::None;
Food food;
bool hasEaten = false;
bool hasBitten = false;
int score = 0;
double lastMoveTime = 0;
double moveDelay = 0.15;
std::list<Snake::Body> bodyList;
};
+66
View File
@@ -0,0 +1,66 @@
#pragma once
template <typename T>
class Vec2
{
public:
Vec2() = default;
constexpr Vec2(T x, T y)
:
x(x),
y(y)
{}
constexpr T GetX() const {return x;};
constexpr T GetY() const {return y;};
constexpr void SetX(T x_in) { x = x_in;};
constexpr void SetY(T y_in) { y = y_in;};
public:
constexpr bool operator==(const Vec2& rhs) const
{
return (x == rhs.x && y == rhs.y);
}
constexpr bool operator!=(const Vec2& rhs) const
{
return !(*this == rhs);
}
constexpr Vec2 operator+(const Vec2& rhs) const
{
return {x + rhs.x, y + rhs.y};
}
constexpr Vec2 operator+(const int rhs) const
{
return {x + rhs, y + rhs};
}
constexpr Vec2& operator+=(const Vec2& rhs)
{
return *this = *this + rhs;
}
constexpr Vec2 operator-(const Vec2& rhs) const
{
return {x - rhs.x, y - rhs.y};
}
constexpr Vec2 operator-(const int rhs) const
{
return { x - rhs, y - rhs};
}
constexpr Vec2& operator-=(const Vec2& rhs)
{
return *this = *this - rhs;
}
constexpr Vec2 operator*(const Vec2& rhs) const
{
return {x * rhs.x, y * rhs.y};
}
constexpr Vec2 operator*(const int rhs) const
{
return { x * rhs, y * rhs };
}
constexpr Vec2& operator*=(const Vec2& rhs)
{
return *this = *this * rhs;
}
private:
T x;
T y;
};
+8
View File
@@ -0,0 +1,8 @@
#pragma once
#include <raylib.h>
#include "Vec2.h"
namespace raycpp
{
void DrawRectangle(Vec2<int> pos, Vec2<int> widthHeight, Color color);
void DrawRectangleLinesEx(Vec2<int> pos, Vec2<int> widthHeight, int lineThick, Color color);
}