标识符“ <名称>”未定义

时间:2019-02-13 04:37:56

标签: c++ visual-studio sfml

在创建Frogger游戏时遇到一些问题。我已经将汽车和青蛙设置为外部对象(它们没有什么问题,并且可以正常工作),但是我试图通过将SFML矩形的创建置于它们自己的单独函数中来清理main()区域。 / p>

问题在于,我在自己的函数中创建的任何对象都没有在最后绘制。如果我将完全相同的代码放到main()函数中,那么它将起作用,但是在createCars()函数中,它将不起作用。

#include <SFML/Graphics.hpp>
#include <random>
#include "Frog.h"
#include "Cars.h"

using namespace std;


int temp = 50;

//Set size of the playable window
int windowW = 1100;
int windowH = windowW;

//Set the size of the grid cells based on the window size
const int gridSize = windowW / 11;

//Instantiate the variables for the frogs starting location
float startingX = (windowW / 2) - (gridSize / 2);
float startingY = (windowH - gridSize);


//Create 2 variables for the current location of the frog
float posX = startingX;
float posY = startingY;

//Set a boolean to test if a key is being pressed
bool keyDown = false;

//Set the speed of each row of cars
int Row1Speed = 10;
int Row2Speed = 7;
int Row3Speed = 3;
int Row4Speed = 5;
sf::Vector2f carMove;

int CarReset = windowW / 2 * -1;

//This method resets the game. It is called when the frog dies or the level is complete.
void resetGame() {}
void createBackground() {
    sf::RectangleShape BG1(sf::Vector2f(windowW, windowH));
    BG1.setPosition(0, 0);
    BG1.setFillColor(sf::Color::Green);
    sf::RectangleShape BG2(sf::Vector2f(windowW, gridSize * 4));
    BG2.setPosition(0, windowH - gridSize * 5);
    BG2.setFillColor(sf::Color(105, 105, 105));
    sf::RectangleShape BG3(sf::Vector2f(windowW, gridSize * 4));
    BG3.setPosition(0, windowH - gridSize * 10);
    BG3.setFillColor(sf::Color::Blue);
}
void createCars() {
    /*Cars Row1Car1;
    Row1Car1.NewCar(gridSize, 1, windowW);

    Cars Row2Car1;
    Row2Car1.NewCar(gridSize, 2, windowW);

    Cars Row3Car1;
    Row3Car1.NewCar(gridSize, 3, windowW);

    Cars Row4Car1;
    Row4Car1.NewCar(gridSize, 4, windowW);*/
}
void createFrogs() {

}
void createLogs() {

}

int main()
{
    sf::RenderWindow Game(sf::VideoMode(windowW, windowH), "An Frogger!");
    Game.setFramerateLimit(60);

    createBackground();
    createCars();
    createLogs();
    createFrogs();
    Frog Frogger;
    Frogger.NewFrog(gridSize, windowW, windowH);
    Frogger.move(posX, posY);

    Cars Row1Car1;
    Row1Car1.NewCar(gridSize, 1, windowW);

    Cars Row2Car1;
    Row2Car1.NewCar(gridSize, 2, windowW);

    Cars Row3Car1;
    Row3Car1.NewCar(gridSize, 3, windowW);

    Cars Row4Car1;
    Row4Car1.NewCar(gridSize, 4, windowW);


    while (Game.isOpen())
    {
        sf::Event event;
        while (Game.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                Game.close();
        }

        if (event.type == sf::Event::KeyPressed && keyDown == false) {

            if (event.key.code == sf::Keyboard::Up){
                posY -= gridSize;
                Frogger.move(posX, posY);
                keyDown = true;
            }
            else if (event.key.code == sf::Keyboard::Down) {
                posY += gridSize;
                Frogger.move(posX, posY);
                keyDown = true;
            }
            else if (event.key.code == sf::Keyboard::Left) {
                posX -= gridSize;
                Frogger.move(posX, posY);
                keyDown = true;
            }
            else if(event.key.code == sf::Keyboard::Right) {
                posX += gridSize;
                Frogger.move(posX, posY);
                keyDown = true;
            }
            else {}

            if (posX >= windowW - 1 || posX <= -1 || posY >= windowH - 1 || posY <= -1) {
                posX = startingX;
                posY = startingY;
                Frogger.move(posX, posY);
                Frogger.changeColour(rand() % 256, rand() % 256, rand() % 256);


            }
        }

        if (event.type == sf::Event::KeyReleased) {
            keyDown = false;
        }


        carMove = Row1Car1.Car.getPosition();
        if (carMove.x + Row1Speed >= windowW) {
            Row1Car1.move(CarReset, carMove.y);
        }
        else {
            Row1Car1.move(carMove.x + Row1Speed, carMove.y);
        }

        carMove = Row2Car1.Car.getPosition();
        if (carMove.x + Row2Speed >= windowW) {
            Row2Car1.move(CarReset, carMove.y);
        }
        else {
            Row2Car1.move(carMove.x + Row2Speed, carMove.y);
        }

        carMove = Row3Car1.Car.getPosition();
        if (carMove.x + Row3Speed >= windowW) {
            Row3Car1.move(CarReset, carMove.y);
        }
        else {
            Row3Car1.move(carMove.x + Row3Speed, carMove.y);
        }

        carMove = Row4Car1.Car.getPosition();
        if (carMove.x + Row4Speed >= windowW) {
            Row4Car1.move(CarReset, carMove.y);
        }
        else {
            Row4Car1.move(carMove.x + Row1Speed, carMove.y);
        }

        Game.clear();
        Game.draw(BG1);
        Game.draw(BG2);
        Game.draw(BG3);
        Game.draw(Row1Car1.Car);
        Game.draw(Row2Car1.Car);
        Game.draw(Row3Car1.Car);
        Game.draw(Row4Car1.Car);
        Game.draw(Frogger.Frogger);
        Game.display();
    }

    return 0;
}

任何帮助将不胜感激。另外,我知道我的代码杂乱且效率低下,但我只是想让它在atm上正常工作(我也是n00b)

0 个答案:

没有答案