I am trying to stop A clock after 1 of the players reaches a score of 10 so the game stops and puts a button they can press one to play again I know this is a farfetched question because of stopping the clock but can anyone help me.I am confused because there is no stop function on the clock in time.h.
I tried searching this topic up but i got different ways of doing it that would not suit my code.
Here is my code -->> https://hastebin.com/share/uwuvejakih.cpp
#include <SFML/Graphics.hpp>
#include <time.h>
#include <iostream>
#include <sstream>
using namespace sf;
int N = 30, M = 20;
int N1 = 30, M1 = 20;
int size = 16;
int scoreB = 0, scoreR = 0;
int w = size * N;
int h = size * M;
int dir, dir1, num = 4, num1 = 4;
sf::Text bScore;
std::ostringstream BsScore;
sf::Font arial;
std::ostringstream RsScore;
sf::Text rScore;
RenderWindow window(VideoMode(w, h), "Snake Game!");
struct Snake
{
int x, y;
} s[100];
struct Snake2
{
int x1, y1;
} s1[100];
struct Fruit
{
int x, y;
} f;
struct Fruit1
{
int x1, y1;
} f1;
void Tick3();
void Tick4();
void Tick()
{
for (int i = num; i > 0; --i)
{
s[i].x = s[i - 1].x; s[i].y = s[i - 1].y;
}
if (dir == 0) s[0].y += 1;
if (dir == 1) s[0].x -= 1;
if (dir == 2) s[0].x += 1;
if (dir == 3) s[0].y -= 1;
if ((s[0].x == f.x) && (s[0].y == f.y))
{
Tick4();
}
if (s[0].x > N) s[0].x = 0; if (s[0].x < 0) s[0].x = N;
if (s[0].y > M) s[0].y = 0; if (s[0].y < 0) s[0].y = M;
for (int i = 1; i < num; i++)
if (s[0].x == s[i].x && s[0].y == s[i].y)
{
num = i;
scoreR = num;
RsScore.str("");
rScore.setString(RsScore.str());
window.draw(rScore);
RsScore << scoreR;
rScore.setString(RsScore.str());
}
}
void Tick2()
{
for (int i = num1; i > 0; --i)
{
s1[i].x1 = s1[i - 1].x1; s1[i].y1 = s1[i - 1].y1;
}
if (dir1 == 0) s1[0].y1 += 1;
if (dir1 == 1) s1[0].x1 -= 1;
if (dir1 == 2) s1[0].x1 += 1;
if (dir1 == 3) s1[0].y1 -= 1;
if ((s1[0].x1 == f1.x1) && (s1[0].y1 == f1.y1))
{
Tick3();
}
if (s1[0].x1 > N1) s1[0].x1 = 0; if (s1[0].x1 < 0) s1[0].x1 = N1;
if (s1[0].y1 > M1) s1[0].y1 = 0; if (s1[0].y1 < 0) s1[0].y1 = M1;
for (int i = 1; i < num1; i++)
if (s1[0].x1 == s1[i].x1 && s1[0].y1 == s1[i].y1)
{
num1 = i;
scoreB = num1;
BsScore.str("");
bScore.setString(BsScore.str());
window.draw(bScore);
BsScore << scoreB;
bScore.setString(BsScore.str());
}
}
void Tick3()
{
num1++; f1.x1 = rand() % N1; f1.y1 = rand() % M1;
scoreB++;
BsScore.str("");
bScore.setString(BsScore.str());
window.draw(bScore);
BsScore << scoreB;
bScore.setString(BsScore.str());
}
void Tick4()
{
num++; f.x = rand() % N; f.y = rand() % M;
scoreR++;
RsScore.str("");
rScore.setString(RsScore.str());
window.draw(rScore);
RsScore << scoreR;
rScore.setString(RsScore.str());
}
int main()
{
arial.loadFromFile("ARIAL.TTF");
BsScore << scoreB;
bScore.setFont(arial);
bScore.setFillColor(sf::Color::Blue);
bScore.setCharacterSize(30);
bScore.setPosition({ 10, 10 });
bScore.setFont(arial);
bScore.setString(BsScore.str());
RsScore << scoreR;
rScore.setFont(arial);
rScore.setFillColor(sf::Color::Red);
rScore.setCharacterSize(30);
rScore.setPosition({ 450, 10 });
rScore.setFont(arial);
rScore.setString(RsScore.str());
srand(time(0));
Texture t1, t2, t3, t4;
t1.loadFromFile("images/white.png");
t2.loadFromFile("images/red.png");
t3.loadFromFile("images/green.png");
t4.loadFromFile("images/blue.png");
Sprite sprite1(t1);
Sprite sprite2(t2);
Sprite sprite3(t3);
Sprite sprite4(t4);
Clock clock;
Clock clock1;
Clock clock2;
Clock clock3;
float timer = 0, delay = 0.1;
float timer1 = 0;
float timer2 = 0;
float timer3 = 0;
f.x = 10;
f.y = 10;
f1.x1 = 15;
f1.y1 = 15;
while (window.isOpen())
{
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
float time1 = clock1.getElapsedTime().asSeconds();
clock1.restart();
timer1 += time1;
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
}
if (Keyboard::isKeyPressed(Keyboard::Left)) dir = 1;
if (Keyboard::isKeyPressed(Keyboard::Right)) dir = 2;
if (Keyboard::isKeyPressed(Keyboard::Up)) dir = 3;
if (Keyboard::isKeyPressed(Keyboard::Down)) dir = 0;
if (Keyboard::isKeyPressed(Keyboard::A)) dir1 = 1;
if (Keyboard::isKeyPressed(Keyboard::D)) dir1 = 2;
if (Keyboard::isKeyPressed(Keyboard::W)) dir1 = 3;
if (Keyboard::isKeyPressed(Keyboard::S)) dir1 = 0;
if (timer > delay) { timer = 0; Tick(); }
if (timer1 > delay) { timer1 = 0; Tick2(); }
////// draw ///////
window.clear();
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
sprite1.setPosition(i * size, j * size); window.draw(sprite1);
}
for (int i = 0; i < num; i++)
{
sprite2.setPosition(s[i].x * size, s[i].y * size); window.draw(sprite2);
}
for (int i = 0; i < num1; i++)
{
sprite4.setPosition(s1[i].x1 * size, s1[i].y1 * size); window.draw(sprite4);;
}
sprite2.setPosition(f.x * size, f.y * size); window.draw(sprite2);
sprite4.setPosition(f1.x1 * size, f1.y1 * size); window.draw(sprite4);
if (scoreB == 10)
{
std::ostringstream Winner;
Winner << "Blue Wins!";
sf::Text Winb;
Winb.setFont(arial);
Winb.setFillColor(sf::Color::Blue);
Winb.setCharacterSize(40);
Winb.setPosition({ 150, 250 });
Winb.setFont(arial);
Winb.setString(Winner.str());
window.draw(Winb);
}
if (scoreR == 10)
{
std::ostringstream Winner;
Winner << "Red Wins!";
sf::Text Winr;
Winr.setFont(arial);
Winr.setFillColor(sf::Color::Red);
Winr.setCharacterSize(40);
Winr.setPosition({ 150, 250 });
Winr.setFont(arial);
Winr.setString(Winner.str());
window.draw(Winr);
}
window.draw(bScore);
window.draw(rScore);
window.display();
}
return 0;
}
You could create a global flag (game_running
might be a good name) and then use it to ignore the passage of time.
e.g.
if (game_running) { // add this line
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
float time1 = clock1.getElapsedTime().asSeconds();
clock1.restart();
timer1 += time1;
}
The 6 lines in the middle were already part of your main
function, this just adds a way to skip over them.
There's also no good reason to have two separate sf::Clock
objects there, you can use one to update both intervals, like
if (game_running) {
float time = clock.restart().asSeconds();
timer += time;
timer1 += time;
}
After making that change, "stopping the passage of time" is just game_running = false;
and starting it again for a new game is timer = timer1 = 0; clock.restart(); game_running = true;