У меня возникло несколько проблем с созданием игры Frogger.Я настроил машины и лягушку как внешние объекты (в них нет ничего плохого, и они отлично работают), но я пытаюсь очистить свою область main (), поместив создание прямоугольников SFML в их собственные отдельные функции.
Проблема в том, что любые объекты, которые я создаю в своей собственной функции, не рисуются в конце.Если я добавлю точно такой же код в функцию 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)