-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyrect.cpp
More file actions
151 lines (122 loc) · 3.42 KB
/
Copy pathmyrect.cpp
File metadata and controls
151 lines (122 loc) · 3.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
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
#include "myrect.h"
#include "fruit.h"
#include "game.h"
#include <QTimer>
#include <QKeyEvent>
#include <QDebug>
#include <typeinfo>
#include <QList>
extern Game * game; //added the global object Game
QList<MyRect*> bodylist;
MyRect::MyRect(){
timer3 = new QTimer(this);
connect(timer3,SIGNAL(timeout()),this,SLOT(move()));
timer3->start(150);
}
void MyRect::move(){
checkCollision();
follow();
getDirection();
}
MyRect::MyRect(int x, int y)
{
setPos(x,y);
setRect(0,0,50,50);
}
void MyRect::keyPressEvent(QKeyEvent *event){
if(event->key() == Qt::Key_Up && thedirection != 3){
thedirection = 1;
}
else if(event->key() == Qt::Key_Left && thedirection != 4){
thedirection = 2;
}
else if(event->key() == Qt::Key_Down && thedirection != 1){
thedirection = 3;
}
else if(event->key() == Qt::Key_Right && thedirection != 2){
thedirection = 4;
}
}
void MyRect::checkCollision()
{
QList<QGraphicsItem *> colliding_items = collidingItems();
int collideX;
int collideY;
for(int i = 0; i < colliding_items.size(); i++){
//check if colliding with snakebody
if(typeid(*(colliding_items[i])) == typeid(MyRect)){
if(colliding_items[i]->x() == this->x() && colliding_items[i]->y() == this->y()){
qDebug() << "collided with snakebody";
game->gameover = true;
}
}else if(typeid(*(colliding_items[i])) == typeid(Fruit)){
if(colliding_items[i]->x() == this->x() && colliding_items[i]->y() == this->y()){
//adding to score
game->points++;
//remove fruit
scene()->removeItem(colliding_items[i]);
//delete from memory
delete colliding_items[i];
//get a new snakebody
collideX = this->pos().x();
collideY = this->pos().y();
MyRect * test = new MyRect(collideX,collideY);
bodylist.append(test);
scene()->addItem(bodylist.back());
Fruit * fruit = new Fruit();
scene()->addItem(fruit);
}
}
}
}
void MyRect::follow()
{
int testX = this->pos().x();
int testY = this->pos().y();
int oldX;
int oldY;
int oldX2;
int oldY2;
for(int i = 0; i < bodylist.length(); i++){
if(i == 0){
oldX = bodylist[i]->pos().x();
oldY = bodylist[i]->pos().y();
bodylist[i]->setPos(testX,testY);
}else {
oldX2 = bodylist[i]->pos().x();
oldY2 = bodylist[i]->pos().y();
bodylist[i]->setPos(oldX,oldY);
oldX = oldX2;
oldY = oldY2;
}
bodylist[i]->setBrush(QBrush(Qt::darkGray));
}
}
void MyRect::getDirection()
{
if(thedirection == 2){
if(pos().x() != 0){
setPos(x()-50,y());
}else
game->gameover = true;
}
else if(thedirection == 4){
if(pos().x() != 550){
setPos(x()+50,y());
}else
game->gameover = true;
}
else if(thedirection == 1){
if(pos().y() != 0){
setPos(x(),y()-50);
}else
game->gameover = true;
}
else if(thedirection == 3){
if(pos().y() != 550){
setPos(x(),y()+50);
}else
game->gameover = true;
}
game->gameOverBro();
}