Advertisement
MisterEpic

Game.cpp

Apr 6th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include "Game.h"
  2. #include "Player.h"
  3. #include "ball.h"
  4. #include "Human.h"
  5. #include "Computer.h"
  6.  
  7. #include <SFML/Graphics.hpp>
  8. #include <SFML/Audio.hpp>
  9.  
  10. using namespace sf;
  11.  
  12. Game::Game()
  13. {
  14.     //App = new RenderWindow(VideoMode(800, 600, 32), "Pong");
  15.     App.Create(VideoMode(800, 600, 32), "Pong");
  16.  
  17.     SoundBuffer scoreSoundBuffer;
  18.     scoreSoundBuffer.LoadFromFile("datas/pong/score.wav");
  19.     scoreSound.SetBuffer(scoreSoundBuffer);
  20.  
  21.     Image backgroundI;
  22.     backgroundI.LoadFromFile("datas/pong/background.png");
  23.     background.SetImage(backgroundI);
  24.  
  25.     ball = new Ball();
  26.     player1 = new Human(&App, 1);
  27.     player2 = new Computer (&App, 2, ball);
  28. }
  29.  
  30. void Game::run()
  31. {
  32.     bool IsPlaying = true;
  33.     while (App.IsOpened())
  34.     {
  35.         // Handle events
  36.         Event Event;
  37.         while (App.GetEvent(Event))
  38.         {
  39.             // Window closed or escape key pressed : exit
  40.             if ((Event.Type == Event::Closed) ||
  41.                ((Event.Type == Event::KeyPressed) && (Event.Key.Code == Key::Escape)))
  42.             {
  43.                 App.Close();
  44.                 break;
  45.             }
  46.         }
  47.  
  48.         if (IsPlaying)
  49.         {
  50.             player1->refresh();
  51.             player2->refresh();
  52.             IsPlaying = ball->refresh(player1->getPaddleSpriteP(), player2->getPaddleSpriteP());
  53.         }
  54.  
  55.         // Clear the window
  56.         App.Clear();
  57.  
  58.         // Draw the background, paddles and ball sprites
  59.         App.Draw(background);
  60.         App.Draw(player1->getPaddleSprite());
  61.         App.Draw(player2->getPaddleSprite());
  62.         App.Draw(ball->getSprite());
  63.  
  64.         // Display things on screen
  65.         App.Display();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement