-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaddle.cpp
125 lines (101 loc) · 2.35 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "Paddle.h"
#include "Constants.h"
#include <iostream>
#include <sstream>
using namespace std;
Paddle::Paddle(RenderWindow * window, int height, int width, int player, float speed)
{
this->score = 0;
this->y = 300;
this->width = width;
this->height = height;
this->player = player;
this->speed = speed;
this->window = window;
this->rectangle.setSize(Vector2f(this->width, this->height));
this->rectangle.setOrigin(this->width / 2, this->height / 2);
if (this->player == 1)
{
this->x = 25;
this->rectangle.setPosition(this->x, this->y);
}
else
{
this->x = 775;
this->rectangle.setPosition(this->x, this->y);
}
}
void Paddle::draw()
{
this->window->draw(this->rectangle);
}
void Paddle::update(float time)
{
if (this->player == 1)
{
if (Keyboard::isKeyPressed(Keyboard::Up))
{
cout << "Left player go top" << endl;
this->setY(this->y - time * this->speed);
}
else if (Keyboard::isKeyPressed(Keyboard::Down))
{
cout << "Right player go bottom" << endl;
this->setY(this->y + time * this->speed);
}
}
else
{
if (Keyboard::isKeyPressed(Keyboard::W))
{
cout << "Right player go top" << endl;
cout << this->y - time * this->speed << endl;
this->setY(this->y - time * this->speed);
}
else if (Keyboard::isKeyPressed(Keyboard::S))
{
cout << "Right player go bottom" << endl;
this->setY(this->y + time * this->speed);
}
}
}
void Paddle::setY(int y)
{
if ((y >= 0 + this->height / 2) && (y <= WINDOW_HEIGHT - this->height / 2))
{
this->y = y;
cout << "Y value of player " << this->player << " was changed to " << this->y << endl;
this->rectangle.setPosition(rectangle.getPosition().x, this->y);
}
}
void Paddle::updateScore()
{
stringstream ss;
this->score += 1;
cout << "Score of player " << this->player << " was changed to " << this->score << endl;
ss << this->score;
this->scoreText.setString(ss.str());
}
void Paddle::setScoreFont(Font* scoreFont)
{
this->scoreFont = scoreFont;
this->scoreText.setFont(*this->scoreFont);
this->scoreText.setString("0");
}
void Paddle::setScorePosition(int x, int y)
{
this->scoreText.setPosition(x, y);
this->scoreText.setOrigin(this->scoreText.getScale().x / 2, this->scoreText.getScale().y / 2);
}
void Paddle::drawScore()
{
this->window->draw(this->scoreText);
}
int Paddle::getX()
{
return this->x;
}
int Paddle::getY()
{
return this->y;
}