void BouncyLogo::initPos() { initSpeed(); int trial=1000; do { setPos(qrand()%int(scene()->width()),qrand()%int(scene()->height())); advance(0); } while (trial-- && xvel==0.0 && yvel==0.0); }
void BouncyLogo::initPos() { initSpeed(); int trial=1000; do { move(rand()%canvas()->width(),rand()%canvas()->height()); advance(0); // 動畫不移動 } while (trial-- && xVelocity()==0.0 && yVelocity()==0.0); // trial不為0且x,y速度都為0 }
void BouncyLogo::advance(int stage) { switch ( stage ) { case 0: { double vx = xvel; double vy = yvel; if ( vx == 0.0 && vy == 0.0 ) { // stopped last turn initSpeed(); vx = xvel; vy = yvel; } double nx = x() + vx; double ny = y() + vy; if ( nx < 0 || nx >= scene()->width() ) vx = -vx; if ( ny < 0 || ny >= scene()->height() ) vy = -vy; for (int bounce=0; bounce<4; bounce++) { QList<QGraphicsItem *> l=scene()->collidingItems(this); for (QList<QGraphicsItem *>::Iterator it=l.begin(); it!=l.end(); ++it) { QGraphicsItem *hit = *it; QPainterPath advancedShape = QMatrix().translate(xvel, yvel).map(shape()); if ( hit->type()==logo_rtti && hit->collidesWithPath(mapToItem(hit, advancedShape)) ) { switch ( bounce ) { case 0: vx = -vx; break; case 1: vy = -vy; vx = -vx; break; case 2: vx = -vx; break; case 3: // Stop for this turn vx = 0; vy = 0; break; } xvel = vx; yvel = vy; break; } } } if ( x()+vx < 0 || x()+vx >= scene()->width() ) vx = 0; if ( y()+vy < 0 || y()+vy >= scene()->height() ) vy = 0; xvel = vx; yvel = vy; } break; case 1: moveBy(xvel, yvel); break; } }
void initPowUps(){ initFood(); initSpeed(); initFreeze(); initEdwards(); }
void BouncyLogo::advance(int stage) { switch ( stage ) { case 0: { double vx = xVelocity(); double vy = yVelocity(); if ( vx == 0.0 && vy == 0.0 ) { // stopped last turn initSpeed(); vx = xVelocity(); vy = yVelocity(); } double nx = x() + vx; double ny = y() + vy; if ( nx < 0 || nx >= canvas()->width() ) vx = -vx; // 換反方向 if ( ny < 0 || ny >= canvas()->height() ) vy = -vy; // 換反方向 for (int bounce=0; bounce<4; bounce++) { QCanvasItemList l=collisions(FALSE); // 沒有那麼精準的傳回所有有碰撞的canvas item for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { QCanvasItem *hit = *it; if ( hit->rtti()==logo_rtti && hit->collidesWith(this) ) { switch ( bounce ) { case 0: vx = -vx; break; case 1: vy = -vy; vx = -vx; break; case 2: vx = -vx; break; case 3: // Stop for this turn vx = 0; vy = 0; break; } setVelocity(vx,vy); break; } } } if ( x()+vx < 0 || x()+vx >= canvas()->width() ) vx = 0; if ( y()+vy < 0 || y()+vy >= canvas()->height() ) vy = 0; setVelocity(vx,vy); } break; case 1: QCanvasItem::advance(stage); break; } }
Character::Character(qreal p_x, qreal p_y, Maze* p_maze) : Element(p_x, p_y, p_maze), m_xSpeed(0), m_ySpeed(0) { initSpeed(); m_maxSpeed = m_normalSpeed; // To avoid bugs, but will be overridden in the Ghost and Kapman constructors }
Character::Character(qreal p_x, qreal p_y, Arena* p_arena) : Element(p_x, p_y, p_arena), m_xSpeed(0), m_ySpeed(0) { initSpeed(); m_maxSpeed = m_normalSpeed; // To avoid bugs, but will be overridden in the Player constructors }