Пример #1
0
Game::Game(): QGraphicsView(){
    // create a scene
    scene = new QGraphicsScene(this);
    scene->setSceneRect(0,0,800,600);

    // set the scene
    setScene(scene);

    // set curosr
    cursor = nullptr;
    building = nullptr;
    setMouseTracking(true);

    // alter window
    setFixedSize(800,600);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    // create enemy
    Enemy * enemy = new Enemy();
    scene->addItem(enemy);

    // test code
    BuildBrownTowerIcon * bt = new BuildBrownTowerIcon();
    BuildGreenTowerIcon * gt = new BuildGreenTowerIcon();
    BuildRedTowerIcon * rt = new BuildRedTowerIcon();
    gt->setPos(x(),y()+100);
    rt->setPos(x(),y()+200);

    scene->addItem(bt);
    scene->addItem(gt);
    scene->addItem(rt);


}
Пример #2
0
Game::Game(): QGraphicsView(){
move_timer = new QTimer(this);

    move_timer->start(20);

    //create a scene
    scene = new QGraphicsScene(this);
    scene->setSceneRect(0,0,920,720);

    //set the scene
    setScene(scene);

    //set cursor
    cursor = nullptr;
    building = nullptr;
    setMouseTracking(true);

    //alter window
    setFixedSize(920,720);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    //split screen into playfield and menu
    field_menu << QPointF(720,0) << QPointF(720,720);
    for(size_t i=0, n = field_menu.size()-1; i < n; i++){
        //create a line
        QLineF line(field_menu[i],field_menu[i+1]);
        QGraphicsLineItem * lineItem = new QGraphicsLineItem(line);

        QPen pen;
        pen.setWidth(5);
        pen.setColor(Qt::black);

        lineItem->setPen(pen);

        scene->addItem(lineItem);
    }

    //create gridpoints
    for(gPoint_x=0; gPoint_x<20; gPoint_x++){
        for(gPoint_y=0; gPoint_y<20; gPoint_y++){
            Gridpoint *gPoint = new Gridpoint;
            gPoint->setPos(x()+12+(36*gPoint_x),y()+12+(36*gPoint_y));
            scene->addItem(gPoint);
        }
    }

    //creat enemy
    spawnTimer = new QTimer(this);
    enemiesSpawned = 0;
    maxNumberOfEnemies = 0;
    pointsToFollow  << QPointF(18,18) << QPointF(18,414) << QPointF(198,414)<<  QPointF(594,414) << QPointF(594,54) << QPointF(800,54)<< QPointF(594,54);



    for(int i=0;i<=(pointsToFollow.length()-2);i++){
        QPointF temp;
        double test;
        temp=((pointsToFollow[i+1])-(pointsToFollow[i]));
        temp=temp;
        direction[i][0]=temp.x()/(sqrt(pow(temp.x(),2)+pow(temp.y(),2)));
        direction[i][1]=temp.y()/(sqrt(pow(temp.x(),2)+pow(temp.y(),2)));

    }


    createEnemies(20);

    //create Road
    createRoad();

    //create Tower-Icon
    BuildRedTowerIcon * rt = new BuildRedTowerIcon();
    BuildGreenTowerIcon * gt = new BuildGreenTowerIcon();
    BuildBlueTowerIcon * bt = new BuildBlueTowerIcon();
    BuildBlackTowerIcon * bkt = new BuildBlackTowerIcon();
    BuildWhiteTowerIcon * wt = new BuildWhiteTowerIcon();
    BuildYellowTowerIcon * yt = new BuildYellowTowerIcon();

    //move tower icons
    rt->setPos(x()+850,y());
    gt->setPos(x()+850,y()+50);
    bt->setPos(x()+850,y()+100);
    bkt->setPos(x()+850,y()+150);
    yt->setPos(x()+850,y()+200);
    wt->setPos(x()+850,y()+250);

    scene->addItem(rt);
    scene->addItem(gt);
    scene->addItem(bt);
    scene->addItem(bkt);
    scene->addItem(yt);
    scene->addItem(wt);

}