-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·192 lines (156 loc) · 4.7 KB
/
main.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// This is a thing, that does stuff. If you would like to learn about the thing or the stuff
// you can contact me at [email protected]
// GitHub: TheNeoCoder/SFML-MachineLearning
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>
using namespace std;
using namespace sf;
#define PI 3.14159265
constexpr int windowWidth{800}, windowHeight{600};
//constexpr float snakeSize{30.f}, snakePoints{3};
// ------------------------------------------------------------------------------------ Utilities
class Util
{
private:
float desiredAngle;
public:
// Helper func to make nodes of snake objects point at the previous node
// Accepts two CircleShape objects and a float and returns a float which is what
// sfml uses for its setRotation functions. TODO: Perhaps make this more generic and eliminate
// the use of CircleShape objects ..... maybe... eventually ... if I have to (template).
float angleToPointAt(CircleShape& originalShape, CircleShape& targetShape, float offset)
{
// Takes the distance from the origin to the target and calculates tan(y/x) to get the
// desired angle.
desiredAngle = atan2(targetShape.getPosition().y - originalShape.getPosition().y,
targetShape.getPosition().x - originalShape.getPosition().x) * (180/PI);
// From [-180, 180] to [0, 360]
if(desiredAngle < 0)
{
desiredAngle = 360 - (desiredAngle);
}
// Gives the option to offset the angle for use with different shapes.
desiredAngle += offset;
return desiredAngle;
}
};
// ----------------------------------------------------------------------------------- Main Objects
struct Position { int x, y; };
class Snake {
constexpr static float defaultRadius = 30.f;
constexpr static int defaultPointCount = 6;
constexpr static float defaultVelocity = 10.f;
float currentVelocityX;
float currentVelocityY;
vector<CircleShape> bodyShapes; //includes head
public:
Snake(Position pos,
Color fillColor,
int bodyLength,
int pointCount = defaultPointCount,
float radius = defaultRadius)
{
//init body
bodyShapes.resize(bodyLength + 1);
for(auto& shp : bodyShapes)
{
shp.setRadius(radius);
shp.setPosition(pos.x, pos.y);
shp.setPointCount(pointCount);
shp.setFillColor(fillColor);
shp.setOrigin(radius , radius);
pos.y+=50;
}
}
void move()
{
if(Keyboard::isKeyPressed(Keyboard::W))
{
currentVelocityX = 0;
currentVelocityY = -defaultVelocity;
cout << "W is pressed" << "\n";
}
if(Keyboard::isKeyPressed(Keyboard::A))
{
currentVelocityX = -defaultVelocity;
currentVelocityY = 0;
cout << "A is pressed" << "\n";
}
if(Keyboard::isKeyPressed(Keyboard::S))
{
currentVelocityX = 0;
currentVelocityY = defaultVelocity;
cout << "S is pressed" << "\n";
}
if(Keyboard::isKeyPressed(Keyboard::D))
{
currentVelocityX = defaultVelocity;
currentVelocityY = 0;
cout << "D is pressed" << "\n";
}
// Move some stuff here
for(auto& shp : bodyShapes)
{
shp.move(currentVelocityX, currentVelocityY);
}
}
void update(float deltaTime)
{
move();
//calculate your angle here
//float angle = ...;
//bodyShapes.front().setRotation(angle);
}
void draw(RenderWindow& window) const
{
for(const auto& shp : bodyShapes)
{
window.draw(shp);
}
}
};
// -------------------------------------------------------------------------- main ... enough said.
int main()
{
ContextSettings settings;
settings.antialiasingLevel = 4;
RenderWindow window(VideoMode(windowWidth, windowHeight), "AI Snake", Style::Default, settings);
// Constructs and starts timer
Clock clock;
Color color;
// Creates a snake object in the center of the screen.
int snakeBodySize{5};
Position position{windowWidth / 2, windowHeight /2};
Snake snake(position, color.Green, snakeBodySize);
float deltaTime;
float accumulator;
float t = 0.0f;
const float dt = 0.01;
while (window.isOpen())
{
// Restarts clock and returns time since last restart.
Time elapsed = clock.restart();
deltaTime = elapsed.asSeconds();
// Meh, an event
Event event;
// Listens for window close event to close the window.
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
{
window.close();
}
}
// Clear the window each frame
window.clear();
// Update objects
snake.update(deltaTime);
// Draw objects
snake.draw(window);
// Display the screen
window.display();
}
return 0;
}
// ----------------------------------------------------------------------- The End, or is it!??!??!