-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtari.cpp
More file actions
149 lines (135 loc) · 4.39 KB
/
Copy pathAtari.cpp
File metadata and controls
149 lines (135 loc) · 4.39 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/System.hpp>
int main() {
using namespace std;
sf::RenderWindow window(sf::VideoMode({400, 300}), "Atari's pong game: Modified");
window.setFramerateLimit(60);
float UNIy, UNy = 131.25f;
float Ballx = 192.5f;
float Bally = 146.5f;
float xchange = 0.3f;
float ychange = 1.5f;
float sec;
float DetectedY;
sf::Font font;
if (!font.openFromFile("C:/test_sfml/Roboto-VariableFont_wdth,wght.ttf")) {
cerr << "Something went wrong." << endl;
return -1;
}
sf::Text result1(font);
result1.setString("You won!");
result1.setCharacterSize(28);
result1.setPosition({140.f, 110.f});
sf::Text result2(font);
result2.setString("You lost!");
result2.setCharacterSize(28);
result2.setPosition({140.f, 110.f});
sf::RectangleShape paddle1({5.f, 37.5f});
paddle1.setFillColor(sf::Color::Blue);
paddle1.setPosition({2.f, UNIy});
sf::RectangleShape paddle2({5.f, 37.5f});
paddle2.setFillColor(sf::Color::Red);
paddle2.setPosition({393.f, UNy});
sf::RectangleShape ball({7.5f, 7.5f});
ball.setFillColor(sf::Color::Yellow);
ball.setPosition({Ballx, Bally});
while (window.isOpen()) {
while (const optional event = window.pollEvent()) {
if (event->is<sf::Event::Closed>())
window.close();
}
//we move the paddle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
if (UNIy > 0.f) UNIy -= 5.f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) {
if (UNIy < 262.f) UNIy += 5.f;
}
paddle1.setPosition({2.f, UNIy});
//ball moves
Ballx -= xchange;
Bally -= ychange;
if ((Bally <= 0.f) || (Bally >= 300.f)) {
ychange = ychange * -1.f;
ball.setPosition({Ballx, Bally});
}
ball.setPosition({Ballx, Bally});
//collisions
if (Ballx <= 7.f) {
if ((Bally >= UNIy) && (Bally <= (UNIy + 37.5))) {
if (Bally < (UNIy + 18.75)) {
xchange = xchange * -1;
if (xchange < 0.f) {xchange -= 0.5f;}
else {xchange += 0.5f;}
ychange = ychange * -1;
}
else if (Bally == (UNIy + 18.75)) {
xchange = xchange * -1;
}
else if (Bally > (UNIy + 18.75)) {
xchange = xchange * -1;
if (xchange < 0.f) {xchange -= 0.15f;}
else {xchange += 0.15f;}
ychange = ychange * -1;
}
}
}
if ((Ballx + 7.5f) >= 392.f) {
if ((Bally >= UNy) && (Bally <= (UNy + 37.5))) {
if (Bally < (UNy + 18.75)) {
xchange = xchange * -1;
if (xchange < 0.f) {xchange -= 0.5f;}
else {xchange += 0.5f;}
ychange = ychange * -1;
}
else if (Bally == (UNy + 18.75)) {
xchange = xchange * -1;
}
else if (Bally > (UNy + 18.75)) {
xchange = xchange * -1;
if (xchange < 0.f) {xchange -= 0.15f;}
else {xchange += 0.15f;}
ychange = ychange * -1;
}
}
}
//winners!
if (Ballx < 0.f) {
window.draw(result2);
window.display();
sf::sleep(sf::seconds(3));
break;}
else if ((Ballx + 7.5f) > 401.f) {
window.draw(result1);
window.display();
sf::sleep(sf::seconds(3));
break;}
//The dumbmaker and the computer
DetectedY = Bally;
if (DetectedY > UNy) {
if (UNy <= 262.f) {UNy += 5.f;}
paddle2.setPosition({393.f, UNy});}
else if (DetectedY < UNy) {
if (UNy >= 6.f) {UNy -= 5.f;}
paddle2.setPosition({393.f, UNy});}
sf::Clock Timer;
sf::Time count = Timer.getElapsedTime();
sec = count.asSeconds();
if (sec == 6.f) {
while (sec < 9.f) {
if (UNy >= 6.f) {UNy -= 5.f;}
paddle2.setPosition({393.f, UNy});
}
}
if (sec >= 9.f) {
count = Timer.restart();
}
window.clear(sf::Color::Black);
window.draw(paddle1);
window.draw(paddle2);
window.draw(ball);
window.display();
}
return 0;
}