-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaisnake.cpp
More file actions
105 lines (93 loc) · 2.42 KB
/
Copy pathaisnake.cpp
File metadata and controls
105 lines (93 loc) · 2.42 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
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
#include "aisnake.h"
#include "controlcenter.h"
#include <QDebug>
AIsnake::AIsnake(ControlCenter &Game, int player):
snake(Game, player)
{
head = mapFromScene(QPointF(900-block,720-block));
defaultColor = lightGray;
defaultColor1 = deepGray;
color = defaultColor;
color1 = defaultColor1;
dir = UP;
preDir = UP;
}
bool AIsnake::pathIntoBody(QPointF p){
if(this->SnakeBody.contains(p))
return true;
else return false;
}
void AIsnake::advance(int phase){
if(!phase)
return;
Times++;
if(Times%deltaTime != 0) return;
if(lengToGrow>0) {
QPointF newPart = head;
SnakeBody << newPart;
lengToGrow--;
}
else if(lengToGrow < 0) {
while(lengToGrow < 0){
SnakeBody.removeFirst();
lengToGrow++;
}
SnakeBody << head;
}else {
SnakeBody.removeFirst();
SnakeBody << head;
}
this->game.automove(head, dir);
//can't turn back
if(dir-preDir == 2||preDir-dir ==2)
dir = preDir;
switch (dir) {
case UP: moveUP(); break;
case DOWN: moveDOWN(); break;
case LEFT: moveLEFT(); break;
case RIGHT: moveRIGHT(); break;
}
preDir = dir;
setPos(head);
if(ifSpeedUp){
SpeedUpRecorder++;
if(SpeedUpRecorder == 25){
deltaTime+=1;
speed-=1;
ifSpeedUp = false;
SpeedUpRecorder = 0;
}
}
if(ifSpeedDown){
SpeedDownRecorder++;
if(SpeedDownRecorder == 25){
deltaTime-=1;
speed+=1;
ifSpeedDown = false;
SpeedDownRecorder = 0;
}
}
if(invincible) {
timeRecorder++;
switch (timeRecorder) {
case 6: case 10: colorSwitchBack(); break;
case 8: case 12: color = gold; color1 = Qt::yellow; break;
case 14: invincible = false;
colorSwitchBack();
break;
}
}
else {
if(inevitable){
timeRecorder++;
switch (timeRecorder) {
case 8: case 12: colorSwitchBack(); break;
case 10: case 14: color = darkred; color1 = Qt::red; break;
case 16: inevitable = false;
colorSwitchBack();
break;
}
}
handleCollisions();
}
}