Subversion Repositories QTron

Compare Revisions

Ignore whitespace Rev 1 → Rev 8

/ui_mainwindow.h
File deleted
/mainwindow.ui
File deleted
/bike.h
18,7 → 18,8
public:
Bike(QTcpSocket *sock, int i);
void draw(QPainter *painter);
void run(QList<Bike *> bikes);
void run(QList<Bike *> bikes);
bool hasCollided();
void reset();
bool isReady;
bool dead;
31,12 → 32,13
QList<QPoint> linePoints;
int id;
bool isDisconnected;
bool hasHadGo;
bool hasHadGo;
QColor colour;
bool collided;
 
private:
QTcpSocket *socket;
float velocity;
QColor colour;
 
private slots:
void readyRead();
/bike.cpp
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,22 → 61,31
painter->drawLine(point1, point2);
}
 
if (angle == 0)
if (!collided)
{
y -= velocity;
collided = hasCollided();
 
if (angle == 0)
{
y -= velocity;
}
else if (angle == 90)
{
x += velocity;
}
else if (angle == 180)
{
y += velocity;
}
else if (angle == 270)
{
x -= velocity;
}
}
else if (angle == 90)
else
{
x += velocity;
linePoints.clear();
}
else if (angle == 180)
{
y += velocity;
}
else if (angle == 270)
{
x -= velocity;
}
 
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;
131,7 → 148,7
socket->flush();
 
hadGo = false;
if (!socket->waitForReadyRead(2000))
if (!socket->waitForReadyRead(2000))
{
socket->disconnectFromHost();
dead = true;
142,9 → 159,17
dead = true;
}
}
hasHadGo = true;
hasHadGo = true;
}
 
bool Bike::hasCollided()
{
// Do collision detection here
// use linePoints
 
return false;
}
 
void Bike::reset()
{
x = rand() % 800;
152,7 → 177,6
linePoints.clear();
 
linePoints.append(QPoint(x, y));
linePoints.append(QPoint(x, y));
 
velocity = 1;
angle = 0;
160,6 → 184,7
isReady = true;
hadGo = false;
dead = false;
collided = false;
 
socket->write("RESET\n");
}
173,7 → 198,6
 
if (line == "L")
{
linePoints.append(QPoint(x, y));
angle -= 90;
 
if (angle >= 360)
188,7 → 212,6
}
else if (line == "R")
{
linePoints.append(QPoint(x, y));
angle += 90;
 
if (angle >= 360)
203,19 → 226,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 → 251,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());
}
/QTron.pro
1,7 → 1,7
# -------------------------------------------------
# Project created by QtCreator 2010-05-31T15:59:14
# -------------------------------------------------
QT += network
QT += network opengl
TARGET = QTron
TEMPLATE = app
SOURCES += main.cpp \
9,5 → 9,5
bike.cpp
HEADERS += mainwindow.h \
bike.h
FORMS += mainwindow.ui
FORMS +=
OTHER_FILES += docs.txt
/mainwindow.h
6,16 → 6,12
#include <QTcpServer>
#include <QList>
#include <QTimer>
#include <QGLWidget>
 
#include "bike.h"
 
namespace Ui
class MainWindow : public QGLWidget
{
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
Q_OBJECT
 
public:
24,11 → 20,9
void reset();
 
protected:
void changeEvent(QEvent *e);
void paintEvent(QPaintEvent *e);
 
private:
Ui::MainWindow *ui;
QTcpServer *server;
QList<Bike *> bikes;
QTimer *timer;
/mainwindow.cpp
1,16 → 1,10
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
QGLWidget(parent)
{
ui->setupUi(this);
this->setFixedSize(800, 600);
 
this->setMinimumSize(800, 600);
this->resize(800, 600);
this->setMaximumSize(800, 600);
 
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
server->listen(QHostAddress::Any, 4567);
24,7 → 18,6
 
MainWindow::~MainWindow()
{
delete ui;
delete server;
}
 
36,24 → 29,12
}
}
 
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
 
void MainWindow::paintEvent(QPaintEvent *e)
{
e->accept();
QPainter painter(this);
 
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillRect(0, 0, 800, 600, Qt::white);
 
bool ready = true;
for (int i = 0; i < bikes.count(); i++)
70,16 → 51,18
{
if (bikes[i]->show)
{
bikes[i]->hasHadGo = false;
bikes[i]->run(bikes);
bikes[i]->hasHadGo = false;
bikes[i]->run(bikes);
bikes[i]->draw(&painter);
}
}
Bike *bike;
foreach(bike,bikes){
while(!bike->hasHadGo){
}
}
Bike *bike;
foreach(bike,bikes)
{
while(!bike->hasHadGo)
{
}
}
}
 
checkClients();