sfml船没有包裹在窗口尽头

时间:2014-02-16 01:42:01

标签: c++ sfml

好朋友,我已经回到这里尝试再次获得一点帮助。问题在于,构建中的船没有包裹。也就是说,当它越过窗口限制时,它不再出现在窗口的另一侧。名为setmaxLocations()的函数非常奇怪。它会擦除船只,或者不设置最大位置。所以这是我的代码。忍受我,这是一个大项目。如果我搞砸了格式就让我知道,我会经常检查,所以当你告诉我时它会被修复。

主要实施cpp:

   #include <SFML/Graphics.hpp>
   #include "ship.h"

   const int WINDOW_WIDTH = 700;
   const int WINDOW_HEIGHT = 700;
   //==============================================================================
int main()
{
   Ship ship;


   sf::RenderWindow window( sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), 
    "Delta Quadrant", sf::Style::Titlebar | sf::Style::Close);

    window.setFramerateLimit(120); 
    // this causes loop to execute 120 times a second at most.
    // (a delay is automatically added after screen is drawn)

    //set's the limit on the ship's location to the window dimensions;
    ship.setmaxLocations(WINDOW_WIDTH, WINDOW_HEIGHT);

    //sets position of the ship in the middle of the screen
    ship.setLocation(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);

    while (window.isOpen())
{
    //----------------------------------------------------------
    //handle user input (events and keyboard keys being pressed)
    sf::Event event;
    while (window.pollEvent(event)) {
        if (event.type == sf::Event::Closed)
            window.close();
    }

    //turn left with press of left button
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        ship.rotateLeft();

    //turn right with press of right button
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        ship.rotateRight();

    //apply thrust with press of up button
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        ship.applyThrust();

    //----------------------------------------------------------
    //draw new frame
    window.clear();

    //draw ship
    ship.updateLocation();

    ship.draw(window);


    //redisplay window
    window.display();

}

return 0;
}

SpaceObject标题:

#ifndef SPACE_OBJECT_H
#define SPACE_OBJECT_H

#include "vector.h"

class SpaceObject  {
private: 
    Vector maxLocations;    //maximum allowable values for location
    Vector location;        //current location (x,y)
    Vector velocity;        //current velocity (in pixels/frame)
    double angleDeg;        //angle ship is facing, in degrees
    double radius;      //gross radius (for collision detection)

public:
    SpaceObject();

    //--------------------------------------------
    //mutators
    void setLocation(double x, double y);
    void setVelocity(double velocityX, double velocityY);
    void setAngle(double angDeg);
    void setRadius(double radius);

    //--------------------------------------------
    //accessors
    Vector getLocation();
    Vector getVelocity();
    double getAngle();
    double getRadius();
    //--------------------------------------------
    //others
    void updateLocation();
    void setmaxLocations(double x, double y);
 };

#endif 

SpaceObject源文件:

#include<cmath>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include"vector.h"
#include "SpaceObject.h"

//constructor
SpaceObject::SpaceObject() {
maxLocations.x = 500;
maxLocations.y = 500;
radius = 5;

location.x = 0;
location.y = 0;

velocity.x = 0;
velocity.y = 0;

angleDeg = 0;

}
//================================================================
//mutators

//================================================================
// Function:   setLocation
//
// Description: Sets the x and y values of the SpaceObject's location
// Argument list:
// x(I) - x coordinate for SpaceObject's location
// y(I) - y coordinate for SpaceObject's location 
//=================================================================*/
void SpaceObject::setLocation(double x, double y){

//Check and correct for the SpaceObject going out of bounds.
if (x < 0)
    location.x += maxLocations.x;
else if (x > maxLocations.x)
    location.x -= maxLocations.x;
else 
    location.x = x;

if (y < 0)
    location.y += maxLocations.y;
else if (y > maxLocations.y)
    location.y -= maxLocations.y;
else
    location.y = y;
}

//================================================================
// Function:   setVelocity
//
// Description: Sets the velocity for the x and y direction of the SpaceObject
// Argument list:
// velocityX(I) - sets the velocity in the x direction
// velocityY(I) - sets the velocity in the y direction
//=================================================================
void SpaceObject::setVelocity(double velocityX, double velocityY){
velocity.x = velocityX;
velocity.y = velocityY;
}
//================================================================
// Function:   setLocation
//
// Description: Sets the directional angle of the SpaceObject
// Argument List: 
// angDeg(I) - sets the SpaceObjects angle
//=================================================================*/
void SpaceObject::setAngle(double angDeg){
while (angDeg >= 360)
    angDeg -= 360;
while (angDeg < 0)
    angDeg += 360;
angleDeg = angDeg;   
}


//================================================================
//accessors
//================================================================
// Function: getRadius
// Description: Returns the Radius
//
// Return value: 
//  Radius - the SpaceObjects turning axis
//=================================================================*/
double SpaceObject::getRadius(){
return radius;
}
//================================================================
// Function: getLocation
// Description: Returns the x and y values of the SpaceObjects location
//
// Return value: 
//  location - the location of the SpaceObject
//=================================================================*/
Vector SpaceObject::getLocation(){
return location;
}
//================================================================
// Function: getVelocity
// Description: Returns the x and y values of the SpaceObjects directional velocity
//
// Return value: 
//  Velocity
//=================================================================*/
Vector SpaceObject::getVelocity(){
return velocity;
}
//================================================================
// Function:   getAngle
// Description: returns the angle 
// 
// Return value: 
//  Angle - the SpaceObjects direction
//=================================================================*/
double SpaceObject::getAngle(){
return angleDeg;
}

//============================================
//other functions
//================================================================
// Function:   setmaxLocations
//
// Description: Sets the x and y values of the SpaceObject's max possible area of movement 
// Argument list:
// x(I) - max width of SpaceObject's possible positions
// y(I) - max heigth of SpaceObject's possible positions
//=================================================================*/
void SpaceObject::setmaxLocations(double x, double y){
 maxLocations.x = x;
 maxLocations.y = y;
}
//================================================================
// Function:   updateLocations
//
// Description: Sets the x and y values of the SpaceObject's location while including the change with velocity
//=================================================================*/
void SpaceObject::updateLocation(){
location.x = location.x + velocity.x;
location.y = location.y + velocity.y;
 }  

出货头文件:

#ifndef SHIP_H
#define SHIP_H

#include "SpaceObject.h"

class Ship: public SpaceObject {
 public:
Ship();
void rotateLeft(); 
void rotateRight();
void applyThrust();
void draw(sf::RenderWindow& win);
};

#endif

发货源文件:

#include<cmath>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include"vector.h"
#include "ship.h"


//Constants
const double PI = 3.14159;
const double THRUST = 0.005;
const double TURN_SPEED = 1;

Ship::Ship() {

}
//============================================
//other functions
//================================================================
// Function:  rotateLeft
//
// Description: turns the ship left by subtracting from the ship's angle
//=================================================================*/
void Ship::rotateLeft(){
int newAngle;
newAngle = getAngle() - TURN_SPEED;
setAngle(newAngle);
}
//================================================================
// Function:  rotateRight
//
// Description: turns the ship Right by adding to the ship's angle
//=================================================================*/
void Ship::rotateRight(){
int newAngle;
newAngle = getAngle() + TURN_SPEED;
setAngle(newAngle);
}
//================================================================
// Function:   applyThrust
//
// Description: Sets the x and y value of the ship's movement
//=================================================================*/
void Ship::applyThrust(){

double forcex = cos((getAngle()-90)*PI/180) * .005;
double forcey = sin((getAngle()-90)*PI/180) * .005;
setVelocity(getVelocity().x + forcex, getVelocity().y + forcey);

}

//--------------------------------------------------------------------------
// Function: draw
// Description: draws the ship on the given window
// Parameters:
//  win - the window for drawing the ship
//--------------------------------------------------------------------------
void Ship::draw(sf::RenderWindow& win) {

// draw ship
sf::ConvexShape ship;
ship.setPointCount(3);
ship.setPoint(0, sf::Vector2f(10, 0));
ship.setPoint(1, sf::Vector2f(0, 25));
ship.setPoint(2, sf::Vector2f(20, 25));

sf::Vector2f midpoint(10,15);
ship.setOrigin(midpoint);

ship.setFillColor(sf::Color(0, 0, 0));
ship.setOutlineThickness(1);
ship.setOutlineColor(sf::Color(255, 255, 255));

ship.setPosition(getLocation().x, getLocation().y);
ship.setRotation(getAngle());
win.draw(ship);
}

矢量头文件(仅为了清晰起见):

#ifndef VECTOR_H
#define VECTOR_H
     struct Vector{
      float x;
      float y;
     };
#endif

1 个答案:

答案 0 :(得分:0)

似乎SpaceObject::setLocation()进行实际检查以确定船是否在边界内,但此功能仅在main()开始时调用一次。它需要在循环内调用,以便它可以不断检查船舶是否有有效位置。我建议在SpaceObject::updateLocation()

内调用它