Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <SFML\Graphics.hpp>
- class Ball{
- public:
- Ball(){
- }
- Ball(sf::Vector2f size){
- rect.setSize(size);
- }
- void getColl(){
- bottom = rect.getPosition().y + rect.getSize().y;
- left = rect.getPosition().x;
- right = rect.getPosition().x + rect.getSize().x;
- top = rect.getPosition().y;
- }
- void setPos(sf::Vector2f newPos){
- rect.setPosition(newPos);
- }
- void draw(sf::RenderWindow &window){
- window.draw(rect);
- }
- void moveRight(float moveSpeed, float rndHeight){
- rect.move(moveSpeed, -rndHeight);
- }
- void moveLeft(float moveSpeed, float rndHeight){
- rect.move(-moveSpeed, rndHeight);
- }
- float getRight(){
- return right;
- }
- float getLeft(){
- return left;
- }
- float getTop(){
- return top;
- }
- float getBottom(){
- return bottom;
- }
- protected:
- sf::RectangleShape rect;
- float bottom;
- float top;
- float right;
- float left;
- };
- ///////////////////////////////////////////////////////////////
- #pragma once
- #include <SFML\Graphics.hpp>
- #include <iostream>
- #include "Ball.h"
- class Paddle : public Ball{
- public:
- Paddle(){
- //Default Constructor
- }
- Paddle(sf::Vector2f padSize){
- rect.setSize(padSize);
- }
- void drawPaddle(sf::RenderWindow &window){
- window.draw(rect);
- }
- void setPos(sf::Vector2f newPos){
- rect.setPosition(newPos);
- }
- void movePaddle(char direction, double moveSpeed){
- if(direction == 'u'){
- rect.move(0, -moveSpeed);
- }else if(direction == 'd'){
- rect.move(0, moveSpeed);
- }else if(direction == 'r'){
- rect.move(moveSpeed, 0);
- }else if(direction == 'l'){
- rect.move(-moveSpeed, 0);
- }
- }
- void getColl(){
- bottom = rect.getPosition().y + rect.getSize().y;
- left = rect.getPosition().x;
- right = rect.getPosition().x + rect.getSize().x;
- top = rect.getPosition().y;
- }
- bool checkColl(Ball ball){
- if(this->right < ball.getLeft() || this->left > ball.getRight()
- || this->top > ball.getBottom() || this->bottom < ball.getTop()){
- return false;
- }
- return true;
- }
- protected:
- sf::RectangleShape rect;
- float bottom;
- float top;
- float right;
- float left;
- };
- ///////////////////////////////////////////////////////////////
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include "Paddle.h"
- #include "Ball.h"
- #include <ctime>
- #include <cstdlib>
- #include <sstream>
- int main(){
- sf::RenderWindow window;
- sf::Vector2i centerWindow((sf::VideoMode::getDesktopMode().width/2)-755, (sf::VideoMode::getDesktopMode().height/2)-390);
- window.create(sf::VideoMode(1500, 700), "Pong by Ben Bollinger", sf::Style::Titlebar | sf::Style::Close);
- window.setPosition(centerWindow);
- window.setKeyRepeatEnabled(true);
- //Create Game Objects:
- Paddle pd1(sf::Vector2f(8, 120));
- Paddle pd2(sf::Vector2f(8, 120));
- Ball ball1(sf::Vector2f(20, 20));
- Paddle boundBottom(sf::Vector2f(1500, 10));
- Paddle boundTop(sf::Vector2f(1500, 10));
- Paddle boundLeft(sf::Vector2f(10, 700));
- Paddle boundRight(sf::Vector2f(10, 700));
- //Score Settings:
- int pScore1 = 0;
- int pScore2 = 0;
- sf::Font font;
- font.loadFromFile("arial.ttf");
- sf::Text score1;
- sf::Text score2;
- score1.setFont(font);
- score2.setFont(font);
- score1.setCharacterSize(21);
- score2.setCharacterSize(21);
- std::ostringstream sScore1;
- std::ostringstream sScore2;
- sScore1 << "Player1's Score: " << pScore1;
- sScore2 << "Player2's Score: " << pScore2;
- score1.setString(sScore1.str());
- score2.setString(sScore2.str());
- score1.setColor(sf::Color::White);
- //Relocate Game Objects:
- pd1.setPos(sf::Vector2f(20, 100));
- ball1.setPos(sf::Vector2f(700, 350));
- pd2.setPos(sf::Vector2f(1472, 100));
- boundBottom.setPos(sf::Vector2f(0, 690));
- boundTop.setPos(sf::Vector2f(0, 0));
- boundLeft.setPos(sf::Vector2f(0, 0));
- boundRight.setPos(sf::Vector2f(1490, 0));
- score1.setPosition(sf::Vector2f(20, 7));
- score2.setPosition(sf::Vector2f(1255, 7));
- float ballSpeed = 0.14;
- int paddleSpeed = 16;
- //Main Loop:
- while(window.isOpen()){
- sf::Event Event;
- //Event Loop:
- while(window.pollEvent(Event)){
- switch(Event.type){
- case sf::Event::Closed:
- window.close();
- }
- //Player2 Movement
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
- pd2.movePaddle('u', paddleSpeed);
- pd2.drawPaddle(window);
- }else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
- pd2.movePaddle('d', paddleSpeed);
- pd2.drawPaddle(window);
- }
- //Player1 Movement:
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
- pd1.movePaddle('u', paddleSpeed);
- pd1.drawPaddle(window);
- }else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
- pd1.movePaddle('d', paddleSpeed);
- pd1.drawPaddle(window);
- }
- }
- window.clear(); //Update window
- pd1.getColl(); //Get collision info
- pd2.getColl(); //Get collision info
- ball1.getColl(); //Get collision info
- static bool goLeft = true;
- srand(static_cast<unsigned int>(time(0))); //Seed random number with current time.
- int rndNum1 = rand() % 3 + 1;
- float upSpeed;
- //Random number handler:
- if(rndNum1 == 1){
- upSpeed = 0.01;
- }else if(rndNum1 == 2){
- upSpeed = 0.02;
- }else{
- upSpeed = -0.01;
- }
- /* If the ball is not colliding with paddle1 and it's going
- left, then move it left. But if it is colliding with paddle1 stop
- going left*/
- if(!pd1.checkColl(ball1) && goLeft == true){
- ball1.moveLeft(ballSpeed, upSpeed);
- }else if(pd1.checkColl(ball1)){
- goLeft = false;
- }
- srand(static_cast<unsigned int>(time(0))); //Seed random number with current time.
- int rndNum2 = rand() % 3 + 1;
- float upSpeed2;
- //Random number handler:
- if(rndNum2 == 1){
- upSpeed2 = 0.01;
- }else if(rndNum2 == 2){
- upSpeed2 = 0.02;
- }else{
- upSpeed2 = -0.01;
- }
- /* If the ball is not colliding with paddle2 and it's not going
- left, then move it right. But if it is colliding with paddle2 stop
- going right*/
- if(!pd2.checkColl(ball1) && goLeft == false){
- ball1.moveRight(ballSpeed, upSpeed2);
- }else if(pd2.checkColl(ball1)){
- goLeft = true;
- }
- //Wall Collisions:
- boundLeft.getColl();
- if(boundLeft.checkColl(ball1)){
- //Reset ball location:
- ball1.setPos(sf::Vector2f(700, 350));
- //Increment score and redraw:
- pScore2++;
- sScore2.str("");
- sScore2 << "Player2's Score: " << pScore2;
- score2.setString(sScore2.str());
- }
- boundRight.getColl();
- if(boundRight.checkColl(ball1)){
- //Reset ball location:
- ball1.setPos(sf::Vector2f(700, 350));
- //Increment score and redraw:
- pScore1++;
- sScore1.str("");
- sScore1 << "Player1's Score: " << pScore1;
- score1.setString(sScore1.str());
- }
- ball1.getColl();
- //Draw Bounds:
- boundBottom.drawPaddle(window);
- boundTop.drawPaddle(window);
- boundLeft.drawPaddle(window);
- boundRight.drawPaddle(window);
- //Draw Scores:
- window.draw(score1);
- window.draw(score2);
- pd2.drawPaddle(window); //Draw Player2
- ball1.draw(window); //Draw Ball
- pd1.drawPaddle(window); //Draw Player1
- window.display(); //Display Window
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement