-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnakeThread.h
69 lines (50 loc) · 1.34 KB
/
SnakeThread.h
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
#ifndef SnakeThread_H
#define SnakeThread_H
#include <algorithm>
#include <utility>
#include <vector>
#include <pthread.h>
typedef enum {
V_NEG = -1,
V_NUL,
V_POS
} V;
class SnakeThread {
public:
SnakeThread();
// Courtesy of https://stackoverflow.com/questions/1151582/pthread-function-from-a-class
/** Returns true if the thread was successfully started, false if there was an error starting the thread */
bool StartInternalThread() {
return (pthread_create(&_thread, NULL, InternalThreadEntryFunc, this) == 0);
}
// User intervention
void setXDirection(V vel);
void setYDirection(V vel);
private:
void run();
void drawField();
void checkEdges();
bool checkFruit(int x, int y);
bool checkHead(int x, int y);
bool checkBody(int x, int y);
void updateBodyCoord();
void genFruit();
void genBody();
static void *InternalThreadEntryFunc(void *This) {((SnakeThread *)This)->run(); return NULL;}
pthread_t _thread;
// Law of motion's components
int m_x{};
int m_y{};
int m_xT = 1;
int m_yT = 1;
V m_vx = V_POS;
V m_vy = V_NUL;
bool m_fruit = false;
int m_xFruit{};
int m_yFruit{};
int m_countFruit{};
// Snake's body
std::vector<std::pair<int, int>> m_body;
std::vector<std::pair<int, int>> m_field;
};
#endif