Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma region Bibliotheken
- //SFML Bibliotheken
- #include <SFML/Graphics.hpp>
- //C++ Bilbiotheken
- #include <iostream>
- #include <string>
- #include <time.h>
- //Headerdateien
- #include "Texturmanager.h"
- #include "Gamelogic.h"
- #include "Character.h"
- //prototypes
- int set_coins(vector<sf::RectangleShape>& object, int position);
- void draw_coins(sf::RenderWindow& window, vector<sf::RectangleShape>& object, int position);
- using namespace std;
- #pragma endregion
- int main()
- {
- #pragma region variables
- //tick
- int loops = 0; //loops and maxFrameskip are there to avoid the loop to continue more than 5 times if the gameupdate takes too long
- sf::Clock frameClock; //clock for the game loop
- const int maxFrameskip = 5;
- float updateRate = 1000/60; //updating the game logic 60 times per second
- //graphics
- //sf::Sprite character; //sprite of the character
- sf::RectangleShape coin; //square "coin" for testing purpose
- sf::RectangleShape bgWhite; //white background
- sf::RectangleShape testCharacter; //square character for testing purpose
- //class
- game_logic Gamelogik; //instance of the class game_logic which contains the functions for the game logic like update
- set_character charInstance; //instance of the class set_character to manage settings for the character like the position or size
- texture_manager Texturmanager; //instance of the class texture_manager to give the sprites textures
- //control
- bool enableKeys = true; //bool to enable the keys
- //others
- int grid = 6; //vertical grid the player moves in. 6 is the position of the player
- int moveUp = -50; //number of pixels the player can move up
- int moveDown = 50; //number of pixels the player can move down
- int whichCoin = 0; //check which coin of the vector to move
- int prevCoin = 0; //variable for checking
- int coinPosition = 750; //position of the coins (increasing by every tick of the game)
- vector<sf::RectangleShape>coins(12, sf::RectangleShape(coin)); //vector to create 12 coins per column
- #pragma endregion
- #pragma region window
- sf::RenderWindow mainWindow(sf::VideoMode(800, 600), "Spiel", sf::Style::Close);
- mainWindow.setFramerateLimit(60);
- #pragma endregion
- #pragma region graphical settings
- //white background
- bgWhite.setSize(sf::Vector2f(800, 600));
- bgWhite.setFillColor(sf::Color(255, 255, 255));
- bgWhite.setPosition(0, 0);
- //character
- //character = charInstance.Erstellung(sf::IntRect(0,0,256, 252), sf::Vector2f(0,0));
- //character.setTexture(Texturmanager.get_texture("Data/Jump.png"));
- testCharacter.setSize(sf::Vector2f(50, 50));
- testCharacter.setFillColor(sf::Color(sf::Color::Red));
- testCharacter.setPosition(100, 250);
- #pragma endregion
- whichCoin = set_coins(coins, coinPosition);
- #pragma region windowloop
- while (mainWindow.isOpen())
- {
- loops = 0;
- if(frameClock.getElapsedTime().asMilliseconds() >= updateRate && loops < maxFrameskip)
- {
- frameClock.restart();
- coins[whichCoin].move(-2.5, 0);
- if(coins[whichCoin].getPosition().x + 50 <= mainWindow.getSize().x - 50)
- {
- whichCoin = set_coins(coins, coinPosition);
- }
- loops++;
- }
- #pragma region eventloop
- sf::Event event;
- while (mainWindow.pollEvent(event))
- {
- if (event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
- mainWindow.close();
- if (event.type == sf::Event::LostFocus)
- {
- mainWindow.setActive(false);
- enableKeys = false;
- }
- if (event.type == sf::Event::GainedFocus)
- {
- mainWindow.setActive(true);
- enableKeys = true;
- }
- if(enableKeys)
- {
- if(event.key.code == sf::Keyboard::Up)
- {
- if(grid == 1)
- {
- moveUp = 0;
- }
- else
- {
- moveUp = -50;
- testCharacter.move(0, moveUp);
- grid--;
- }
- }
- if(event.key.code == sf::Keyboard::Down)
- {
- if(grid == 12)
- {
- moveDown = 0;
- }
- else
- {
- moveDown = 50;
- testCharacter.move(0, moveDown);
- grid++;
- }
- }
- /* user input
- if(event.type == event.TextEntered)
- {
- if(event.text.unicode < 128)
- cout << (char)(event.text.unicode);
- }
- else if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Return)
- cout << endl;
- */
- }
- }
- #pragma endregion
- //mainWindow.clear();
- mainWindow.draw(bgWhite);
- mainWindow.draw(testCharacter);
- /*for(int i = 0; i < coins.size(); i++)
- {
- mainWindow.draw(coins[i]);
- }*/
- draw_coins(mainWindow, coins, whichCoin);
- mainWindow.display();
- }
- #pragma endregion
- return EXIT_SUCCESS;
- }
- int set_coins(vector<sf::RectangleShape>& object, int position)
- {
- int maxSpawn = 0; //max spawnable coins per column
- int setSpawn[12]; //sets the spawn for each of the 12 coins
- srand(time(NULL)); //reset the previous random numbers
- for(int a = 0; a <= 11; a++)
- {
- setSpawn[a] = rand() % 2; //set 0 or 1 for each of the 12 coins
- //cout << setSpawn[a] << endl; //test to check the spawns
- }
- for(int i = 0; i < object.size(); i++)
- {
- if(maxSpawn < 1) //allow only 1 coin to spawn per column
- {
- if(setSpawn[i] == 1) //if the spawn of the coin is 1
- {
- object[i].setSize(sf::Vector2f(50, 50));
- object[i].setFillColor(sf::Color::Green);
- object[i].setPosition(position, i*50);
- return i;
- }
- }
- maxSpawn = maxSpawn + setSpawn[i];
- }
- }
- void draw_coins(sf::RenderWindow& window, vector<sf::RectangleShape>& object, int position)
- {
- window.draw(object[position]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement