Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GAME..H
- #ifndef GAME_H
- #define GAME_H
- #include <SFML/Graphics.hpp>
- class Game
- {
- public:
- Game();
- void run();
- private:
- void processEvents();
- void update();
- void render();
- void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
- private:
- sf::RenderWindow mWindow;
- sf::CircleShape mCircle;
- bool mIsMovingUp;
- };
- #endif
- // END GAME.H
- //GAME.CPP
- #include "Book/Games.h"
- Game::Game()
- :mWindow(sf::VideoMode(640,480), "SFML PROGRAM")
- ,mCircle()
- ,mIsMovingUp(false)
- {
- mCircle.setRadius(40.f);
- mCircle.setPosition(100.f, 100.f);
- mCircle.setFillColor(sf::Color::Red);
- }
- void Game::run()
- {
- while (mWindow.isOpen())
- {
- processEvents();
- update();
- render();
- }
- }
- void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
- {
- if (key == sf::Keyboard::W)
- mIsMovingUp = isPressed;
- }
- void Game::processEvents()
- {
- sf::Event event;
- while (mWindow.pollEvent(event))
- {
- switch (event.type)
- {
- case sf::Event::KeyPressed:
- handlePlayerInput(event.key.code, true);
- break;
- case sf::Event::KeyReleased:
- handlePlayerInput(event.key.code, false);
- break;
- case sf::Event::Closed:
- mWindow.close();
- break;
- }
- }
- }
- void Game::render()
- {
- mWindow.clear();
- mWindow.draw(mCircle);
- mWindow.display();
- }
- void Game::update()
- {
- sf::Vector2f movement(0.f, 0.f);
- if (mIsMovingUp)
- movement.y -= 1.f;
- mCircle.move(movement);
- }
- //END GAME.CPP
- //MAIN.CPP
- #include "Book/Games.h"
- int main()
- {
- Game game;
- game.run();
- }
- // END MAIN.CPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement