Advertisement
AaronThomsen

Untitled

Jun 18th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <ctime>
  6. using namespace sf;
  7.  
  8. void moveBackground(RenderWindow& w, Sprite& s);
  9. void printText(RenderWindow& w, Text& score, bool updateScore);
  10. bool updateBall(RenderWindow& w, Sprite& s, Sprite& p1, Sprite& p2);
  11.  
  12. int main() {
  13.     RenderWindow w(VideoMode(1000, 500), "Super Mario Bros. Pong!");
  14.     Texture bg, paddleTexture, ballTexture;
  15.     Image windowIcon;
  16.     Font roboto;
  17.     std::srand(std::time(0));
  18.  
  19.     if (!bg.loadFromFile("background_2.png") || !windowIcon.loadFromFile("icon.png") || !roboto.loadFromFile("Roboto-bold.ttf") || !paddleTexture.loadFromFile("t.png")
  20.         || !ballTexture.loadFromFile("ball.png")) {
  21.         exit(-1);
  22.     }
  23.  
  24.     bg.setSmooth(1);
  25.     ballTexture.setSmooth(1);
  26.     paddleTexture.setSmooth(1);
  27.     paddleTexture.setRepeated(1);
  28.  
  29.     //Paddles
  30.     Sprite paddle1(paddleTexture, IntRect(0, 0, paddleTexture.getSize().x, paddleTexture.getSize().y));
  31.     paddle1.rotate(90);
  32.     paddle1.setOrigin(Vector2f((float)paddle1.getGlobalBounds().width / 2, (float)paddle1.getGlobalBounds().height / 2));
  33.     paddle1.setPosition(Vector2f(10.f, (float)w.getSize().y / 2));
  34.     Sprite paddle2(paddleTexture, IntRect(0, 0, paddleTexture.getSize().x, paddleTexture.getSize().y));
  35.     paddle2.rotate(-90);
  36.     paddle2.setOrigin(Vector2f((float)paddle2.getGlobalBounds().width / 2, (float)paddle2.getGlobalBounds().height / 2));
  37.     paddle2.setPosition(Vector2f((float)w.getSize().x - 10.f, (float)w.getSize().y / 2));
  38.  
  39.     //Balls
  40.     Sprite ball(ballTexture, IntRect(0, 0, ballTexture.getSize().x, ballTexture.getSize().y));
  41.     ball.setOrigin(Vector2f((float)ball.getGlobalBounds().width / 2, (float)ball.getGlobalBounds().height / 2));
  42.     ball.setPosition(Vector2f(w.getSize().x / 2.f, w.getSize().y / 2.f));
  43.     ball.scale(.20f, .20f);
  44.  
  45.     //Set Window Background
  46.     Sprite bgSprite;
  47.     bgSprite.setTexture(bg);
  48.  
  49.     //bgSprite.setColor(Color(236, 68, 68));
  50.  
  51.     //Set Window Icon
  52.     Vector2u iconSize = windowIcon.getSize();
  53.     w.setIcon(iconSize.x, iconSize.y, windowIcon.getPixelsPtr());
  54.  
  55.     //Write Message
  56.     Text score;
  57.     score.setFont(roboto);
  58.     score.setString("SCORE:");
  59.     FloatRect scoreSize = score.getGlobalBounds();
  60.     score.setPosition(Vector2f((float)w.getSize().x - scoreSize.width, 10));
  61.     score.setCharacterSize(20);
  62.     score.setOutlineColor(Color::Black);
  63.     score.setOutlineThickness(5);
  64.  
  65.     //Set Views
  66.     View standard = w.getView(),
  67.         fixed = standard;
  68.  
  69.     while (w.isOpen()) {
  70.         Event e;
  71.         while (w.pollEvent(e)) {
  72.             switch (e.type) {
  73.             case Event::Closed:
  74.                 w.close();
  75.                 break;
  76.             case Event::KeyPressed:
  77.                 if (e.key.code == Keyboard::W) {
  78.                     if (paddle1.getGlobalBounds().top > 0)
  79.                         paddle1.move(Vector2f(0, -20));
  80.                 }
  81.                 else if (e.key.code == Keyboard::S) {
  82.                     if (paddle1.getGlobalBounds().top + paddle1.getGlobalBounds().height < w.getSize().y)
  83.                         paddle1.move(Vector2f(0, 20));
  84.                 }
  85.                 else if (e.key.code == Keyboard::Up) {
  86.                     if (paddle2.getGlobalBounds().top > 0)
  87.                         paddle2.move(Vector2f(0, -20));
  88.                 }
  89.                 else if (e.key.code == Keyboard::Down) {
  90.                     if (paddle2.getGlobalBounds().top + paddle2.getGlobalBounds().height < w.getSize().y)
  91.                         paddle2.move(Vector2f(0, 20));
  92.                 }
  93.             }
  94.         }
  95.  
  96.         w.clear();
  97.  
  98.         moveBackground(w, bgSprite);
  99.  
  100.         w.setView(standard);
  101.         w.draw(bgSprite);
  102.  
  103.         w.setView(w.getDefaultView());
  104.         w.draw(paddle1);
  105.         w.draw(paddle2);
  106.         printText(w, score, updateBall(w, ball, paddle1, paddle2));
  107.         w.display();
  108.     }
  109.     return 0;
  110. }
  111.  
  112. void moveBackground(RenderWindow & w, Sprite & s) {
  113.     static int dx = 1, dy = 1;
  114.  
  115.     FloatRect sBounds = s.getGlobalBounds();
  116.     Vector2u wSize = w.getSize();
  117.  
  118.     if (dx == -1 && sBounds.left + sBounds.width <= wSize.x)
  119.         dx *= -1;
  120.     else if (sBounds.left >= 0)
  121.         dx *= -1;
  122.  
  123.     if (dy == 1 && sBounds.top >= 0)
  124.         dy *= -1;
  125.     else if (sBounds.top + sBounds.height <= wSize.y)
  126.         dy *= -1;
  127.  
  128.     s.move(.01 * dx, .01 * dy);
  129. }
  130.  
  131. void printText(RenderWindow& w, Text& score, bool updateScore) {
  132.     static int scoreCount = 0;
  133.  
  134.     if (updateScore)
  135.         scoreCount++;
  136.  
  137.     std::string count = std::to_string(scoreCount);
  138.  
  139.     score.setString("Score: " + count);
  140.     w.draw(score);
  141. }
  142.  
  143. bool updateBall(RenderWindow& w, Sprite& ball, Sprite& p1, Sprite& p2) {
  144.     static float dx = -1, dy = -1;
  145.     bool hitPaddle = false;
  146.  
  147.     Vector2f ballCoord = ball.getPosition();
  148.     float randFloat = std::rand() % 100 / 100.f + 1;
  149.  
  150.     if (ballCoord.x <= 0 || ballCoord.x >= w.getSize().x) {
  151.         dx *= -1;
  152.         ball.rotate(40);
  153.     }
  154.  
  155.     if (ballCoord.y <= 0 || ballCoord.y >= w.getSize().y) {
  156.         dy *= -1;
  157.         ball.rotate(-40);
  158.     }
  159.  
  160.     if (ball.getGlobalBounds().intersects(p1.getGlobalBounds()) ||
  161.         ball.getGlobalBounds().intersects(p2.getGlobalBounds())) {
  162.         dx *= (-randFloat);
  163.         dy *= (-randFloat);
  164.         ball.rotate(30);
  165.         hitPaddle = true;
  166.     }
  167.     dx = dx >= 2 ? dx - 1 : dx;
  168.     dy = dy >= 2 ? dy - 1 : dy;
  169.     ball.move(Vector2f(.5f * dx, .5f * dy));
  170.     w.draw(ball);
  171.     return hitPaddle;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement