forked from ht177970/ForSchool_Snake_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.hpp
75 lines (69 loc) · 1.96 KB
/
Snake.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef SNAKE_RG_SNAKE_H_
#define SNAKE_RG_SNAKE_H_
#include "Base.hpp"
#include <SFML/Graphics.hpp>
namespace rg {
class Unit : BaseDrawable{
public:
explicit Unit(Unit* previous_unit, int X, int Y, int size);
~Unit();
void setNext(Unit* next_unit);
void draw(sf::RenderWindow& window) override;
virtual void setPos(Pos pos);
Unit* prev() { return u1; }
Unit* next() { return u2; }
Pos getPos() { return { x,y }; }
protected:
int x, y;
Unit* u1, * u2;
explicit Unit(Unit* previous_unit, int X, int Y);
private:
sf::RectangleShape grap;
};
class Snake : public BaseDrawable{
public:
enum class Ways { UP = 1, DOWN = -1, R = 2, L = -2, Non = 0};//R = Right, L = Left
//explicit Snake(int outgame_size, int ingame_width, int ingame_height, int SIZE);
explicit Snake(BaseData data);
~Snake();
bool move();
int getHeadX() { return x; };
int getHeadY() { return y; };
void draw(sf::RenderWindow& window) override;
void gainFood();
void detectWayKeys();
bool posInBody(int x, int y) { return posInBody({ x,y }); }
bool posInBody(Pos position);
bool gameover_delete_tail();
private:
int x, y;
int size;
int m_tail_x, m_tail_y;
int m_outgame_size, m_ingame_width, m_ingame_height;
Ways now_direction, last_move_direction;
Unit* head;
Unit* tail;
int getTailX_prev() { return m_tail_x; }
int getTailY_prev() { return m_tail_y; }
bool illegalPos();
void setDirection(Ways way);
bool UpkeyDown();
bool DownkeyDown();
bool RightkeyDown();
bool LeftkeyDown();
};
class HeadUnit : public Unit {
public:
explicit HeadUnit(Unit* previous_unit, int X, int Y, int size);
void draw(sf::RenderWindow& window) override;
void setPos(Pos pos) override;
void onWayChanged(Snake::Ways new_direction);
private:
int m_size;
int c_adjust_x, c_adjust_y;
bool first_draw;
sf::RectangleShape m_grap_rect;
sf::CircleShape m_grap_circle;
};
}
#endif