Beispiel #1
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();
}
Beispiel #2
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;
}