-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.cpp
120 lines (120 loc) · 3.06 KB
/
snake.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
#include "snake.h"
Snake::Snake()
{
isRun=false;
isOver=false;
isPause=false;
}
Snake::~Snake()
{
delete[]SnakeRect;
}
void Snake::timerEvent(QTimerEvent *event)
{
if(event->timerId()==time&&!isOver&&!isPause)
{for(int i=Snakelen-1;i>0;i--)
SnakeRect[i]=SnakeRect[i-1];
x+=x1*10;y+=y1*10;
}
}
void Snake::init_Snake()
{
x=50;y=50;
Snakelen=1;
direction=4;notletthesnakegoback=4;
isRun=true;
isOver=false;
score=0;
QRect rect(x,y,10,10);
SnakeHeard=rect;
SnakeRect=new QRect[Snakelen];
SnakeRect[0]=SnakeHeard;
time=startTimer(70);
}
void Snake::Snake_update()
{
bool flag;
flag=false;
if(SnakeHeard.left()<50||SnakeHeard.right()>480||SnakeHeard.top()>870||SnakeHeard.bottom()<50)
{
isOver=true;
}
switch (direction)
{case 1:if(notletthesnakegoback!=2) {y2=-1;x2=0;flag=true;}
else {y2=1;x2=0;}
break;
case 2:if(notletthesnakegoback!=1) {y2=1;x2=0;flag=true;}
else {y2=-1;x2=0;}
break;
case 3:if(notletthesnakegoback!=4) {x2=-1;y2=0;flag=true;}
else {x2=1;y2=0;}
break;
case 4:if(notletthesnakegoback!=3) {x2=1;y2=0;flag=true;}
else {x2=-1;y2=0;}
break;
default: ;
}
if(flag)
notletthesnakegoback=direction;
if(isPause)
{x1=0;y1=0;}
else {x1=x2;y1=y2;}
QRect rect(x,y,10,10);
SnakeHeard=rect;
SnakeRect[0]=SnakeHeard;
isHit();
}
void Snake::isEat()
{
Snakelen++;
QRect *tmp=new QRect[Snakelen-1];
for(int i=0;i<Snakelen-1;++i)
tmp[i]=SnakeRect[i];
delete[] SnakeRect;
SnakeRect= new QRect[Snakelen];
for(int i=0;i<Snakelen-1;++i)
SnakeRect[i]=tmp[i];
score++;
if(score<6)
time=startTimer(70-score*5);
else time=startTimer(40);
}
void Snake::isHit()
{
for(int i=1;i<Snakelen;i++)
{
if(SnakeRect[0]==SnakeRect[i])
{
isOver=true;
}
}
}
void Snake::reset()
{
isRun=false;
isOver=!isOver;
}
void Snake::rebegin()
{
istrulyover=false;
isRun=false;
}
Snake &Snake::operator=(const Snake &snaketemp)
{
if(this!=&snaketemp)
{
this->isRun=snaketemp.isRun;this->isOver=snaketemp.isOver;
this->isPause=snaketemp.isPause;this->istrulyover=snaketemp.istrulyover;
this->direction=snaketemp.direction;this->notletthesnakegoback=snaketemp.notletthesnakegoback;
this->x=snaketemp.x;this->y=snaketemp.y;
this->x1=snaketemp.x1;this->x2=snaketemp.x2;
this->y1=snaketemp.y1;this->y2=snaketemp.y2;
this->time=snaketemp.time;this->score=snaketemp.score;this->Snakelen=snaketemp.Snakelen;
this->SnakeRect=new QRect[this->Snakelen];
for(int i=0;i<snaketemp.Snakelen;i++)
this->SnakeRect[i]=snaketemp.SnakeRect[i];
this->SnakeHeard=snaketemp.SnakeHeard;
return *this;
}
return *this;
}