Asteroids Game, need help moving ship



  • Hello all,

    I have a school group project to create an Asteroids game. We've managed to get most of the game going by following tutorials and messing with the code. Sadly however most of my group has given up at this point as we can't figure out a few fundamental problems. As such I was wondering if you fine people could help me with the main problem, moving the ship. I have a theory, but I'm not sure of the math behind it.

    My theory:
    I draw the ship, which is a polygon triangle, inside a QRect. The ship already turns using the left and right arrows keys, and it changes a parameter called angle. If I can find a mathematical equation to change the center coordinates of the QRect, based on the current angle, then it should work just fine.

    I'll have to figure out exactly how to do that, like for every second of holding down the up arrow the ship would move ____ distance or something, but this should at least get the ship moving around the screen.

    I can't include the full code in here, at it's several files and is kind of long, so I've just included the file that I feel is most important. The rest of the program, ready to run, is in the zip file linked at the bottom of the post. Please if you have a chance help, this is due Monday and I don't think we're going to make it. Thank you.

    @

    //Sets the angle of the ship
    void CannonField::setAngle(int angle)
    {
    if (currentAngle == angle)
    return;
    currentAngle = angle;
    update(cannonRect());
    emit angleChanged(currentAngle);
    }

    void CannonField::paintEvent(QPaintEvent * /* event */)
    {
    QPainter painter(this);

    if (gameEnded) {
        painter.setPen(Qt::black);
        painter.setFont(QFont("Courier", 48, QFont::Bold));
        painter.drawText(rect(), Qt::AlignCenter, tr("Game Over"));
    }
    paintCannon(painter);
    if (isShooting())
        paintShot(painter);
    if (!gameEnded)
        paintTarget(painter);
    

    }

    void CannonField::paintCannon(QPainter &painter)
    {
    //setPen determines if there is an outline I believe.
    painter.setPen(Qt::NoPen);
    //setBrush determines color.
    painter.setBrush(Qt::blue);

    painter.save();
    //Translate the coordinates to that the center of the play
    //field is (0, 0)
    painter.translate(width()/2, height()/2);
    //Rotates the ship
    painter.rotate(-currentAngle);
    //creates the dimensions for Polygon
    QPolygon polygon;
    polygon << QPoint(-10, 0) << QPoint(10, 0) << QPoint(0, -20);
    //draws the ship
    painter.drawPolygon(polygon);
    painter.restore();
    

    }

    QRect CannonField::cannonRect() const
    {
    //cannonRect should be around the ship, as a
    //sort of hit box and way to move the ship.
    QRect result(0, 0, 500, 500);
    //we should use something like this to move the box
    //and our ship would automatically move with it, since
    //it would always paint inside the box
    result.moveBottomLeft(rect().bottomLeft());
    return result;
    }

    QRect CannonField::targetRect() const
    {
    //QRect is set up for the target/asteroid, and it starts at the bottom
    //left corner, and is 20 wide and 10 high.
    QRect result(0, 0, 20, 10);
    //It is moved from it's center by a certain amount.
    result.moveCenter(QPoint(target.x(), height() - 1 - target.y()));
    return result;
    }@

    "Link to the QT4 Project, Rar format":http://www.box.com/s/4898353af9c9e12e9fd0


  • Moderators

    welcome to devnet

    Are you aware of "this Qt":http://qt-project.org/doc/qt-4.8/graphicsview-portedasteroids.html example?
    I am not familiar enough with the tutorials and it might be exactly this example. Anyhow, based on your topic the example might be your teacher's inspiration. Possibly you can check out the example for some details.



  • First off, thank you for the quick reply. I have seen that, but there were a few problems. Being relatively new to C++, and VERY new to QT, the lack of any comments makes that program almost unreadable to me. It is possible that something in there does what I need, but I lack the knowledge to figure out which part it is, and then figure out how to extract it from the rest of the code so that I can use it in mine.

    My program, which we have already turned in once, is very different from that one, and if our next turn in changes everything to be more like that example, well he might not like that. Still, I'll try searching through that example and see if I can't figure it out, unless someone comes along with a different option.

    Thanks again good sir :)


  • Moderators

    [quote author="Krovlar" date="1333728185"]
    My program, which we have already turned in once, is very different from that one, and if our next turn in changes everything to be more like that example, well he might not like that. [/quote]
    Certainly it was not and is not my suggestion that you should not turn in the example's code.

    Good luck for your project. Hope you find a solution until Monday.



  • Is there any way I can download the sprites for that portedasteroids example? I tried copying all of the files, then creating some bs pictures to take the place of the sprites just to get it to compile, so that I could mess with things and see what changed. Sadly it just says The Program has unexpectedly finished. says it exited with code 0, whatever that means. I'm guessing that the code would not be up there if it didn't compile, so I am guessing that it is my images that are causing a problem, probably not being the right sizes etc.



  • Sounds like someone has skipped trigonometry lessons in school :)

    I guess what you want is to form a forward vector which is something like f = cos(angle)x + sin(angle)y depending on what you choose angle=0 to represent..

    Then you form a velocity vector of s*f (s=scalar speed) and use that to move your ship's position.

    • M

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.