-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
187 lines (130 loc) · 3.21 KB
/
Copy pathmain.cpp
File metadata and controls
187 lines (130 loc) · 3.21 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//#include<iostream>
#include<SFML/Graphics.hpp>
#include<random>
#include<iostream>
int main()
{
sf::RenderWindow win(sf::VideoMode(200,200),"Game");
win.setFramerateLimit(60);
////back
sf::Texture Background;
Background.loadFromFile("backg.jpg");
sf::RectangleShape backRend(sf::Vector2f(200,200));
backRend.setTexture(&Background);
///player
sf::CircleShape player(5);
player.setFillColor(sf::Color::Green);
player.setOutlineThickness(2);
player.setOutlineColor(sf::Color::Blue);
/////make blocks
///random of blocks
std::random_device dev;
std::mt19937 ran(dev());
std::uniform_int_distribution<std::mt19937::result_type> rang(1,200);
/////create text
sf::Font font;
font.loadFromFile("FreeMonoBoldOblique.ttf");
sf::Text pWin;
sf::Text pLoss;
pWin.setFont(font);
pWin.setString("you win :)");
pWin.setPosition(sf::Vector2f(25,25));
pWin.setFillColor(sf::Color::White);
pWin.setFillColor(sf::Color::Green);
pWin.setCharacterSize(25);
pLoss.setFont(font);
pLoss.setString("you loss :(");
pLoss.setPosition(sf::Vector2f(25,25));
pLoss.setFillColor(sf::Color::White);
pLoss.setFillColor(sf::Color::Red);
pLoss.setCharacterSize(25);
sf::RectangleShape bOf(sf::Vector2f(200,50));
bOf.setPosition(0,20);
////
bool ifLoss=false;
///make time for end game
sf::Time time;
sf::Clock clock;
int numOfb=20;
sf::ConvexShape c[numOfb];
for(int i=0;i<numOfb;i++)
{
c[i].setPointCount(4);
c[i].setFillColor(sf::Color::Red);
c[i].setPoint(0,sf::Vector2f(20,20));
c[i].setPoint(1,sf::Vector2f(30,0));
c[i].setPoint(2,sf::Vector2f(10,10));
c[i].setPoint(3,sf::Vector2f(20,0));
c[i].setPosition(rang(ran)+200,rang(ran));
}
///loop of window
while(win.isOpen())
{
sf::Event event;///event of window
while(win.pollEvent(event))
{
if(event.type == sf::Event::Closed)
win.close();
///move player by keybord
if(event.type == sf::Event::KeyPressed)
{
if(event.key.code == sf::Keyboard::Up)
player.move(0,-5);
if(event.key.code == sf::Keyboard::Down)
player.move(0,5);
if(event.key.code == sf::Keyboard::Left)
player.move(-5,0);
if(event.key.code == sf::Keyboard::Right)
player.move(5,0);
}
//move by mouse
else if(event.type ==sf::Event::MouseMoved)
{
player.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(win)));
}
///if cross over blocks
for(int i=0;i<numOfb;i++)
{
if(player.getGlobalBounds().intersects(c[i].getGlobalBounds()))
{
ifLoss=true;
player.setOutlineColor(sf::Color::Red);
}
}
}
///move blocks
for(int i=0;i<numOfb;i++)
{
if(c[i].getPosition().x>0)
c[i].move(-1,0);
}
time=clock.getElapsedTime();
std::cout<<time.asSeconds()<<std::endl;
///clear
win.clear();
///draw objects
win.draw(backRend);
win.draw(player);
if( !(time.asSeconds()>10))//wite for 30 sec for win
{
if(!ifLoss){
for(int i=0;i<numOfb;i++)
{
if(c[i].getPosition().x>0)
win.draw(c[i]);
else
c[i].setPosition(rang(ran)+200,rang(ran));
}
}else{
win.draw(bOf);
win.draw(pLoss);
}
}else if(!ifLoss){
win.draw(bOf);
win.draw(pWin);
}
//display objects
win.display();
}
return 0;
}