Krovlar
6th April 2012, 16:11
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 rar file containing my QT4 project ready to compile and run. Thank you again for any help you can offer.
http://www.box.com/s/4898353af9c9e12e9fd0
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 rar file containing my QT4 project ready to compile and run. Thank you again for any help you can offer.
http://www.box.com/s/4898353af9c9e12e9fd0