Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include "Ball.h"
- #include "Paddle.h"
- int main() {
- sf::RenderWindow window;
- sf::Vector2i centerWindow((sf::VideoMode::getDesktopMode().width / 2) - 445, (sf::VideoMode::getDesktopMode().height / 2) - 480);
- window.create(sf::VideoMode(900, 900), "SFML Project", sf::Style::Titlebar | sf::Style::Close);
- window.setPosition(centerWindow);
- window.setKeyRepeatEnabled(true);
- //Paddle Objects:
- Paddle paddle1({ 10, 100 });
- Paddle paddle2({ 10, 100 });
- paddle1.setPos({ 50, 200 });
- paddle2.setPos({ 850, 200 });
- //Ball Object:
- Ball ball({ 20, 20 });
- ball.setPos({ 400, 200 });
- //Main Loop:
- while (window.isOpen()) {
- sf::Event Event;
- const float moveSpeed = 0.2;
- //Player1 Movement:
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
- paddle1.move({ 0, -moveSpeed });
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
- paddle1.move({ 0, moveSpeed });
- }
- //Player2 Movement:
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
- paddle2.move({ 0, -moveSpeed });
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
- paddle2.move({ 0, moveSpeed });
- }
- //Event Loop:
- while (window.pollEvent(Event)) {
- switch (Event.type) {
- case sf::Event::Closed:
- window.close();
- }
- }
- static bool goingLeft = true;
- static bool goingRight = false;
- if (goingLeft == true && paddle1.isCollidingWith(ball) == false) {
- ball.move({ -moveSpeed, 0 });
- }
- else if (goingLeft == true && paddle1.isCollidingWith(ball) == true) {
- goingRight = true;
- goingLeft = false;
- }
- if (goingRight == true && paddle2.isCollidingWith(ball) == false) {
- ball.move({ moveSpeed, 0 });
- }
- else if (goingRight == true && paddle2.isCollidingWith(ball) == true) {
- goingRight = false;
- goingLeft = true;
- }
- window.clear();
- paddle1.drawTo(window);
- paddle2.drawTo(window);
- ball.drawTo(window);
- window.display();
- }
- }
- #pragma once
- #include <SFML\Graphics.hpp>
- #include "Ball.h"
- class Paddle {
- public:
- Paddle(sf::Vector2f size) {
- paddle.setSize(size);
- }
- void drawTo(sf::RenderWindow &window) {
- window.draw(paddle);
- }
- void move(sf::Vector2f distance) {
- paddle.move(distance);
- }
- bool isCollidingWith(Ball ball) {
- if (ball.getGlobalBounds().intersects(paddle.getGlobalBounds())) {
- return true;
- }
- return false;
- }
- void setPos(sf::Vector2f newPos) {
- paddle.setPosition(newPos);
- }
- private:
- sf::RectangleShape paddle;
- };
- #pragma once
- #include <SFML\Graphics.hpp>
- class Ball {
- public:
- Ball(sf::Vector2f size) {
- ball.setSize(size);
- }
- void drawTo(sf::RenderWindow &window) {
- window.draw(ball);
- }
- void move(sf::Vector2f distance) {
- ball.move(distance);
- }
- sf::FloatRect getGlobalBounds() {
- return ball.getGlobalBounds();
- }
- void setPos(sf::Vector2f newPos) {
- ball.setPosition(newPos);
- }
- private:
- sf::RectangleShape ball;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement