Example #1
0
void Snake::move(Direction dir)
{
   autoMoveTimer->start(speed);
   int x = 0;
   int y = 0;
   newdir = dir;
   switch (dir) {
      case right: x = 16; break;
      case left: x = -16; break;
      case down: y = 16; break;
      case up: y = -16; break;
   }
   int index = lookUpPiece(currentdir, newdir);
   QCanvasSprite* sprite = new QCanvasSprite(snakeparts, canvas );
   sprite->setFrame(index);
   snakelist.insert(1, sprite);
   sprite->move(snakelist.first()->x(), snakelist.first()->y() );

   snakelist.first()->moveBy(x, y);
   if (grow <= 0)
      changeTail();
   else
      grow--;
   sprite->show();

   currentdir = dir;
}
Example #2
0
void KGrCanvas::makeEnemySprite (int i, int j, int startFrame)
{
    QCanvasSprite * enemySprite = new QCanvasSprite (enemyArray, field);

    enemySprites->append (enemySprite);

    // In KGoldrunner, the top-left visible cell is [1,1] --- in QCanvas [2,2].
    i++; j++;
    enemySprite->move (i * 4 * STEP, j * 4 * STEP, startFrame);
    enemySprite->setZ (2);
    enemySprite->show();
}
Example #3
0
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;
   }
}
Example #4
0
void Snake::createSnake()
{
   snakeparts = new QCanvasPixmapArray();
   QString s0 = Opie::Core::OResource::findPixmap("snake/s0001");
   s0.replace(QRegExp("0001"),"%1");
   snakeparts->readPixmaps(s0, 15);

   grow = 0;
   last = Key_Right;

   QCanvasSprite* head = new QCanvasSprite(snakeparts, canvas );
   head->setFrame(7);
   snakelist.insert(0, head);
   head->show();
   head->move(34, 16);

   QCanvasSprite* body = new QCanvasSprite(snakeparts, canvas );
   body->setFrame(6);
   snakelist.append( body );
   body->show();
   body->move(18, 16);

   QCanvasSprite* end = new QCanvasSprite(snakeparts, canvas );
   end->setFrame(11);
   snakelist.append( end );
   end->show();
   end->move(2, 16);

   currentdir = right;
   speed = 250;
   autoMoveTimer->start(speed);
   moveSnake();
}