13,7 → 13,6 |
x = rand() % 800; |
y = rand() % 600; |
linePoints.append(QPoint(x, y)); |
linePoints.append(QPoint(x, y)); |
|
velocity = 1; |
angle = 0; |
22,6 → 21,7 |
isReady = false; |
hadGo = false; |
dead = false; |
collided = false; |
|
colour.setBlue(0); |
colour.setRed(0); |
61,6 → 61,10 |
painter->drawLine(point1, point2); |
} |
|
if (!collided) |
{ |
collided = hasCollided(); |
|
if (angle == 0) |
{ |
y -= velocity; |
77,6 → 81,11 |
{ |
x -= velocity; |
} |
} |
else |
{ |
linePoints.clear(); |
} |
|
if (angle == 0 || angle == 180) |
{ |
112,11 → 121,19 |
socket->write(QString::number(bike->x).toAscii()); |
socket->write(" "); |
socket->write(QString::number(bike->y).toAscii()); |
socket->write(" "); |
socket->write(QString::number(bike->colour.red()).toAscii()); |
socket->write(" "); |
socket->write(QString::number(bike->colour.green()).toAscii()); |
socket->write(" "); |
socket->write(QString::number(bike->colour.blue()).toAscii()); |
socket->write("\n"); |
} |
} |
} |
|
linePoints.append(QPoint(x, y)); |
|
if (dead) |
{ |
show = false; |
145,6 → 162,51 |
hasHadGo = true; |
} |
|
int sign(float x){ |
if(x > 0){ |
return 1; |
} |
if(x < 0){ |
return -1; |
} |
return 0; |
} |
|
bool Bike::hasCollided() |
{ |
// Do collision detection here |
// use linePoints |
int i = linePoints.count() - 1; |
if(linePoints[i-1].x() < 0 || linePoints[i-1].x() > 800 || linePoints[i-1].y() < 0 || linePoints[i-1].y() > 600) |
return true; |
if(linePoints[i].x() < 0 || linePoints[i].x() > 800 || linePoints[i].y() < 0 || linePoints[i].y() > 600) |
return true; |
for(int j = 0; j < linePoints.count() - 2; j++) |
{ |
if(!(linePoints[j].x == linePoints[j+1].x() && linePoints[i].x() == linePoints[i-1].x()) && !(linePoints[j].y() == linePoints[j+1].y() && linePoints[i].y == linePoints[i-1].y())) |
{ |
// If not parallel |
if(linePoints[j].x() == linePoints[j+1].x()) |
{ |
// x equal |
if(linePoints[i-1].x() > linePoints[j].x() && linePoints[i].x() < linePoints[j].x() || linePoints[i-1].x() < linePoints[j].x() && linePoints[i].x() > linePoints[j].x()){ |
if((sign(linePoints[i-1].y() - linePoints[j].y()) != sign(linePoints[i-1].y() - linePoints[j+1].y()))) |
return true; |
} |
} |
else if(linePoints[j].y() == linePoints[j+1].y()) |
{ |
if(linePoints[i-1].y() > linePoints[j].y() && linePoints[i].y() < linePoints[j].y() || linePoints[i-1].y() < linePoints[j].y() && linePoints[i].y() > linePoints[j].y()) |
{ |
if((sign(linePoints[i-1].x() - linePoints[j].x()) != sign(linePoints[i-1].x() - linePoints[j+1].x()))) |
return true; |
} |
} |
} |
} |
return false; |
} |
|
void Bike::reset() |
{ |
x = rand() % 800; |
152,7 → 214,6 |
linePoints.clear(); |
|
linePoints.append(QPoint(x, y)); |
linePoints.append(QPoint(x, y)); |
|
velocity = 1; |
angle = 0; |
160,6 → 221,7 |
isReady = true; |
hadGo = false; |
dead = false; |
collided = false; |
|
socket->write("RESET\n"); |
} |
173,7 → 235,6 |
|
if (line == "L") |
{ |
linePoints.append(QPoint(x, y)); |
angle -= 90; |
|
if (angle >= 360) |
188,7 → 249,6 |
} |
else if (line == "R") |
{ |
linePoints.append(QPoint(x, y)); |
angle += 90; |
|
if (angle >= 360) |
203,19 → 263,16 |
} |
else if (line == "A") |
{ |
linePoints[linePoints.size() - 1] = QPoint(x, y); |
velocity += 0.1; |
hadGo = true; |
} |
else if (line == "D") |
{ |
linePoints[linePoints.size() - 1] = QPoint(x, y); |
velocity -= 0.2; |
hadGo = true; |
} |
else if (line == "N") |
{ |
linePoints[linePoints.size() - 1] = QPoint(x, y); |
hadGo = true; |
} |
else if (line.startsWith("NAME ")) |
231,11 → 288,11 |
{ |
colour.setRed(list[1].toInt()); |
} |
else if (list.count() >= 3) |
if (list.count() >= 3) |
{ |
colour.setGreen(list[2].toInt()); |
} |
else if (list.count() >= 4) |
if (list.count() >= 4) |
{ |
colour.setBlue(list[3].toInt()); |
} |