-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaddle.cpp
46 lines (38 loc) · 1.11 KB
/
Paddle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "Paddle.h"
#include "Global.h"
#include <SFML/Graphics.hpp>
Paddle::Paddle(float mX, float mY)
{
if (!texture.loadFromFile("./images/paddleBlue.png"))
{
// error...
}
texture.setSmooth(true);
shape.setTexture(&texture);
shape.setPosition(mX, mY);
shape.setSize({ paddleWidth, paddleHeight });
//shape.setFillColor(sf::Color::Red);
shape.setOrigin(paddleWidth / 2.f, paddleHeight / 2.f);
}
void Paddle::update()
{
shape.move(velocity);
// To move the paddle, we check if the user is pressing
// the left or right arrow key: if so, we change the velocity.
// To keep the paddle "inside the window", we change the velocity
// only if its position is inside the window.
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && left() > 0)
velocity.x = -paddleVelocity;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) &&
right() < windowWidth)
velocity.x = paddleVelocity;
// If the user isn't pressing anything, stop moving.
else
velocity.x = 0;
}
void Paddle::reset()
{
shape.setPosition(windowWidth / 2, windowHeight - 50);
shape.setSize({ paddleWidth, paddleHeight });
velocity.x = 0;
}