-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (104 loc) · 3.52 KB
/
main.cpp
File metadata and controls
123 lines (104 loc) · 3.52 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
//
// Выпускной проект.
// Игра "Тетрис"
//
#include <iostream>
#include "Utils.hpp"
#include "Field.hpp"
using namespace std;
int main() {
unsigned int w = 425;
unsigned int h = 600;
srand((unsigned int)time(nullptr));
try {
Field field(w, h);
Press press;
sf::RenderWindow window(sf::VideoMode(w, h), "Tetris", sf::Style::Close);
sf::Clock tic_clock;
sf::Clock tap_clock;
float tic_time;
float tap_time;
while (window.isOpen()) {
sf::Event event;
tic_time = tic_clock.getElapsedTime().asSeconds();
tap_time = tap_clock.getElapsedTime().asSeconds();
field.check_field(window);
if (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
field.game_over(window);
if (event.type == sf::Event::KeyReleased) {
if (press.space) {
if (event.key.code == sf::Keyboard::Space) {
field.tic(true);
field.draw_field(window);
press.space = false;
}
}
if (press.up) {
if (event.key.code == sf::Keyboard::Up) {
field.rotate();
press.up = false;
}
}
if (press.M) {
if (event.key.code == sf::Keyboard::M) {
field.mute();
press.M = false;
}
}
if (press.P) {
if (event.key.code == sf::Keyboard::P) {
field.pause(window);
press.P = false;
}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::M))
press.M = true;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::P))
press.P = true;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
press.up = true;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
press.down = true;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
press.space = true;
if (tap_time > TAP_CLOCK) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
field.update_figure(-1);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
field.update_figure(+1);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
field.tic();
tap_clock.restart();
}
if (tic_time > TIC_CLOCK) {
field.tic();
tic_clock.restart();
}
field.draw_field(window);
window.display();
window.clear();
}
}
catch(std::bad_alloc err) {
std::cout << "Bad allocate" << std::endl;
return EXIT_FAILURE;
}
catch(Expection expection) {
expection.show_error();
return EXIT_FAILURE;
}
catch(std::exception exp ) {
std::ofstream new_file("../records/records.txt");
new_file << 0;
new_file.close();
}
catch(...) {
std::cout << "Unexpected error" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}