Subversion Repositories QTron

Compare Revisions

Regard whitespace Rev 3 → Rev 28

/bike.cpp
13,15 → 13,16
x = rand() % 800;
y = rand() % 600;
linePoints.append(QPoint(x, y));
linePoints.append(QPoint(x, y));
 
velocity = 1;
angle = 0;
angle = (rand() % 4) * 90;
abpool = 0;
name = "";
show = false;
isReady = false;
hadGo = false;
dead = false;
collided = false;
 
colour.setBlue(0);
colour.setRed(0);
36,7 → 37,7
}
}
 
void Bike::draw(QPainter *painter)
void Bike::draw(QPainter *painter, QList<Bike *> bikes)
{
if (show)
{
61,6 → 62,9
painter->drawLine(point1, point2);
}
 
if (!collided)
{
collided = hasCollided(bikes);
if (angle == 0)
{
y -= velocity;
77,7 → 81,6
{
x -= velocity;
}
 
if (angle == 0 || angle == 180)
{
painter->fillRect(x - 5, y - 15, 10, 30, colour);
86,11 → 89,19
{
painter->fillRect(x - 15, y - 5, 30, 10, colour);
}
 
painter->drawText(x, y - 20, name);
}
else
{
linePoints.clear();
}
 
 
 
 
}
}
 
void Bike::run(QList<Bike *> bikes)
{
for (int i = 0; i < bikes.count(); i++)
98,12 → 109,18
Bike *bike = bikes[i];
if (bike->isReady && bike->show)
{
if (bike->dead)
if (bike->collided)
{
socket->write("DEAD ");
socket->write(bike->name.toAscii());
socket->write("\n");
}
else if (bike->dead)
{
socket->write("DISCO ");
socket->write(bike->name.toAscii());
socket->write("\n");
}
else
{
socket->write("BIKE ");
112,11 → 129,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 → 170,90
hasHadGo = true;
}
 
int sign(int x){
if(x > 0){
return 1;
}
if(x < 0){
return -1;
}
return 0;
}
 
bool Bike::hasCollided(QList<Bike *> bikes)
{
// 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;
int j, r;
Bike *bike;
for(r = 0; r < bikes.count(); r++)
{
bike = bikes[r];
int forsubtract = 1;
if(bike->name == name){
forsubtract = 3;
}
for(j = 0; j < bike->linePoints.count() - forsubtract; j++)
{
int jx = bike->linePoints[j].x();
int j1x = bike->linePoints[j+1].x();
int jy = bike->linePoints[j].y();
int j1y = bike->linePoints[j+1].y();
int ix = linePoints[i-1].x();
int i1x = linePoints[i].x();
int iy = linePoints[i-1].y();
int i1y = linePoints[i].y();
 
if(angle == 0){
iy += 1;
i1y -= 1;
}
if(angle == 90){
ix -= 1;
i1x += 1;
}
if(angle == 180){
iy -= 1;
i1y += 1;
}
if(angle == 270){
ix += 1;
i1x -= 1;
}
if(!(jx == j1x && i1x == ix) && !(jy == j1y && i1y == iy))
{
 
// If not parallel
if(jx == j1x)
{
// x equal
 
if(ix > jx && i1x < jx || ix < jx && i1x > jx)
{
if((sign(iy - jy) != sign(iy - j1y)))
return true;
}
}
if(jy == j1y)
{
 
if(iy > jy && i1y < jy || iy < jy && i1y > jy)
{
if((sign(ix - jx) != sign(ix - j1x)))
return true;
}
}
}
}
}
return false;
}
 
void Bike::reset()
{
x = rand() % 800;
152,14 → 261,15
linePoints.clear();
 
linePoints.append(QPoint(x, y));
linePoints.append(QPoint(x, y));
 
velocity = 1;
angle = 0;
abpool = 0;
show = true;
isReady = true;
hadGo = false;
dead = false;
collided = false;
 
socket->write("RESET\n");
}
173,7 → 283,6
 
if (line == "L")
{
linePoints.append(QPoint(x, y));
angle -= 90;
 
if (angle >= 360)
184,11 → 293,18
{
angle += 360;
}
if(velocity < 5)
velocity += 0.3;
else if(velocity > 5)
velocity -= 0.3;
if(abs(5-velocity)<0.3)
velocity = 5;
if(abpool<10)
abpool += 0.2;
hadGo = true;
}
else if (line == "R")
{
linePoints.append(QPoint(x, y));
angle += 90;
 
if (angle >= 360)
199,23 → 315,58
{
angle += 360;
}
if(velocity < 5)
velocity += 0.3;
else if(velocity > 5)
velocity -= 0.3;
if(abs(5-velocity)<0.3)
velocity = 5;
if(abpool<10)
abpool += 0.2;
hadGo = true;
}
else if (line == "A")
{
linePoints[linePoints.size() - 1] = QPoint(x, y);
if(abpool > 0){
velocity += 0.1;
abpool -= 0.5;
} else {
if(velocity < 5)
velocity += 0.3;
else if(velocity > 5)
velocity -= 0.3;
if(abs(5-velocity)<0.3)
velocity = 5;
}
hadGo = true;
}
else if (line == "D")
{
linePoints[linePoints.size() - 1] = QPoint(x, y);
if(abpool > 0){
velocity -= 0.2;
abpool -= 0.5;
}
else {
if(velocity < 5)
velocity += 0.3;
else if(velocity > 5)
velocity -= 0.3;
if(abs(5-velocity)<0.3)
velocity = 5;
}
hadGo = true;
}
else if (line == "N")
{
linePoints[linePoints.size() - 1] = QPoint(x, y);
 
if(velocity < 5)
velocity += 0.3;
else if(velocity > 5)
velocity -= 0.3;
if(abs(5-velocity)<0.3)
velocity = 5;
if(abpool<10)
abpool += 0.2;
hadGo = true;
}
else if (line.startsWith("NAME "))
231,11 → 382,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());
}
/mainwindow.cpp
4,7 → 4,6
QGLWidget(parent)
{
this->setFixedSize(800, 600);
 
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
server->listen(QHostAddress::Any, 4567);
53,7 → 52,6
{
bikes[i]->hasHadGo = false;
bikes[i]->run(bikes);
bikes[i]->draw(&painter);
}
}
Bike *bike;
63,7 → 61,15
{
}
}
 
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
bikes[i]->draw(&painter,bikes);
}
}
}
 
checkClients();
}
84,6 → 90,8
 
void MainWindow::checkClients()
{
bool everyoneDied = true;
 
for (int i = bikes.count() - 1; i >= 0; i--)
{
if (bikes[i]->isDisconnected)
92,5 → 100,21
bikes.removeAt(i);
cout << ":: Removed Bike: " << i << endl;
}
else
{
if (!bikes[i]->collided)
{
everyoneDied = false;
}
else if (!bikes[i]->dead)
{
everyoneDied = false;
}
}
}
 
if (everyoneDied)
{
reset();
}
}
/bike.h
8,6 → 8,7
#include <iostream>
#include <QHostAddress>
#include <QThread>
#include <cmath>
 
using namespace std;
 
17,8 → 18,9
 
public:
Bike(QTcpSocket *sock, int i);
void draw(QPainter *painter);
void draw(QPainter *painter,QList<Bike *> bikes);
void run(QList<Bike *> bikes);
bool hasCollided(QList<Bike *> bikes);
void reset();
bool isReady;
bool dead;
32,11 → 34,13
int id;
bool isDisconnected;
bool hasHadGo;
QColor colour;
bool collided;
float abpool;
 
private:
QTcpSocket *socket;
float velocity;
QColor colour;
 
private slots:
void readyRead();