-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.cpp
More file actions
58 lines (49 loc) · 1.74 KB
/
Copy pathupdate.cpp
File metadata and controls
58 lines (49 loc) · 1.74 KB
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
//
// Created by Patryk on 04.06.2021.
//
#include "engine.h"
void Engine::update() {
if(timeSinceLastMove.asSeconds()>=seconds(1.f / (speed * (turbo? 2 : 1))).asSeconds()){
Vector2f currentPos = snake[0].getPosition();
Vector2f lastPos= currentPos;
if(add) {
growUp();
add=false;
}
switch(dir){
case direction::right:
snake[0].setPosition(Vector2f((float)((int)(currentPos.x+20+800)%800),currentPos.y));
break;
case direction::left:
snake[0].setPosition(Vector2f((float)((int)(currentPos.x-20+800)%800),currentPos.y));
break;
case direction::up:
snake[0].setPosition(Vector2f(currentPos.x,(float)((int)(currentPos.y-20+600)%600)));
break;
case direction::down:
snake[0].setPosition(Vector2f(currentPos.x,(float)((int)(currentPos.y+20+600)%600)));
break;
}
for(int i=1;i<snake.size();i++){
currentPos = snake[i].getPosition();
snake[i].setPosition(lastPos);
lastPos=currentPos;
}
for(auto &s : snake)
s.update();
if(snake[0].getShape().getGlobalBounds().intersects(eat.getField().getGlobalBounds())) {
randEatPos();
add= true;
speed+=0.3;
score+=5;
}
if(score==100) state=State::won;
else {
for (int i = 1; i < snake.size(); i++) {
if (snake[0].getShape().getGlobalBounds().intersects(snake[i].getShape().getGlobalBounds()))
state = State::lose;
}
}
timeSinceLastMove = Time::Zero;
}
}