Pong::Pong() { // players player1 = new QGraphicsRectItem(0, 0, PADDLE, PADDLE * 3); player1->setPos(PADDLE, (HEIGHT - PADDLE * 3) / 2); player1->setBrush(QBrush(Qt::gray)); addItem(player1); player2 = new QGraphicsRectItem(0, 0, PADDLE, PADDLE * 3); player2->setPos(WIDTH - PADDLE * 2, (HEIGHT - PADDLE * 3) / 2); player2->setBrush(QBrush(Qt::gray)); addItem(player2); // ball ball = new QGraphicsEllipseItem(0, 0, PADDLE, PADDLE); ball->setPos((WIDTH - PADDLE) / 2, (HEIGHT - PADDLE) / 2); ball->setBrush(QBrush(Qt::gray)); addItem(ball); // score tscore1 = new QGraphicsSimpleTextItem(); tscore1->setText("0"); tscore1->setFont(QFont("Times", 36, QFont::Bold)); tscore1->setPos(WIDTH / 2 - PADDLE - 24, PADDLE); tscore1->setBrush(QBrush(Qt::gray)); addItem(tscore1); tscore2 = new QGraphicsSimpleTextItem(); tscore2->setText("0"); tscore2->setFont(QFont("Times", 36, QFont::Bold)); tscore2->setPos(WIDTH / 2 + PADDLE, PADDLE); tscore2->setBrush(QBrush(Qt::gray)); addItem(tscore2); // line int h = 0; int pointSize = PADDLE / 4; while (h < HEIGHT) { QGraphicsRectItem *linePoint = new QGraphicsRectItem(0, 0, pointSize, pointSize); linePoint->setBrush(QBrush(Qt::gray)); linePoint->setPos((WIDTH - pointSize) / 2, h); addItem(linePoint); h += pointSize * 2; } score1 = 0; score2 = 0; dx = -1; dy = -1; speedUpCounter = 0; ballSpeed = 0.2; setSceneRect(0, 0, WIDTH, HEIGHT); setBackgroundBrush(QBrush(Qt::black)); QTimeLine *timer = new QTimeLine(); timer->setFrameRange(0, 100); timer->setLoopCount(10000); timer->start(); connect(timer, SIGNAL(frameChanged(int)), this, SLOT(value_changed(int))); }
void MainWindow::slotAddAnimationItem() //在场景中加入一个动画星星 { StartItem *item = new StartItem; QGraphicsItemAnimation *anim = new QGraphicsItemAnimation; anim->setItem(item); QTimeLine *timeLine = new QTimeLine(4000); timeLine->setCurveShape(QTimeLine::SineCurve); timeLine->setLoopCount(0); anim->setTimeLine(timeLine); int y =(qrand()%400)-200; for(int i=0; i<400; i++) { anim->setPosAt(i/400.0,QPointF(i-200,y)); } timeLine->start(); scene->addItem(item); }
Pong::Pong() { this->setSceneRect(0, 0, WIDTH, HEIGHT); this->p1 = new QGraphicsRectItem(0, 0, PADDLE, PADDLE*3); this->p2 = new QGraphicsRectItem(0, 0, PADDLE, PADDLE*3); this->p1->setPos(PADDLE, (HEIGHT-PADDLE*3)/2); this->p2->setPos(WIDTH-PADDLE*2, (HEIGHT-PADDLE*3)/2); this->p1->setBrush(QBrush(Qt::red)); this->p2->setBrush(QBrush(Qt::blue)); /* ball */ this->ball = new QGraphicsEllipseItem(0, 0, PADDLE, PADDLE); this->ball->setPos((WIDTH-PADDLE)/2, (HEIGHT-PADDLE)/2); this->ball->setBrush(QBrush(Qt::black)); this->setFocusItem(p1); this->dx = -1; this->dy = -1; QTimeLine *timer = new QTimeLine(5000); timer->setFrameRange(0, 100); this->addItem(ball); this->addItem(p1); this->addItem(p2); timer->setLoopCount(10000); timer->start(); connect(timer, SIGNAL(frameChanged(int)), this, SLOT(value_changed(int))); }