Witam! Otóż zacząłem się uczyć SFML'a i zrobiłem taki o to program z tej strony: http://gamecodeschool.com/sfml/coding-a-simple-pong-game-with-sfml/ . Wszystko ładnie działało więc postanowiłem ją nieco przerobić tak aby były dwie paletki na 2 graczy. Problemem jest fakt że piłka nie odbija się pod kątem. Jeśli znacie rozwiązanie będe wdzięczny!
Kody źródłowe:
Main.cpp
#include <SFML/Graphics.hpp>
#include "Bat.h"
#include "Ball.h"
#include <sstream>
#include <cstdlib>
#include <SFML/Audio.hpp>
using namespace sf;
int main()
{
int windowWidth=1000;
int windowHeight=600;
RenderWindow window(VideoMode(windowWidth,windowHeight),"Pong");
int score=0;
int lives=3;
Bat bat(windowWidth-20,windowHeight/2);
Bat bacik(20,windowHeight/2);
Ball ball(500,250);
Text hud;
Font font;
font.loadFromFile("DS-DIGIT.TTF");
hud.setFont(font);
hud.setCharacterSize(75);
hud.setFillColor(sf::Color::White);
while(window.isOpen())
{
/*
*/
Event event;
while(window.pollEvent(event))
{
if(event.type==Event::Closed)
window.close();
}
if(Keyboard::isKeyPressed(Keyboard::Up))
{
bat.moveUp();
}
else if(Keyboard::isKeyPressed(Keyboard::Down))
{
bat.moveDown();
}
else if(Keyboard::isKeyPressed(Keyboard::Escape))
{
window.close();
}
if(Keyboard::isKeyPressed(Keyboard::W))
{
bacik.moveUp();
}
else if(Keyboard::isKeyPressed(Keyboard::S))
{
bacik.moveDown();
}
if(ball.getPosition().top>windowHeight||ball.getPosition().top<windowHeight)
{
ball.reboundTopOrBottom();
}
if (ball.getPosition().left < 0 )
{
ball.reboundSideLeft();
}
if(ball.getPosition().left+10>windowWidth)
{
ball.reboundSideRight();
}
if(ball.getPosition().intersects(bat.getPosition()))
{
ball.reboundBat();
}
if(ball.getPosition().intersects(bacik.getPosition()))
{
ball.reboundBat();
}
ball.update();
bat.update();
bacik.update();
std::stringstream ss;
ss<<"Score: "<<score<<" Lives: "<<lives;
hud.setString(ss.str());
window.clear(Color(26,128,182,255));
window.draw(bat.getShape());
window.draw(bacik.getShape());
window.draw(ball.getShape());
window.draw(hud);
window.display();}
getchar();
return 0;
}
Ball.cpp
#include "Ball.h"
Ball::Ball(float startX, float startY)
{
position.x=startX;
position.y=startY;
ballShape.setSize(sf::Vector2f(10,10));
ballShape.setPosition(position);
}
FloatRect Ball::getPosition()
{
return ballShape.getGlobalBounds();
}
RectangleShape Ball::getShape()
{
return ballShape;
}
float Ball::getYVelocity()
{
return yVelocity;
}
void Ball::reboundTopOrBottom()
{
yVelocity=-yVelocity;
}
void Ball::reboundSideLeft()
{
position.y = 250;
position.x = 500;
}
void Ball::reboundSideRight()
{
position.y = 250;
position.x = 500;
}
void Ball::reboundBat()
{
position.x -= (xVelocity * 30);
xVelocity = -xVelocity;
}
void Ball::update()
{
position.y+=yVelocity;
position.x+=xVelocity;
ballShape.setPosition(position);
}
ball.h
#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;
class Ball
{private:
Vector2f position;
RectangleShape ballShape;
float xVelocity= .2f;
float yVelocity= .2f;
public:
Ball (float startX, float startY);
FloatRect getPosition();
RectangleShape getShape();
float getYVelocity();
void reboundSideLeft();
void reboundSideRight();
void reboundTopOrBottom();
void reboundBat();
void update();
};
Bat.h
#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;
class Bat
{
private:
Vector2f position;
//rectangleshape object
RectangleShape batShape;
float batSpeed = .3f;
public:
Bat(float startX, float startY);
FloatRect getPosition();
RectangleShape getShape();
void moveUp();
void moveDown();
void update();
};
Bat.cpp
#include "Bat.h"
Bat::Bat(float startX, float startY)
{
position.x=startX;
position.y=startY;
batShape.setSize(sf::Vector2f(5,100));
batShape.setPosition(position);
}
FloatRect Bat::getPosition()
{
return batShape.getGlobalBounds();
}
RectangleShape Bat::getShape()
{
return batShape;
}
void Bat::moveUp()
{
position.y-=batSpeed;
}
void Bat::moveDown()
{
position.y+=batSpeed;
}
void Bat::update()
{
batShape.setPosition(position);
}