您的位置:首页 > 其它

一个自己的工程(code)

2011-02-22 21:59 281 查看
找了一个qt的例子,改吧改吧,毕竟界面并不是我这个工程的重点。各位看官凑活着看把。

main.cpp

#include <QtGui>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
GraphWidget widget;
widget.show();
return app.exec();
}


mainwindow.h 定制自己的视窗

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QGraphicsView>
class Snake;
//! [0]
class GraphWidget : public QGraphicsView
{
Q_OBJECT
public:
GraphWidget(QWidget *parent = 0);
void addsnake(int length);
protected:
void keyPressEvent(QKeyEvent *event);
void timerEvent(QTimerEvent *event);
void wheelEvent(QWheelEvent *event);
void drawBackground(QPainter *painter, const QRectF &rect);
void scaleView(qreal scaleFactor);
private:
int timerId;
QGraphicsScene *scene;
Snake *psnake;
};

#endif // MAINWINDOW_H


mainwindow.cpp

#include "mainwindow.h"
#include <QtGui>
#include <math.h>
#include "snake.h"
//! [0]
GraphWidget::GraphWidget(QWidget *parent)
: QGraphicsView(parent), timerId(0)
{
scene = new QGraphicsScene(this);
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
scene->setSceneRect(-200, -200, 400, 400);
setScene(scene);
setCacheMode(CacheBackground);
setViewportUpdateMode(BoundingRectViewportUpdate);
setRenderHint(QPainter::Antialiasing);
setTransformationAnchor(AnchorUnderMouse);
scale(qreal(0.8), qreal(0.8));
setMinimumSize(400, 400);
setWindowTitle(tr("Greedy Snake"));
//! [0]
addsnake(5);
timerId = startTimer(1000/ 25);
}
void GraphWidget::addsnake(int length)
{
psnake=new Snake(length);
QList<Node*> list=psnake->getList();
for(int i=0;i<list.length();i++)
{
Node* temp=list[i];
scene->addItem((QGraphicsItem*)temp);
}
}

//! [3]
void GraphWidget::keyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_Up:
psnake->moveUp();
break;
case Qt::Key_Down:
psnake->moveDown();
break;
case Qt::Key_Left:
psnake->moveLeft();
break;
case Qt::Key_Right:
psnake->moveRight();
break;
default:
QGraphicsView::keyPressEvent(event);
}
}
//! [3]
//! [4]
void GraphWidget::timerEvent(QTimerEvent *event)
{
psnake->forward();
}
//! [4]
//! [5]
void GraphWidget::wheelEvent(QWheelEvent *event)
{
scaleView(pow((double)2, -event->delta() / 240.0));
}
//! [5]
//! [6]
void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_UNUSED(rect);
// Shadow
QRectF sceneRect = this->sceneRect();
QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
if (rightShadow.intersects(rect) || rightShadow.contains(rect))
painter->fillRect(rightShadow, Qt::darkGray);
if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
painter->fillRect(bottomShadow, Qt::darkGray);
// Fill
QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
gradient.setColorAt(0, Qt::white);
gradient.setColorAt(1, Qt::lightGray);
painter->fillRect(rect.intersect(sceneRect), gradient);
painter->setBrush(Qt::NoBrush);
painter->drawRect(sceneRect);
// Text
QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
sceneRect.width() - 4, sceneRect.height() - 4);
QString message(tr("Click and drag the nodes around, and zoom with the mouse "
"wheel or the '+' and '-' keys"));
QFont font = painter->font();
font.setBold(true);
font.setPointSize(14);
painter->setFont(font);
painter->setPen(Qt::lightGray);
painter->drawText(textRect.translated(2, 2), message);
painter->setPen(Qt::black);
painter->drawText(textRect, message);
}
//! [6]
//! [7]
void GraphWidget::scaleView(qreal scaleFactor)
{
qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
if (factor < 0.07 || factor > 100)
return;
scale(scaleFactor, scaleFactor);
}


snake.h 贪食蛇的定义,定义了蛇的组成,蛇的运动

#ifndef SNAKE_H
#define SNAKE_H
#include <QList>
class Node;
class Snake
{
public:
Snake(int length);
void forward();
void moveUp();
void moveLeft();
void moveRight();
void moveDown();
QList<Node*> getList();
private:
QList<Node*> nodelist;
};
#endif // SNAKE_H


snake.cpp

#include"snake.h"
#include"node.h"
#define CENTERX 100
#define CENTERY 100
#define SPACE 10
Snake::Snake(int length)
{
qreal startx=CENTERX;
qreal starty=CENTERY;
for(int i=0;i<length;i++)
{
Node *temp=new Node();
temp->setPos(startx+i*SPACE,starty);
nodelist<<temp;
}
}
void Snake::forward()
{
qreal tempx=0;
qreal tempy=0;
qreal step=0;
if(nodelist[0]->pos().x()-nodelist[1]->pos().x()!=0)
{
step=nodelist[0]->pos().x()-nodelist[1]->pos().x();
for(int i=nodelist.length()-1;i>0;i--)
{
tempx=nodelist[i-1]->pos().x();
tempy=nodelist[i-1]->pos().y();
nodelist[i]->setPos(tempx,tempy);
}
tempx=nodelist[0]->pos().x();
tempy=nodelist[0]->pos().y();
nodelist[0]->setPos(tempx+step,tempy);
}
else
{
step=nodelist[0]->pos().y()-nodelist[1]->pos().y();
for(int i=nodelist.length()-1;i>0;i--)
{
tempx=nodelist[i-1]->pos().x();
tempy=nodelist[i-1]->pos().y();
nodelist[i]->setPos(tempx,tempy);
}
tempx=nodelist[0]->pos().x();
tempy=nodelist[0]->pos().y();
nodelist[0]->setPos(tempx,tempy+step);
}
}
void Snake::moveUp()
{
qreal tempx=0;
qreal tempy=0;
qreal step=-SPACE;
if(nodelist[0]->pos().x()-nodelist[1]->pos().x()!=0)
{
for(int i=nodelist.length()-1;i>0;i--)
{
tempx=nodelist[i-1]->pos().x();
tempy=nodelist[i-1]->pos().y();
nodelist[i]->setPos(tempx,tempy);
}
tempx=nodelist[0]->pos().x();
tempy=nodelist[0]->pos().y();
nodelist[0]->setPos(tempx,tempy+step);
}
else
return;
}
void Snake::moveDown()
{
qreal tempx=0;
qreal tempy=0;
qreal step=SPACE;
if(nodelist[0]->pos().x()-nodelist[1]->pos().x()!=0)
{
for(int i=nodelist.length()-1;i>0;i--)
{
tempx=nodelist[i-1]->pos().x();
tempy=nodelist[i-1]->pos().y();
nodelist[i]->setPos(tempx,tempy);
}
tempx=nodelist[0]->pos().x();
tempy=nodelist[0]->pos().y();
nodelist[0]->setPos(tempx,tempy+step);
}
else
return;
}

void Snake::moveLeft()
{
qreal tempx=0;
qreal tempy=0;
qreal step=-SPACE;
if(nodelist[0]->pos().y()-nodelist[1]->pos().y()!=0)
{
for(int i=nodelist.length()-1;i>0;i--)
{
tempx=nodelist[i-1]->pos().x();
tempy=nodelist[i-1]->pos().y();
nodelist[i]->setPos(tempx,tempy);
}
tempx=nodelist[0]->pos().x();
tempy=nodelist[0]->pos().y();
nodelist[0]->setPos(tempx+step,tempy);
}
else
return;
}
void Snake::moveRight()
{
qreal tempx=0;
qreal tempy=0;
qreal step=SPACE;
if(nodelist[0]->pos().y()-nodelist[1]->pos().y()!=0)
{
for(int i=nodelist.length()-1;i>0;i--)
{
tempx=nodelist[i-1]->pos().x();
tempy=nodelist[i-1]->pos().y();
nodelist[i]->setPos(tempx,tempy);
}
tempx=nodelist[0]->pos().x();
tempy=nodelist[0]->pos().y();
nodelist[0]->setPos(tempx+step,tempy);
}
else
return;
}
QList<Node*> Snake::getList()
{
return nodelist;
}


node.h 组成贪食蛇的每个节点。(主要是一些绘图上的润色)

#ifndef NODE_H
#define NODE_H
#include <QGraphicsItem>
#include <QList>

class GraphWidget;
QT_BEGIN_NAMESPACE
class QGraphicsSceneMouseEvent;
QT_END_NAMESPACE
//! [0]
class Node : public QGraphicsItem
{
public:
Node();
bool advance();
QRectF boundingRect() const;
QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
private:
QPointF newPos;
GraphWidget *graph;
};
//! [0]
#endif // NODE_H


node.cpp

#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QStyleOption>
#include "node.h"
//! [0]
Node::Node()
{
setFlag(ItemIsMovable);
setFlag(ItemSendsGeometryChanges);
setCacheMode(DeviceCoordinateCache);
setZValue(-1);
}
//! [0]

//! [7]
bool Node::advance()
{
if (newPos == pos())
return false;
setPos(newPos);
return true;
}
//! [7]
//! [8]
QRectF Node::boundingRect() const
{
qreal adjust = 2;
return QRectF(-10 - adjust, -10 - adjust,
23 + adjust, 23 + adjust);
}
//! [8]
//! [9]
QPainterPath Node::shape() const
{
QPainterPath path;
path.addEllipse(-10, -10, 20, 20);
return path;
}
//! [9]
//! [10]
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::darkGray);
painter->drawEllipse(-7, -7, 20, 20);
QRadialGradient gradient(-3, -3, 10);
if (option->state & QStyle::State_Sunken) {
gradient.setCenter(3, 3);
gradient.setFocalPoint(3, 3);
gradient.setColorAt(1, QColor(Qt::yellow).light(120));
gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
} else {
gradient.setColorAt(0, Qt::yellow);
gradient.setColorAt(1, Qt::darkYellow);
}
painter->setBrush(gradient);
painter->setPen(QPen(Qt::black, 0));
painter->drawEllipse(-10, -10, 20, 20);
}
//! [10]

//! [12]
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
update();
QGraphicsItem::mousePressEvent(event);
}
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
update();
QGraphicsItem::mouseReleaseEvent(event);
}
//! [12]


最后一个能够自由运动的贪食蛇就出来了。有了qt开发确实很快,读书的时候曾经用windows sdk写过一个贪食蛇,比这个可复杂多了,而且还得考虑许多显示上的细节。(比如双缓存什么的)但是,这里只是勉强搭了个台子,关于这个贪食蛇的游戏规则并没有制定。(哦,背景上的文字是找的例子上带的,下次修改的时候在去掉把:P)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: