-
Notifications
You must be signed in to change notification settings - Fork 0
/
autosnake.cpp
175 lines (138 loc) · 4.1 KB
/
autosnake.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
#include "autosnake.h"
#include "snake.h"
#include "rasterwindow.h"
autoSnake::autoSnake()
{
setAttribute(Qt::WA_DeleteOnClose);
setTitle("QSnake Game");
RasterWindow::resize(1200, 800);//窗口大小
x = 600;
y = 400;//蛇的初始位置
length = 0;//初始蛇长,0表示还没有吃果实
snake.append(QRect(x, y, 25, 25));
do{//随机出现果实的位置
food.a = rand()%24*50;
food.b = rand()%16*50;
}while(snake.contains(QRect(food.a, food.b, 25, 25)));
m_timerId = RasterWindow::startTimer(200);
}
void autoSnake::timerEvent(QTimerEvent *event)
{
int p=snake[length].x(), q=snake[length].y();
if (event->timerId() == m_timerId){
direction = nextStep(snake[length].x(),snake[length].y(),food.a,food.b);
switch (direction) {
case 0: y = y-50; break;
case 1: y = y+50; break;
case 2: x = x+50; break;
case 3: x = x-50; break;
case 4: break;
}//判断目前方向,前进
QRect NEW = QRect(x, y, 25, 25);
snake.append(NEW);
snake.removeFirst();
if(x == food.a && y == food.b){
QRect NEW = QRect(p, q, 25, 25);
snake.push_front(NEW);
length++;
do{
food.a = rand()%24*50;
food.b = rand()%16*50;
}while(snake.contains(QRect(food.a, food.b, 25, 25)));
}
}
handleCollisions();
renderLater();
}
int autoSnake::nextStep(int hx, int hy, int fx, int fy)
{
int nextdir = 4;
int mindis = 9999;
distance[0]=abs(fx-hx)+abs(fy-(hy-50));
for(int i=length-1; i>=0; i--){
if((snake[i].x()==hx && snake[i].y()==(hy-50)) || (hy-50)<0){
distance[0] = 9999;
}//蛇吃到自己
}
distance[1]=abs(fx-hx)+abs(fy-(hy+50));
for(int i=length-1; i>=0; i--){
if((snake[i].x()==hx && snake[i].y()==(hy+50)) || (hy+50)>800){
distance[1] = 9999;
}//蛇吃到自己
}
distance[2]=abs(fx-(hx+50))+abs(fy-hy);
for(int i=length-1; i>=0; i--){
if((snake[i].x()==(hx+50) && snake[i].y()==hy) || (hx+50)>1200){
distance[2] = 9999;
}//蛇吃到自己
}
distance[3]=abs(fx-(hx-50))+abs(fy-hy);
for(int i=length-1; i>=0; i--){
if((snake[i].x()==(hx-50) && snake[i].y()==hy) || (hx-50)<0){
distance[3] = 9999;
}//蛇吃到自己
}
for(int i=0; i<4; i++){
if(distance[i]<mindis){
mindis = distance[i];
nextdir = i;
}
}
return nextdir;
}
void autoSnake::gameover()//判断游戏结束
{
RasterWindow::killTimer(m_timerId);
QMessageBox::information(this,"failed","game over!");
QMessageBox::StandardButton rb = QMessageBox::question(NULL, "Game Over!", "Do you want to restart?", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if(rb == QMessageBox::Yes)
{
this->restart();
return;
}
else{
this->RasterWindow::close();
}
}
void autoSnake::handleCollisions()//处理碰撞
{
for(int i=length-3; i>=0; i--){
if(snake[i].x()==snake[length].x() && snake[i].y()==snake[length].y()){
gameover();
return;
}//蛇吃到自己
}
return;
}
void autoSnake::restart(){
x = 600;
y = 400;//蛇的初始位置
length = 0;
snake.clear();
snake.append(QRect(x, y, 25, 25));
do{//随机出现果实的位置
food.a = rand()%24*50;
food.b = rand()%16*50;
}while(snake.contains(QRect(food.a, food.b, 25, 25)));
direction = 4;
m_timerId = RasterWindow::startTimer(200);
return;
}
void autoSnake::render(QPainter *p)
{
QPen pen;
pen.setWidth(25);
pen.setJoinStyle(Qt::RoundJoin);
pen.setBrush(QColor(255,255,255));
p->setPen(pen);
p->drawRect(snake[length]);//画头
pen.setBrush(Qt::blue);
p->setPen(pen);
for(int i = length-1; i >= 0; i--){
p->drawRect(snake.at(i));
}//画身体
pen.setBrush(Qt::blue);
p->setPen(pen);
p->drawEllipse(food.a, food.b, 25, 25);//画食物
return;
}