-
Notifications
You must be signed in to change notification settings - Fork 0
/
slime.cpp
59 lines (51 loc) · 998 Bytes
/
slime.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
#include "slime.h"
#include <QList>
#include <QTimer>
slime::slime():QGraphicsPixmapItem(QPixmap(":/images/slime_right.png")),x_speed(5),y_speed(5)
{
QTimer* timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
timer->start(500);
}
slime::~slime()
{
emit death();
}
void slime::set_movement()
{
x_speed = rand()%5;
y_speed = rand()%10;
if(rand()%2 == 0){
x_speed = -x_speed;
}
if (rand()%2 == 0) {
y_speed = -y_speed;
}
}
bool slime::colliding()
{
QList<QGraphicsItem*> list = collidingItems();
if (list.size() > 0){
return true;
}
else return false;
}
void slime::move()
{
if (colliding()){
set_movement();
}
setPos(x()+x_speed,y()+y_speed);
if (x() < 0) {
x_speed = -x_speed;
}
else if (x() > 570){
x_speed = -x_speed;
}
if (y() < 10){
y_speed = -y_speed;
}
else if (y() > 580){
y_speed = -y_speed;
}
}