I am working on the first lab of headfirst C#. I ran into a problem in my program that the dogs go at the same speed. I cant figure out why they do so, because in my eyes every object instance get a random 1 to 5 pixels added to its X location. The randomness of this should make enough of a difference.
So because I dont want to post my entire set of classes of Lab 1 I recreated a small and independent version with only two dogs racing, and without the betting aspect.
Form1.Designer contains: -Two pictureboxes with a hound in it. -a Start button
Greyhound.cs class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace test
{
public class Greyhound
{
public int DogID;
public PictureBox myPictureBox;
public Point StartingPosition;
public Point CurrentPosition;
public Random Randomizer;
public bool Run()
{
int AddDistance = Randomizer.Next(1, 7);
CurrentPosition.X += AddDistance;
myPictureBox.Location = CurrentPosition;
if (CurrentPosition.X > 600)
{
return true;
}
else
{
return false;
}
}
public void ReturnToStart()
{
CurrentPosition = StartingPosition;
myPictureBox.Location = StartingPosition;
}
}
}
Form1.cs class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Greyhound One = new Greyhound() { DogID = 1, myPictureBox = pictureBox1, StartingPosition = pictureBox1.Location, CurrentPosition = pictureBox1.Location, Randomizer = new Random() };
Greyhound Two = new Greyhound() { DogID = 2, myPictureBox = pictureBox2, StartingPosition = pictureBox1.Location, CurrentPosition = pictureBox2.Location, Randomizer = new Random() };
if (One.Run())
{
timer1.Enabled = false;
MessageBox.Show("Dog One WON!");
}
else if (Two.Run())
{
timer1.Enabled = false;
MessageBox.Show("Dog Two WON!");
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
}
}
Can anyone see something wrong with the way this is created? A reason for why the dogs would run at the same speed instead of random 1 to 5 pixels per time.