Subversion Repositories QTron

Compare Revisions

Ignore whitespace Rev 35 → Rev 1

/ui_mainwindow.h
0,0 → 1,55
/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created: Tue Jun 1 15:55:16 2010
** by: Qt User Interface Compiler version 4.6.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
 
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
 
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QWidget>
 
QT_BEGIN_NAMESPACE
 
class Ui_MainWindow
{
public:
QWidget *centralWidget;
 
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(600, 400);
centralWidget = new QWidget(MainWindow);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
MainWindow->setCentralWidget(centralWidget);
 
retranslateUi(MainWindow);
 
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
 
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
} // retranslateUi
 
};
 
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
 
QT_END_NAMESPACE
 
#endif // UI_MAINWINDOW_H
/bike.h
8,52 → 8,39
#include <iostream>
#include <QHostAddress>
#include <QThread>
#include <cmath>
#include <time.h>
#include <stdio.h>
#include <pthread.h>
 
using namespace std;
 
class Bike : public QObject
class Bike : public QThread
{
Q_OBJECT
Q_OBJECT
 
public:
Bike(QTcpSocket *sock, int i);
void draw(QPainter *painter,QList<Bike *> bikes);
void run();
bool hasCollided(QList<Bike *> bikes);
void setText(QString text);
void reset();
bool isReady;
bool dead;
bool show;
bool hadGo;
float x;
float y;
QString name;
int angle;
QList<QPoint> linePoints;
int id;
bool isDisconnected;
bool hasHadGo;
QList<Bike *> bikes;
QColor colour;
bool collided;
float abpool;
float velocity;
float speed;
public:
Bike(QTcpSocket *sock, int i);
void draw(QPainter *painter);
void run(QList<Bike *> bikes);
void reset();
bool isReady;
bool dead;
bool show;
bool hadGo;
float x;
float y;
QString name;
int angle;
QList<QPoint> linePoints;
int id;
bool isDisconnected;
bool hasHadGo;
 
private:
QTcpSocket *socket;
private:
QTcpSocket *socket;
float velocity;
QColor colour;
 
private slots:
void readyRead();
void disconnected();
 
signals:
void chat(QString name, QString message);
private slots:
void readyRead();
void disconnected();
};
 
#endif // BIKE_H
/QTron.pro
1,7 → 1,7
# -------------------------------------------------
# Project created by QtCreator 2010-05-31T15:59:14
# -------------------------------------------------
QT += network opengl
QT += network
TARGET = QTron
TEMPLATE = app
SOURCES += main.cpp \
9,5 → 9,5
bike.cpp
HEADERS += mainwindow.h \
bike.h
FORMS +=
FORMS += mainwindow.ui
OTHER_FILES += docs.txt
/mainwindow.h
6,36 → 6,37
#include <QTcpServer>
#include <QList>
#include <QTimer>
#include <QGLWidget>
 
 
#include "bike.h"
 
class MainWindow : public QGLWidget
namespace Ui
{
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
Q_OBJECT
 
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void reset();
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void reset();
 
protected:
void paintEvent(QPaintEvent *e);
protected:
void changeEvent(QEvent *e);
void paintEvent(QPaintEvent *e);
 
private:
QTcpServer *server;
QList<Bike *> bikes;
QTimer *timer;
int id;
QTimer *suddenDeathTimer;
bool suddenDeath;
private:
Ui::MainWindow *ui;
QTcpServer *server;
QList<Bike *> bikes;
QTimer *timer;
int id;
 
private slots:
void newConnection();
void checkClients();
void chat(QString name, QString message);
void activateSuddenDeath();
void newConnection();
void checkClients();
};
 
#endif // MAINWINDOW_H
/main.cpp
7,5 → 7,4
MainWindow w;
w.show();
return a.exec();
pthread_exit(NULL);
}
/mainwindow.ui
0,0 → 1,21
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>400</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
/bike.cpp
2,418 → 2,250
 
Bike::Bike(QTcpSocket *sock, int i)
{
socket = sock;
id = i;
socket = sock;
id = i;
 
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
 
isDisconnected = false;
isDisconnected = false;
 
x = rand() % 800;
y = rand() % 600;
linePoints.append(QPoint(x, y));
x = rand() % 800;
y = rand() % 600;
linePoints.append(QPoint(x, y));
linePoints.append(QPoint(x, y));
 
velocity = 1;
angle = (rand() % 4) * 90;
abpool = 0;
name = "";
show = false;
isReady = false;
hadGo = false;
dead = false;
collided = false;
speed = 5;
velocity = 1;
angle = 0;
name = "";
show = false;
isReady = false;
hadGo = false;
dead = false;
 
colour.setBlue(0);
colour.setRed(0);
colour.setGreen(0);
colour.setBlue(0);
colour.setRed(0);
colour.setGreen(0);
 
if (!socket->waitForReadyRead(2000))
{
socket->disconnectFromHost();
show = false;
isReady = true;
hadGo = false;
}
if (!socket->waitForReadyRead(2000))
{
socket->disconnectFromHost();
show = false;
isReady = true;
hadGo = false;
}
}
 
void Bike::draw(QPainter *painter, QList<Bike *> bikes)
void Bike::draw(QPainter *painter)
{
if (show)
{
painter->setPen(colour);
painter->setFont(QFont("sans", 12));
if (show)
{
painter->setPen(colour);
painter->setFont(QFont("sans", 12));
 
for (int i = 0; i < linePoints.count(); i++)
{
QPoint point1;
QPoint point2;
if (i == 0)
{
point1 = linePoints[0];
}
else
{
point1 = linePoints[i - 1];
}
for (int i = 0; i < linePoints.count(); i++)
{
QPoint point1;
QPoint point2;
if (i == 0)
{
point1 = linePoints[0];
}
else
{
point1 = linePoints[i - 1];
}
 
point2 = linePoints[i];
point2 = linePoints[i];
 
painter->drawLine(point1, point2);
}
painter->drawLine(point1, point2);
}
 
if (!collided)
{
collided = hasCollided(bikes);
if (angle == 0)
{
y -= velocity;
}
else if (angle == 90)
{
x += velocity;
}
else if (angle == 180)
{
y += velocity;
}
else if (angle == 270)
{
x -= velocity;
}
if (angle == 0 || angle == 180)
{
painter->fillRect(x - 5, y - 15, 10, 30, colour);
}
else
{
painter->fillRect(x - 15, y - 5, 30, 10, colour);
}
painter->drawText(x, y - 20, name);
}
else
{
linePoints.clear();
}
if (angle == 0)
{
y -= velocity;
}
else if (angle == 90)
{
x += velocity;
}
else if (angle == 180)
{
y += velocity;
}
else if (angle == 270)
{
x -= velocity;
}
 
if (angle == 0 || angle == 180)
{
painter->fillRect(x - 5, y - 15, 10, 30, colour);
}
else
{
painter->fillRect(x - 15, y - 5, 30, 10, colour);
}
 
 
 
}
painter->drawText(x, y - 20, name);
}
}
 
void Bike::run()
void Bike::run(QList<Bike *> bikes)
{
for (int i = 0; i < bikes.count(); i++)
{
Bike *bike = bikes[i];
if (bike->isReady && bike->show)
{
if (bike->collided)
{
socket->write("DEAD ");
socket->write(bike->name.toAscii());
socket->write("\n");
}
else
{
socket->write("BIKE ");
socket->write(bike->name.toAscii());
socket->write(" ");
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");
}
}
}
for (int i = 0; i < bikes.count(); i++)
{
Bike *bike = bikes[i];
if (bike->isReady && bike->show)
{
if (bike->dead)
{
socket->write("DEAD ");
socket->write(bike->name.toAscii());
socket->write("\n");
}
else
{
socket->write("BIKE ");
socket->write(bike->name.toAscii());
socket->write(" ");
socket->write(QString::number(bike->x).toAscii());
socket->write(" ");
socket->write(QString::number(bike->y).toAscii());
socket->write("\n");
}
}
}
 
linePoints.append(QPoint(x, y));
if (dead)
{
show = false;
isReady = true;
hadGo = false;
dead = false;
}
 
if (dead)
{
show = false;
isReady = true;
hadGo = false;
dead = false;
}
if (!dead)
{
socket->write("G\n");
socket->flush();
 
if (!dead)
{
socket->write("G\n");
socket->flush();
 
hadGo = false;
if (!socket->waitForReadyRead(10000))
{
socket->disconnectFromHost();
dead = true;
}
if (!hadGo)
{
socket->disconnectFromHost();
dead = true;
}
}
hasHadGo = true;
 
time_t result = time(NULL);
cout << result << ": Recieved next move from " << name.toStdString().c_str() << endl;
pthread_exit(NULL);
hadGo = false;
if (!socket->waitForReadyRead(2000))
{
socket->disconnectFromHost();
dead = true;
}
if (!hadGo)
{
socket->disconnectFromHost();
dead = true;
}
}
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::setText(QString text)
{
socket->write(text.toAscii().data());
socket->flush();
}
 
void Bike::reset()
{
x = rand() % 800;
y = rand() % 600;
linePoints.clear();
x = rand() % 800;
y = rand() % 600;
linePoints.clear();
 
linePoints.append(QPoint(x, y));
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;
speed = 5;
velocity = 1;
angle = 0;
show = true;
isReady = true;
hadGo = false;
dead = false;
 
socket->write("RESET\n");
socket->write("RESET\n");
}
 
void Bike::readyRead()
{
while (socket->canReadLine())
{
QByteArray data = socket->readLine();
QString line = data.trimmed();
while (socket->canReadLine())
{
QByteArray data = socket->readLine();
QString line = data.trimmed();
 
if (line == "L")
{
angle -= 90;
if (line == "L")
{
linePoints.append(QPoint(x, y));
angle -= 90;
 
if (angle >= 360)
{
angle -= 360;
}
if (angle < 0)
{
angle += 360;
}
if(velocity < speed)
velocity += 0.3;
else if(velocity > speed)
velocity -= 0.3;
if(abs(speed-velocity)<0.3)
velocity = speed;
if(abpool<10)
abpool += 0.2;
hadGo = true;
}
else if (line == "R")
{
angle += 90;
 
if (angle >= 360)
{
angle -= 360;
}
if (angle < 0)
{
angle += 360;
}
if(velocity < speed)
velocity += 0.3;
else if(velocity > speed)
velocity -= 0.3;
if(abs(speed-velocity)<0.3)
velocity = speed;
if(abpool<10)
abpool += 0.2;
hadGo = true;
}
else if (line == "A")
{
if(abpool > 0){
velocity += 0.1;
abpool -= 0.5;
} else {
if(velocity < speed)
velocity += 0.3;
else if(velocity > speed)
velocity -= 0.3;
if(abs(speed-velocity)<0.3)
velocity = speed;
}
hadGo = true;
}
else if (line == "D")
{
if(abpool > 0){
velocity -= 0.2;
abpool -= 0.5;
}
else {
if(velocity < speed)
velocity += 0.3;
else if(velocity > speed)
velocity -= 0.3;
if(abs(speed-velocity)<0.3)
velocity = speed;
}
hadGo = true;
}
else if (line == "N")
{
 
if(velocity < speed)
velocity += 0.3;
else if(velocity > speed)
velocity -= 0.3;
if(abs(speed-velocity)<0.3)
velocity = speed;
if(abpool<10)
abpool += 0.2;
hadGo = true;
}
else if (line.startsWith("NAME "))
{
name = line.remove(0, 5);
isReady = true;
show = true;
}
else if (line.startsWith("COLOUR "))
{
QStringList list = line.split(" ");
if (list.count() >= 2)
{
colour.setRed(list[1].toInt());
}
if (list.count() >= 3)
{
colour.setGreen(list[2].toInt());
}
if (list.count() >= 4)
{
colour.setBlue(list[3].toInt());
}
}
else if (line.startsWith("CHAT "))
if (angle >= 360)
{
angle -= 360;
}
if (angle < 0)
{
angle += 360;
}
hadGo = true;
}
else if (line == "R")
{
QString message = line.remove(0, 5);
linePoints.append(QPoint(x, y));
angle += 90;
 
if (!message.isEmpty())
if (angle >= 360)
{
emit chat(name, message);
angle -= 360;
}
 
if (angle < 0)
{
angle += 360;
}
hadGo = true;
}
}
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 "))
{
name = line.remove(0, 5);
isReady = true;
show = true;
}
else if (line.startsWith("COLOUR "))
{
QStringList list = line.split(" ");
if (list.count() >= 2)
{
colour.setRed(list[1].toInt());
}
else if (list.count() >= 3)
{
colour.setGreen(list[2].toInt());
}
else if (list.count() >= 4)
{
colour.setBlue(list[3].toInt());
}
}
}
}
 
void Bike::disconnected()
{
dead = true;
isDisconnected = true;
cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl;
dead = true;
isDisconnected = true;
cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl;
}
/mainwindow.cpp
1,175 → 1,113
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
QGLWidget(parent)
QMainWindow(parent),
ui(new Ui::MainWindow)
{
this->setFixedSize(800, 600);
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
server->listen(QHostAddress::Any, 4567);
ui->setupUi(this);
 
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(50);
this->setMinimumSize(800, 600);
this->resize(800, 600);
this->setMaximumSize(800, 600);
 
suddenDeath = false;
suddenDeathTimer = new QTimer(this);
connect(suddenDeathTimer, SIGNAL(timeout()), this, SLOT(activateSuddenDeath()));
suddenDeathTimer->start(300000);
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
server->listen(QHostAddress::Any, 4567);
 
id = 0;
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(50);
 
id = 0;
}
 
MainWindow::~MainWindow()
{
delete server;
delete ui;
delete server;
}
 
void MainWindow::reset()
{
suddenDeathTimer->stop();
suddenDeathTimer->start(300000);
suddenDeath = false;
 
for (int i = 0; i < bikes.count(); i++)
{
bikes[i]->reset();
}
}
 
void *runtherun(void *bike){
Bike *realbike = (Bike *)bike;
realbike->run();
pthread_exit(NULL);
}
 
void MainWindow::paintEvent(QPaintEvent *e)
{
if (suddenDeath)
for (int i = 0; i < bikes.count(); i++)
{
for (int i = 0; i < bikes.count(); i++)
{
bikes[i]->speed *= 1.01;
}
bikes[i]->reset();
}
 
e->accept();
QPainter painter(this);
 
painter.fillRect(0, 0, 800, 600, Qt::white);
 
bool ready = true;
for (int i = 0; i < bikes.count(); i++)
{
if (!bikes[i]->isReady)
{
ready = false;
}
}
 
if (ready)
{
cout << ":: Sent move request..." << endl;
pthread_t threads[bikes.count()];
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
bikes[i]->hasHadGo = false;
bikes[i]->bikes = bikes;
pthread_create(&threads[i],NULL,runtherun,(void *)bikes[i]);
}
}
Bike *bike;
foreach(bike,bikes)
{
while(!bike->hasHadGo)
{
}
}
 
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
bikes[i]->draw(&painter,bikes);
}
}
}
 
checkClients();
}
 
void MainWindow::newConnection()
void MainWindow::changeEvent(QEvent *e)
{
while (server->hasPendingConnections())
{
QTcpSocket *socket = server->nextPendingConnection();
Bike *bike = new Bike(socket, id);
connect(bike, SIGNAL(chat(QString,QString)), this, SLOT(chat(QString,QString)));
id += 1;
reset();
 
bikes.append(bike);
cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl;
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
 
void MainWindow::checkClients()
void MainWindow::paintEvent(QPaintEvent *e)
{
bool everyoneDied = true;
e->accept();
QPainter painter(this);
 
for (int i = bikes.count() - 1; i >= 0; i--)
{
if (bikes[i]->isDisconnected)
{
for (int j = 0; j < bikes.count(); j++)
{
bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n"));
}
painter.setRenderHint(QPainter::Antialiasing, true);
 
delete bikes[i];
bikes.removeAt(i);
cout << ":: Removed Bike: " << i << endl;
}
else
bool ready = true;
for (int i = 0; i < bikes.count(); i++)
{
if (!bikes[i]->isReady)
{
if (!bikes[i]->collided)
ready = false;
}
}
 
if (ready)
{
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
everyoneDied = false;
bikes[i]->hasHadGo = false;
bikes[i]->run(bikes);
bikes[i]->draw(&painter);
}
else if (bikes[i]->dead)
{
everyoneDied = false;
}
}
}
Bike *bike;
foreach(bike,bikes){
while(!bike->hasHadGo){
}
}
}
 
if (everyoneDied)
{
reset();
}
checkClients();
}
 
void MainWindow::chat(QString name, QString message)
void MainWindow::newConnection()
{
QString packet = "CHAT ";
packet.append(name);
packet.append(" ");
packet.append(message);
packet.append("\n");
while (server->hasPendingConnections())
{
QTcpSocket *socket = server->nextPendingConnection();
Bike *bike = new Bike(socket, id);
id += 1;
reset();
 
for (int i = 0; i < bikes.count(); i++)
{
bikes[i]->setText(packet);
bikes.append(bike);
cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl;
}
}
 
void MainWindow::activateSuddenDeath()
void MainWindow::checkClients()
{
if (!suddenDeath)
for (int i = bikes.count() - 1; i >= 0; i--)
{
suddenDeath = true;
chat("Server", "SUDDEN DEATH ACTIVATED!");
if (bikes[i]->isDisconnected)
{
delete bikes[i];
bikes.removeAt(i);
cout << ":: Removed Bike: " << i << endl;
}
}
}