O'Reilly Forums: A Day At The Races - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

A Day At The Races

#1 User is offline   mexzony 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 23-July 11

Posted 23 July 2011 - 03:18 AM

hey guys am new to this forum and am currently learning c# using this head first c#.i love the book so much,i have read other books b4 but this book is great.am currently coding c# lab1,a day at the races.i have problem trying to make the picture box containing the dog move slowly,using the while loop and the for loop.i have coded some but the picture box moves so fast.i dont know how to make it move slow to give the impression of a race.please am a beginning programmer.any tip on how to implement that part of the code.
i dont want you to give me the whole code.just tips and maybe skeleton.i want to code this on my own so as to improve my skill and have satisfaction.thanks
0

#2 User is offline   Sikta_26785 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 67
  • Joined: 18-May 11

Posted 23 July 2011 - 05:40 AM

System.Threading.Thread.Sleep(1000);


That will pause the program for 1 second (the integer is in milliseconds). Put that into the loop where you call all the dogs run method. Change the number to adjust the speed.
0

#3 User is offline   mexzony 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 23-July 11

Posted 27 July 2011 - 07:47 AM

thanks.i have finished coding it it was fun and challenging.am happy and now i move on
0

#4 User is offline   Abaf 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 12-July 11

Posted 30 July 2011 - 04:25 AM

View Postmexzony, on 27 July 2011 - 07:47 AM, said:

thanks.i have finished coding it it was fun and challenging.am happy and now i move on


Another way would be using timer....
private void InitInstances() //It will run only once
{
Clock = new Timer();
Clock.Interval = 100;
Clock.Tick += new EventHandler(Clock_Tick);//will call Clock_Tick on each tick (of 100 ms)
}

private void Clock_Tick(object sender, EventArgs args)//call run mwthod on tick
{
while (!Result)
{
for (int i = 0 ; i <= 3 ; i++)
{
if (Result) break;
pt[i] = doggy[i].MyPictureBox.Location;
Result = doggy[i].Run(pt[i]);
winner = i;
}
}
0

#5 User is offline   Diaz 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 30-September 12

Posted 04 October 2012 - 10:44 AM

Oh briliant, thank you. I spent two days trying to get my dogs run slow, with timer.
I kept on turning at Type Write example, but I was not able to do it. I want to use timer in RACE button event instead of adding Tick event and Enabled property of Timer was set to true.

I believe that gave me a problem. When I start the debugger, the first method to be invoked was RACE button event in that case.

Then I changed Enabled=True to false in Timer object property. And to run the debugger. It started normal. But dogs were running super fast although I set the interval to 5000..... I knew the following code has something missing. But I was not sure how to fix it with proper understanding.

private void btnRace_Click(object sender, EventArgs e)
        {   
            btnBet.Enabled = false ;

            while (!isWon)
            {   
                for (i = 0; i < Dogs.Length; i ++)
                {
                    if (Dogs[i].Run())
                    {
                        MessageBox.Show("We have a winner! Dog #" + i);
                        Winner = i;

                        isWon = true;
                        timer1.Stop();
                        break;
                    }
                }
....
..
.



1. I am just curious to know how I could set Ticker into RACE button instead of having Tick event. Is it possible? If so what mistake did I do? Oh perhaps it's meant to invoke 'first method in an application' rather than setting on a event like RACE?

2. Secondly I am not sure why I had to set isWon bool to true....in order to run through the while loop. When I put in the default value (false) into While it never ran through the statements. Just slips out. Could you please give some reasoning on this bizzare behaviour as well?

3.
Your Run() method has a parameter, is it a Point[] type?

So I guess, in that case you do not use Randomizer to get value as the incrementor for each picturebox's X point.

4. I would like to know how we can implement Run() method as original, without parameters. And is it a must to have this initInitialize() method since Timer object is already added in the form... ?
Haven't seen this declaration in Type Writer application...


Thanks much!



View PostAbaf, on 30 July 2011 - 04:25 AM, said:

Another way would be using timer....
private void InitInstances() //It will run only once
{
Clock = new Timer();
Clock.Interval = 100;
Clock.Tick += new EventHandler(Clock_Tick);//will call Clock_Tick on each tick (of 100 ms)
}

private void Clock_Tick(object sender, EventArgs args)//call run mwthod on tick
{
while (!Result)
{
for (int i = 0 ; i <= 3 ; i++)
{
if (Result) break;
pt[i] = doggy[i].MyPictureBox.Location;
Result = doggy[i].Run(pt[i]);
winner = i;
}
}

This post has been edited by Diaz: 04 October 2012 - 10:59 AM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users