I am working on a game in C#. I need to display the score and the length of time the game was played. I am able to get the score but I cannot get the length of time the game was played. I am using TimeSpan. The error I get is ( the name 'interationDuration' does not exist in the current context). How do I go about getting the length of time the game was displayed?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ShootGreenLightGame
{
public partial class Form1 : Form
{
private Image redLightImage;
private Image greenLightImage;
private Graphics g;
private Boolean IsGameOver = false;
private int width; // Win Mobile Pocket PC screen Width default 230
private int height; // Win Mobile Pocket PC screen Height default 258
private int redLightImageW;
private int redLightImageH;
private int currentRedLightX;
private int currentRedLightY;
private int greenLightImageW;
private int greenLightImageH;
private int currentGreenLightX;
private int currentGreenLightY;
private int score = 0;
TimeSpan iterationDuration;// = new TimeSpan();
private const double SPEED = 1000 / 50; // 50 frames per second
public KeyEventArgs keyState;
public Form1()
{
width = Width;
height = Height;
redLightImage =
new Bitmap(System.Reflection.Assembly.GetExecutingAssembly()
.GetManifestResourceStream("ShootTheGreenLightGame.RedLightW30H20.gif"));
redLightImageW = redLightImage.Width;
redLightImageH = redLightImage.Height;
greenLightImage =
new Bitmap(System.Reflection.Assembly.GetExecutingAssembly()
.GetManifestResourceStream("ShootTheGreenLightGame.GreenLightW30H20.gif"));
greenLightImageW = greenLightImage.Width;
greenLightImageH = greenLightImage.Height;
g = CreateGraphics();
InitializeComponent();
}
public void Stop()
{
IsGameOver = true;
MessageBox.Show(String.Format("Game Over\n\nScore = {0}\n\nTime Played = {0}", score, interationDuration));
//Console.WriteLine("{0}", iterationDuration);
Application.Exit();
}
public void Start()
{
score = 0;
IsGameOver = false;
currentRedLightX = 0;
currentRedLightY = 0;
currentGreenLightX = width / 2;
currentGreenLightY = height / 2;
double minIterationDuration = SPEED; // 50 frames / sec
//game loop
while (!IsGameOver)
{
if (IsCollision())
{
score += 10;
}
DateTime startIterationTime = System.DateTime.Now;
UpdateGameState();
Render();
DateTime endIterationTime = System.DateTime.Now;
iterationDuration = endIterationTime - startIterationTime;
if (iterationDuration.TotalMilliseconds < minIterationDuration)
Thread.Sleep(Convert.ToInt32(minIterationDuration - iterationDuration.TotalMilliseconds));
Application.DoEvents();
}
}
public void UpdateGameState()
{
if (keyState != null)
{
switch (keyState.KeyCode)
{
case Keys.Left:
currentRedLightX = Math.Max(0, currentRedLightX - 5);
break;
case Keys.Right:
currentRedLightX = Math.Min(width - redLightImageW, currentRedLightX + 5);
break;
}
}
if ((currentRedLightY + 5) >= (height - redLightImageH))
{
currentRedLightY = 0;
}
else
{
currentRedLightY += 5;
}
}
public void Render()
{
g.Clear(System.Drawing.Color.White);
g.DrawImage(redLightImage, currentRedLightX, currentRedLightY);
g.DrawImage(greenLightImage, currentGreenLightX, currentGreenLightY);
}
public bool IsCollision()
{
if (currentRedLightX >= (currentGreenLightX - 15) && // 15 = 1/2 greenLightImageW
currentRedLightX <= (currentGreenLightX + 45) && // 45 = 1 1/2 greenLightImageW
currentRedLightY >= (currentGreenLightY - 10) && // 10 = 1/2 greenLightImageH
currentRedLightY <= (currentGreenLightY + 30)) // 30 = 1 1/2 greenLightImageH
return true;
else
return false;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Render();
}
private void menuItem1_Click(object sender, EventArgs e)
{
Start();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Up))
{
// Up
keyState = e;
}
if ((e.KeyCode == System.Windows.Forms.Keys.Down))
{
// Down
keyState = e;
}
if ((e.KeyCode == System.Windows.Forms.Keys.Left))
{
// Left
keyState = e;
}
if ((e.KeyCode == System.Windows.Forms.Keys.Right))
{
// Right
keyState = e;
}
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
// Enter
Stop();
}
}
}
}
How do I display timespan in MessageBox
Started by marioDunn, Apr 26 2010 06:37 AM
2 replies to this topic
#1
Posted 26 April 2010 - 06:37 AM
|
|
|
#2
Posted 26 April 2010 - 08:59 AM
you've got 'interation'. It should be 'iteration'.
Also, wrap some code tags round all that code.
Also, wrap some code tags round all that code.
#3
Posted 04 May 2010 - 09:14 PM
did this problem , ever get solved . Why do these people ever close their threads after starting them
when you have got the answer for the question do Post a message that you indeed got the solution for the problem ^^
when you have got the answer for the question do Post a message that you indeed got the solution for the problem ^^


Sign In
Create Account

Back to top









