void Snake::detectCrash() { QCanvasSprite* head = snakelist.first(); QCanvasItem* item; QCanvasItemList l=head->collisions(FALSE); for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { item = *it; // check if snake hit target if ( (item->rtti()== 1500 ) && (item->collidesWith(head)) ) { Target* target = (Target*) item; target->done(); emit targethit(); extendSnake(); setScore(5); return; } // check if snake hit obstacles if ( (item->rtti()==1600) && (item->collidesWith(head)) ) { emit dead(); autoMoveTimer->stop(); return; } } //check if snake hit itself for (uint i = 3; i < snakelist.count(); i++) { if (head->collidesWith(snakelist.at(i)) ) { emit dead(); autoMoveTimer->stop(); return; } } //check if snake hit edge if ( (head->x() > canvas->width()-5) || (head->y() > canvas->height()-10) || (head->x() <2) || (head->y() <-5) ) { emit dead(); autoMoveTimer->stop(); return; } }
void Bullet::checkCollision() { QCanvasItem* item; QCanvasItemList l=collisions(FALSE); for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { item = *it; if ( (item->rtti()== 1500) && (item->collidesWith(this)) ) { Man* deadman = (Man*)item; if (deadman->frame() != 5) return; deadman->done(); emit score(10); setShotCount(shotcount+1); setAnimated(false); nobullets--; delete this; return; } else if ( (item->rtti()==1900) && (item->collidesWith(this)) ) { Helicopter* deadchopper = (Helicopter*) item; deadchopper->done(); emit score(50); setAnimated(false); nobullets--; delete this; return; } } //check shot is not out of bounds if ( (y() < 0) || (x() < 0) || (y() > canvas()->height()) || ( x() > canvas()->width())) { setAnimated(false); nobullets--; delete this; return; } }
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; } }