Jump to content
  • Advertisement
Sign in to follow this  
xuele91

How to stop the timer when the game is completed?

This topic is 3078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello!
I'm doing a project on jigsaw puzzle game, i've a timer that is counting up. After the puzzle pieces is at the correct position, the timer won't stop. how can i make the timer stop right after my 12 puzzle pieces is at the correct place?

And how to display the time used to finish the puzzle?

this is my timer class:


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace drag_and_lock_grid
{
class Timer
{
float timeElapsed;
float startingTimeInSeconds;
int currentMinutes;
int currentSeconds;
int currentTime;
float currentFloatTime;
string properSeconds;

public Timer(float startingTime)
{
startingTimeInSeconds = startingTime;
}

public void Update(GameTime gameTime)
{
timeElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;
currentFloatTime = startingTimeInSeconds - timeElapsed;

currentTime = Convert.ToInt32(currentFloatTime);
currentMinutes = currentTime / 60;
currentSeconds = currentTime % 60;

properSeconds = currentSeconds.ToString("00");

}

public void Draw(SpriteFont font)
{
// Draw my level timer stuff:
Vector2 timerPosition = new Vector2(10, 10);

string timerLabel;
if (currentSeconds >= 10)
{ timerLabel = currentMinutes + ":" + currentSeconds; }
else if (currentSeconds >= 0)
{ timerLabel = currentMinutes + ":" + properSeconds; }
else
{ timerLabel = "0:00"; }

Game1.spriteBatch.DrawString(font,
timerLabel, timerPosition, Color.White);
}

public int getTime()
{
return currentTime;
}
}
}

_______________________________________________________________________________

And this is my gameplay class:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Windows7.Multitouch;

namespace drag_and_lock_grid
{
class ScreenGameplay1 : Screen
{
float timeElapsed;
float startingTimeInSeconds;
int currentMinutes;
int currentSeconds;
int currentTime;
float currentFloatTime;
string properSeconds;

SpriteFont font;
private float time;
SoundEffect soundEngine;
SoundEffectInstance soundInstance;

SoundEffect soundGameOver;
Boolean isPaused;

Vector2 basePosition;
Texture2D baseTexture;


Texture2D GamePausedTexture;
Vector2 GamePausedPosition;

Texture2D fadeTexture;
Texture2D pausedBaseTexture;
Vector2 pausedBasePosition;
Button buttonOptions;
Button buttonResumeGame;
Button buttonRestartGame;
Button buttonMainMenu;
Button buttonSoundOn;
Button buttonSoundOff;
Button buttonCurrentSound;

public static int noc = 0;
PuzzlePiece[] pieces;
public static Random random = new Random();

//AI
Boolean hasGenericAI = false;
Boolean hasRandomAI = true;
int interval = 10;

Timer t;

public ScreenGameplay1()
{
}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
public override void Initialize()
{
time = 0;

Timer t = new Timer(0);

//startingTimeInSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;

Game1.menuSound.Pause();

basePosition = new Vector2(0, 0);

isPaused = false;

pieces = new PuzzlePiece[12];

int imageNo = 1;
int puzzleWidth = 3;
int puzzleHeight = 4;

Vector2 startPosition = new Vector2(569, 32);

Random random = new Random();
int tempNumberX = 0;
int tempNumberY = 0;

Texture2D tempTexture;

for (int i = 0; i < puzzleHeight; i++)
{
for (int j = 0; j < puzzleWidth; j++)
{

tempTexture = Game1.content.Load<Texture2D>("images/gameplay/game1/" + imageNo);
tempNumberX = random.Next(5, 560 - tempTexture.Width);
tempNumberY = random.Next(3, 780 - tempTexture.Height);
pieces[imageNo - 1] = new PuzzlePiece(0, new Vector2(tempNumberX, tempNumberY), tempTexture, imageNo, new Vector2(startPosition.X + (tempTexture.Width * j), startPosition.Y + (tempTexture.Height * i)));
imageNo++;
}
}

soundEngine = Game1.content.Load<SoundEffect>("audio/game_play");
soundInstance = soundEngine.CreateInstance();
soundGameOver = Game1.content.Load<SoundEffect>("audio/Game_over");


baseTexture = Game1.content.Load<Texture2D>("images/gameplay/background");

fadeTexture = Game1.content.Load<Texture2D>("images/gameplay/fade");
pausedBaseTexture = Game1.content.Load<Texture2D>("images/base/darkened");
pausedBasePosition = new Vector2((Game1.graphicsViewport.Width - pausedBaseTexture.Width) / 2,
(Game1.graphicsViewport.Height - pausedBaseTexture.Height) / 2);
GamePausedTexture = Game1.content.Load<Texture2D>("images/gameplay/game options");
GamePausedPosition = new Vector2(25, 30);

buttonOptions = new Button(new Vector2(50, 630),
Game1.content.Load<Texture2D>("images/button/game_play/Options_button"));

buttonResumeGame = new Button(new Vector2(500, 160),
Game1.content.Load<Texture2D>("images/button/game_play/Resume_Game_button"));

buttonRestartGame = new Button(new Vector2(500, 310),
Game1.content.Load<Texture2D>("images/button/game_play/retry_button"));

buttonMainMenu = new Button(new Vector2(500, 460),
Game1.content.Load<Texture2D>("images/button/game_play/main_menu_button"));

buttonSoundOn = new Button(new Vector2(500, 610),
Game1.content.Load<Texture2D>("images/button/game_play/on"));

buttonSoundOff = new Button(new Vector2(500, 610),
Game1.content.Load<Texture2D>("images/button/game_play/off"));


font = Game1.content.Load<SpriteFont>("Kootenay");
soundInstance.Volume = 1.0f;
soundInstance.IsLooped = true;

if (Game1.sound == true)
{
buttonCurrentSound = buttonSoundOn;
soundInstance.Play();
}
else
{
buttonCurrentSound = buttonSoundOff;
}

}

public override void touchDown(float x, float y)
{
if (!isPaused)
{
foreach (PuzzlePiece puzzle in pieces)
{
puzzle.touchDown(x, y);
}


if (buttonOptions.isWithin(x, y))
{
buttonOptions.touchDown();
buttonOptions.touchUp();
isPaused = true;
}
}
else
{
if (buttonResumeGame.isWithin(x, y))
{
buttonResumeGame.touchDown();
buttonResumeGame.touchUp();
isPaused = false;
}

if (buttonRestartGame.isWithin(x, y))
{
buttonRestartGame.touchDown();
soundInstance.Dispose();
ScreenManager.screenManager.addScreen(new ScreenGameplay1(), 0);
}

if (buttonMainMenu.isWithin(x, y))
{
buttonMainMenu.touchDown();
Game1.menuSound.Play();
soundInstance.Dispose();
ScreenManager.screenManager.addScreen(new ScreenMainMenu(), 0);
}

if (buttonCurrentSound.isWithin(x, y))
{
toggleSound();
}
}
}

public override void touchUp(float x, float y)
{
if (!isPaused)
{
foreach (PuzzlePiece puzzle in pieces)
{
puzzle.touchUp(x, y);
}

}
}
public override void touchMove(float x, float y)
{
if (!isPaused)
{
foreach (PuzzlePiece puzzle in pieces)
{
puzzle.touchMove(x, y);

}
}
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{

// TODO: Add your update logic here
foreach (PuzzlePiece puzzle in pieces)
{
puzzle.Update();
}

//t.Update(gameTime);

if (!isPaused)
{

{
timeElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;
}
}



base.Update(gameTime);
}


/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Draw(GameTime gameTime)
{
// TODO: Add your drawing code here
Game1.graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.Black);
Game1.spriteBatch.Begin();
Game1.spriteBatch.Draw(baseTexture, basePosition, Microsoft.Xna.Framework.Graphics.Color.White);

buttonOptions.Draw();

foreach (PuzzlePiece puzzle in pieces)
{
puzzle.Draw();
}


if (isPaused)
{
Game1.spriteBatch.Draw(fadeTexture, basePosition, Microsoft.Xna.Framework.Graphics.Color.White);
Game1.spriteBatch.Draw(GamePausedTexture, GamePausedPosition, Microsoft.Xna.Framework.Graphics.Color.White);
//Game1.spriteBatch.Draw(pausedBaseTexture, pausedBasePosition, Microsoft.Xna.Framework.Graphics.Color.White);

buttonResumeGame.Draw();
buttonRestartGame.Draw();
buttonMainMenu.Draw();
buttonCurrentSound.Draw();
}
//Game1.spriteBatch.DrawString(font, "test", new Vector2(10, 10), Color.White);
//Convert.ToString(t.getTime()), new Vector2(10, 10), Color.White);
//t.Draw(font);

Vector2 timerPosition = new Vector2(10, 10);

string timerLabel;
if (currentSeconds > 9)
{ timerLabel = currentMinutes + ":" + currentSeconds; }
else if (currentSeconds > -1)
{ timerLabel = currentMinutes + ":" + properSeconds; }
else
{ timerLabel = "0:00"; }

Game1.spriteBatch.DrawString(font,
Convert.ToString(timeElapsed), timerPosition, Color.Black);

Game1.spriteBatch.End();

base.Draw(gameTime);
}

public void toggleSound()
{
if (soundInstance.State == SoundState.Playing)
{
soundInstance.Pause();
Game1.sound = false;
buttonCurrentSound = buttonSoundOff;
}
else
{
soundInstance.Play();
Game1.sound = true;
buttonCurrentSound = buttonSoundOn;
}
}
}
}



Share this post


Link to post
Share on other sites
Advertisement
do a while loop with a boolean, that's on until the pieces are correct, then turn it off. it should stop the counter from increasing after that point, leaving what it was before the loop stopped.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!