Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1. #pragma region Bibliotheken
  2. //SFML Bibliotheken
  3. #include <SFML/Graphics.hpp>
  4.  
  5. //C++ Bilbiotheken
  6. #include <iostream>
  7. #include <string>
  8. #include <time.h>
  9.  
  10. //Headerdateien
  11. #include "Texturmanager.h"
  12. #include "Gamelogic.h"
  13. #include "Character.h"
  14.  
  15. //prototypes
  16. int set_coins(vector<sf::RectangleShape>& object, int position);
  17. void draw_coins(sf::RenderWindow& window, vector<sf::RectangleShape>& object, int position);
  18.  
  19. using namespace std;
  20. #pragma endregion
  21.  
  22. int main()
  23. {
  24.     #pragma region variables
  25.     //tick
  26.     int loops = 0;                              //loops and maxFrameskip are there to avoid the loop to continue more than 5 times if the gameupdate takes too long
  27.     sf::Clock frameClock;                       //clock for the game loop
  28.     const int maxFrameskip = 5;                
  29.     float updateRate = 1000/60;                 //updating the game logic 60 times per second
  30.  
  31.     //graphics
  32.     //sf::Sprite character;                     //sprite of the character
  33.     sf::RectangleShape coin;                    //square "coin" for testing purpose
  34.     sf::RectangleShape bgWhite;                 //white background
  35.     sf::RectangleShape testCharacter;           //square character for testing purpose
  36.  
  37.     //class
  38.     game_logic Gamelogik;                       //instance of the class game_logic which contains the functions for the game logic like update
  39.     set_character charInstance;                 //instance of the class set_character to manage settings for the character like the position or size
  40.     texture_manager Texturmanager;              //instance of the class texture_manager to give the sprites textures
  41.  
  42.     //control
  43.     bool enableKeys = true;                     //bool to enable the keys
  44.  
  45.     //others
  46.     int grid = 6;                               //vertical grid the player moves in. 6 is the position of the player
  47.     int moveUp = -50;                           //number of pixels the player can move up
  48.     int moveDown = 50;                          //number of pixels the player can move down
  49.     int whichCoin = 0;                          //check which coin of the vector to move
  50.     int prevCoin = 0;                           //variable for checking
  51.     int coinPosition = 750;                     //position of the coins (increasing by every tick of the game)
  52.  
  53.     vector<sf::RectangleShape>coins(12, sf::RectangleShape(coin));              //vector to create 12 coins per column
  54.     #pragma endregion
  55.  
  56.     #pragma region window
  57.     sf::RenderWindow mainWindow(sf::VideoMode(800, 600), "Spiel", sf::Style::Close);
  58.     mainWindow.setFramerateLimit(60);
  59.     #pragma endregion
  60.  
  61.     #pragma region graphical settings
  62.     //white background
  63.     bgWhite.setSize(sf::Vector2f(800, 600));
  64.     bgWhite.setFillColor(sf::Color(255, 255, 255));
  65.     bgWhite.setPosition(0, 0);
  66.  
  67.     //character
  68.     //character = charInstance.Erstellung(sf::IntRect(0,0,256, 252), sf::Vector2f(0,0));
  69.     //character.setTexture(Texturmanager.get_texture("Data/Jump.png"));
  70.     testCharacter.setSize(sf::Vector2f(50, 50));
  71.     testCharacter.setFillColor(sf::Color(sf::Color::Red));
  72.     testCharacter.setPosition(100, 250);
  73.     #pragma endregion
  74.    
  75.     whichCoin = set_coins(coins, coinPosition);
  76.  
  77.     #pragma region windowloop
  78.     while (mainWindow.isOpen())
  79.     {
  80.         loops = 0;
  81.         if(frameClock.getElapsedTime().asMilliseconds() >= updateRate && loops < maxFrameskip)
  82.         {
  83.             frameClock.restart();
  84.             coins[whichCoin].move(-2.5, 0);
  85.             if(coins[whichCoin].getPosition().x + 50 <= mainWindow.getSize().x - 50)
  86.             {
  87.                 whichCoin = set_coins(coins, coinPosition);
  88.             }
  89.  
  90.             loops++;
  91.         }
  92.  
  93.  
  94.  
  95.         #pragma region eventloop
  96.         sf::Event event;
  97.         while (mainWindow.pollEvent(event))
  98.         {
  99.             if (event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
  100.                 mainWindow.close();
  101.  
  102.             if (event.type == sf::Event::LostFocus)
  103.             {
  104.                 mainWindow.setActive(false);
  105.                 enableKeys = false;
  106.             }
  107.  
  108.             if (event.type == sf::Event::GainedFocus)
  109.             {
  110.                 mainWindow.setActive(true);
  111.                 enableKeys = true;
  112.             }
  113.  
  114.             if(enableKeys)
  115.             {
  116.                 if(event.key.code == sf::Keyboard::Up)
  117.                 {
  118.                     if(grid == 1)
  119.                     {
  120.                         moveUp = 0;
  121.                     }
  122.  
  123.                     else
  124.                     {
  125.                         moveUp = -50;
  126.                         testCharacter.move(0, moveUp);
  127.                         grid--;
  128.                     }
  129.                 }
  130.  
  131.                 if(event.key.code == sf::Keyboard::Down)
  132.                 {
  133.                     if(grid == 12)
  134.                     {
  135.                         moveDown = 0;
  136.                     }
  137.  
  138.                     else
  139.                     {
  140.                         moveDown = 50;
  141.                         testCharacter.move(0, moveDown);
  142.                         grid++;
  143.                     }
  144.                 }
  145.  
  146.                 /* user input
  147.                 if(event.type == event.TextEntered)
  148.                 {
  149.                     if(event.text.unicode < 128)
  150.                         cout << (char)(event.text.unicode);
  151.                 }
  152.  
  153.                 else if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Return)
  154.                     cout << endl;
  155.                 */
  156.             }
  157.         }
  158.         #pragma endregion
  159.  
  160.         //mainWindow.clear();
  161.  
  162.         mainWindow.draw(bgWhite);      
  163.         mainWindow.draw(testCharacter);
  164.         /*for(int i = 0; i < coins.size(); i++)
  165.         {
  166.             mainWindow.draw(coins[i]);
  167.         }*/
  168.         draw_coins(mainWindow, coins, whichCoin);
  169.  
  170.         mainWindow.display();
  171.     }
  172.     #pragma endregion
  173.  
  174.     return EXIT_SUCCESS;
  175. }
  176.  
  177. int set_coins(vector<sf::RectangleShape>& object, int position)
  178. {
  179.     int maxSpawn = 0;                       //max spawnable coins per column
  180.     int setSpawn[12];                       //sets the spawn for each of the 12 coins
  181.  
  182.     srand(time(NULL));                      //reset the previous random numbers
  183.  
  184.     for(int a = 0; a <= 11; a++)
  185.     {
  186.         setSpawn[a] = rand() % 2;           //set 0 or 1 for each of the 12 coins
  187.         //cout << setSpawn[a] << endl;      //test to check the spawns
  188.     }
  189.  
  190.     for(int i = 0; i < object.size(); i++)
  191.     {
  192.         if(maxSpawn < 1)                    //allow only 1 coin to spawn per column
  193.         {
  194.             if(setSpawn[i] == 1)            //if the spawn of the coin is 1
  195.             {
  196.                 object[i].setSize(sf::Vector2f(50, 50));
  197.                 object[i].setFillColor(sf::Color::Green);
  198.                 object[i].setPosition(position, i*50);
  199.  
  200.                 return i;
  201.             }
  202.         }
  203.  
  204.         maxSpawn = maxSpawn + setSpawn[i];
  205.     }
  206. }
  207.  
  208. void draw_coins(sf::RenderWindow& window, vector<sf::RectangleShape>& object, int position)
  209. {
  210.     window.draw(object[position]);
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement