ok so I am trying to get this game to have a timer and I got it down to the stopwatch thing but Visual Studio 2005 says that
"Error 1 The type or namespace name 'Stopwatch' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Kakashi\Documents\Visual Studio 2005\Projects\ShootTheGreenLightGame\Form1.cs 40 9 ShootTheGreenLightGame
"
Um what am i missing here? I have this in just like the book says it to be, here is the code for the program; The stop watch is declared at the top and then its started in the main game loop ie public void Start()
and the stoped is called at public void Stop()
Thanks for any help
"Error 1 The type or namespace name 'Stopwatch' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Kakashi\Documents\Visual Studio 2005\Projects\ShootTheGreenLightGame\Form1.cs 40 9 ShootTheGreenLightGame
"
Um what am i missing here? I have this in just like the book says it to be, here is the code for the program; The stop watch is declared at the top and then its started in the main game loop ie public void Start()
and the stoped is called at public void Stop()
Thanks for any help
Code:
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;
using System.Diagnostics;
namespace ShootTheGreenLightGame
{
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;
Stopwatch stopWatch = new Stopwatch();
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;
//stop the watch
stopWatch.Stop();
long millis = stopWatch.ElapsedMilliseconds;
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
MessageBox.Show(String.Format("Game Over\n\nScore = {0}", score + "\n\nTime played is = {0}", millis));
Application.Exit();
}
public void Start()
{
//makes a new stopwatch for the game timer
stopWatch.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;
TimeSpan 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();
}
}
}
}