I have 4 dogs who're racing, I need to move them across the form but they don't move gradually, they start out at the starting line and immediately teleport to the finish line, without moving in between. With every timer tick, their location.X is incremented.
Do I need one timer or 4? I currently have one, and its interval is set to 400.
This is the relevant code:
private void btnRace_Click(object sender, EventArgs e)
{
btnBet.Enabled = false;
timer1.Stop();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{ while (!isWon)
{
for (i = 0; i < Dogs.Length; i++) // there are four dogs
{
if (Dogs[i].Run()) // Run() returns true if full racetrack is covered by this dog
{
Winner = i + 1;
isWon = true;
MessageBox.Show("We have a winner! Dog #" + Winner);
break;
}
}
}
And in the Dog class:
public bool Run()
{
Distance = 10 + Randomizer.Next(1, 4);
p = this.myPictureBox.Location;
p.X += Distance ;
this.myPictureBox.Location = p;
//return true if I won the game
if (p.X >= raceTrackLength)
{
return true ;
}
else
{
return false ;
}
}
The dogs only appear to move one step and then immediately show up on the finish line. What am I doing wrong?
Remove While loop from timer1_Tick method.
This method runs every 400 ms, but in your case at first launch it waits until one dog wins.
Also you should stop the timer after one of dogs win.
private void timer1_Tick(object sender, EventArgs e)
{
for (i = 0; i < Dogs.Length; i++) // there are four dogs
{
if (Dogs[i].Run()) // Run() returns true if full racetrack is covered by this dog
{
Winner = i + 1;
isWon = true;
timer1.Stop();
MessageBox.Show("We have a winner! Dog #" + Winner);
break;
}
}
}
Your timer goes off just once and is stuck in this loop;
while (!isWon)
{
}
Remove the loop and let the Timer do it's work
Add at the end
if (isWon) timer1.Stop();
Related
I'm creating a basic racing game in C# for an assignment where two picture boxes race from the left side of the form to the right side. What I am struggling with is resetting the position back to the 1st pixel on the left side of the form once the picture box has reached the end of the form on the right side. At the moment the picture boxes just keep going right and then disappear from the form and never come back.
That is what the layout of the game looks like:
I've tried searching google for snippets of code or even examples on how I might achieve this and have yet to find anything.
Any help would be much appreciated.
public partial class frmRacing : Form
{
public frmRacing()
{
InitializeComponent();
}
//This is the segment of intergers and the randomizer.
Random r = new Random();
int dir = 1;
int min, sec, ms = 0;
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
//This is the timer for "Player One". It moves the players picture box across the form at a random speed between 1-10 and times how long it takes to complete the total laps.
private void tmrOne_Tick(object sender, EventArgs e)
{
dir = r.Next(1, 10);
picStark.Left += dir;
lblTimer1.Text = min + ":" + sec + ":" + ms.ToString();
ms++;
if (ms > 100)
{
sec++;
ms = 0;
}
else
{
ms++;
}
if (sec > 60)
{
min++;
sec = 0;
}
}
//This is the timer for "Player Two". It moves the players picture box across the form at a random speed between 1-10 and times how long it takes to complete the total laps.
private void tmrTwo_Tick(object sender, EventArgs e)
{
dir = r.Next(1, 10);
picLannister.Left += dir;
lblTimer2.Text = min + ":" + sec + ":" + ms.ToString();
ms++;
if (ms > 100)
{
sec++;
ms = 0;
}
else
{
ms++;
}
if (sec > 60)
{
min++;
sec = 0;
}
}
//This is the start button. It enables all the timers and starts the race.
private void btnStart_Click(object sender, EventArgs e)
{
tmrOne.Enabled = true;
tmrTwo.Enabled = true;
tmrThree.Enabled = true;
}
private void hsbLaps_Scroll(object sender, ScrollEventArgs e)
{
lblLaps3.Text = lblLaps3.Text + 1;
}
//This is the overall timer for the race.
private void tmrThree_Tick(object sender, EventArgs e)
{
lblTimer3.Text = min + ":" + sec + ":" + ms.ToString();
ms++;
if (ms > 100)
{
sec++;
ms = 0;
}
else
{
ms++;
}
if (sec > 60)
{
min++;
sec = 0;
}
}
}
You could create a method which you call at the end of your tick events which checks if the Left property is greater than the width of your form.
private void ResetPicture(PictureBox pb)
{
// check if picture box left property is greater than the width
// of your form - the width of your picturebox
if (pb.Left >= this.Width - pb.Width)
{
// the picture has won the game, reset it
pb.Left = 1;
}
}
I need to wait the next line to be waiting until a timer finishes.
public void animation(){
timer1.start();
labelStatus.Visibility=true;
}
I want the labelStatus to be visible after the timer1 finished.
private void timer1_Tick(object sender, EventArgs e)
{
int fromX = lblMove.Location.X;
int fromY = lblMove.Location.Y;
if (fromY > moveToY)
{
Y = Y - 5;
lblMove.Location = new Point(fromX, Y);
}
else if (fromY < moveToY)
{
Y = Y + 5;
lblMove.Location = new Point(fromX, Y);
}
else
{
timer1.Stop();
}
}
thanks in advance.
Simplest solution I can think of is this
public void animation(){
timer1.start();
labelStatus.Visibility=false;
}
private void timer1_Tick(object sender, EventArgs e)
{
[...]
else if (fromY < moveToY)
{
Y = Y + 5;
lblMove.Location = new Point(fromX, Y);
}
else
{
timer1.Stop();
labelStatus.Visibility = true;
}
}
This way when you stop the timer you make the label visible. There are many other ways of doing this but this should be simple.
Second method.
Apparently Timer doesn't have a "OnStopped" event to hook to (unless you want to try with dispose). However a Timer has a System.Timer.Timer.Enabled flag for telling you that it's running.
It's not a clean solution, but you could create a new Thread and Poll "timer.Enabled" until it turns false.
Am new to C# and i need your help on this, I want to display one character at a time in a textbox this is my code
private void timer1_Tick(object sender, EventArgs e)
{
int i = 0; //why does this don't increment when it ticks again?
string str = "Herman Lukindo";
textBox1.Text += str[i];
i++;
}
private void button1_Click(object sender, EventArgs e)
{
if(timer1.Enabled == false )
{
timer1.Enabled = true;
button1.Text = "Stop";
}
else if(timer1 .Enabled == true )
{
timer1.Enabled = false;
button1.Text = "Start";
}
}
why does this don't increment when it ticks again?
Because your variable i is local to your event. You need to define it at class level.
int i = 0; //at class level
private void timer1_Tick(object sender, EventArgs e)
{
string str = "Herman Lukindo";
textBox1.Text += str[i];
i++;
}
On exit of your event, variable i becomes out of scope and looses its value. On the next event it is considered a new local variable with the initialized value of 0.
Next, you should also look for cross threaded exception. Since your TextBox is not getting updated on the UI thread.
The issue with you code is that you are assigning i = 0 with every tick, so it will always be 0 everytime it is used. I would suggest using a class level variable for this.
However, using a variable at class level means you are going to need to reset to 0 at some point, probably each time you start the timer.
A further point is that you are going to want to validate the tick event to ensure you don't try to access an index that doesn't exist (IndexOutOfRangeException). For this I would recommend automatically stopping the timer once the last letter has been printed.
With all that in mind, here is my suggested code:
int i = 0;// Create i at class level to ensure the value is maintain between tick events.
private void timer1_Tick(object sender, EventArgs e)
{
string str = "Herman Lukindo";
// Check to see if we have reached the end of the string. If so, then stop the timer.
if(i >= str.Length)
{
StopTimer();
}
else
{
textBox1.Text += str[i];
i++;
}
}
private void button1_Click(object sender, EventArgs e)
{
// If timer is running then stop it.
if(timer1.Enabled)
{
StopTimer();
}
// Otherwise (timer not running) start it.
else
{
StartTimer();
}
}
void StartTimer()
{
i = 0;// Reset counter to 0 ready for next time.
textBox1.Text = "";// Reset the text box ready for next time.
timer1.Enabled = true;
button1.Text = "Stop";
}
void StopTimer()
{
timer1.Enabled = false;
button1.Text = "Start";
}
Code updated now, only two problems left until I'm happy to feel I've completed it.
When the player hits a wall the counter goes down at 2 lifes per hit.
When the game starts the sound I have(beep.wav) which should only sound when the player hits a wall, is sounding each time I begin the game and the sound which should play throughout the game(onestop.wav) isn't playing at all.
public partial class Form1 : Form
{
// This SoundPlayer plays a song when the game begins.
System.Media.SoundPlayer onestop = new System.Media.SoundPlayer(#"C:\Users\kC\Favorites\Desktop\Kev stuff\Projects\MazeGame\onestop.wav");
// This SoundPlayer plays a sound whenever the player hits a wall.
System.Media.SoundPlayer beepSound = new System.Media.SoundPlayer(#"C:\Users\kC\Favorites\Desktop\Kev stuff\Projects\MazeGame\beep.wav");
// This SoundPlayer plays a sound when the player finishes the game.
System.Media.SoundPlayer clapSound = new System.Media.SoundPlayer(#"C:\Users\kC\Favorites\Desktop\Kev stuff\Projects\MazeGame\winningApplause.wav");
public Form1()
{
InitializeComponent();
timer1.Interval = (1000) * (1);
timer1.Enabled = true;
timer1.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
begin();
}
private void begin()
{
onestop.Play();
livesTextBox.Text = lives.ToString();
Point startingPoint = panel1.Location;
startingPoint.Offset(10, 10);
Cursor.Position = PointToScreen(startingPoint);
}
int lives = 5;
private void MoveToStart()
{
if ( lives > 0)
{
lives--;
}
if (lives == 0)
{
MessageBox.Show("You Lose!!");
Close();
}
else if (lives < 0)
{
MessageBox.Show("You Lose!!");
Close();
}
}
private void wall_MouseEnter(object sender, EventArgs e)
{
// When the mouse pointer hits a wall or enters the panel,
// call the MoveToStart() method.
beepSound.Play();
MoveToStart();
lives--;
livesTextBox.Text = lives.ToString();
}
private void finishLabel_MouseEnter(object sender, EventArgs e)
{
// Play a sound, show a congratulatory MessageBox, then close the form.
clapSound.Play();
MessageBox.Show("Congratulations! You've beaten the maze!");
Close();
}
int gameElapsed = 0;
private void timer1_Tick(object sender, EventArgs e)
{
gameElapsed++;
textBox1.Text = "" + gameElapsed.ToString();
}
}
You haven't posted the code very clearly, but I'll try and help.
Not sure about your sound issues. But: to 'call in your timer', I presume you want to put a number in the text box? You are half-way there. The Timer object simply calls the tick event every second, so you need to do the displaying part.
place the text box on your form, assume it is called textBox1.
put before the begin() method:
Stopwatch stopWatch = new Stopwatch();
Put inside the begin() method
stopWatch.Start();
inside the timer1_Tick method:
TimeSpan ts = stopWatch.Elapsed;
textBox1.Text = String.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds);
for your 'lives' bit, you'll need to put, before begin() method,
int lives = 0;
inside the begin(), put
int lives = 5;
livesTextBox.Text = lives.toString();
and inside wall_MouseEnter(), put
lives--;
livesTextBox.Text = lives.toString();
(assuming you have drawn a livesTextBox on your form)
Just Create a Timer on your form with interval 1000 and make it enable and then define game_elpased as integer as private field in the class and in timer_tick write :
void Timer1_Tick(object Sender,EventArgs e)
{
game_elapsed++;
textBox1.Text = "Elapsed Seconds : " + game_elapsed.ToString();
}
For the live times , You need to control the public variable like lives, when the player fails :
if(fails && lives>0){
lives--;
}
else if (lives<0)
{
MessageBox.Show("You are a looser ...");
}
The lives can be a property of the player class.
public class Player
{
int lives = 5;
public bool Kill()
{
this.lives--;
return this.lives <= 0;
}
public void run()
{
Player player = new Player();
// do stuff
// Check whether the player needs to die
if ("player fails".Contains("fail"))
{
if (player.Kill())
{
// restart level.
}
else
{
// Game over.
}
}
}
}
i want to develop a video player which has to play different parts in a video (specified by their start n end positions). I am using directx.audiovideoplayback dll for this purpose.
the starting and ending positions are stored in an array.
eg- {2,6,8,19} tells that segment between 2nd to 6th second has to be played and then 8th to 19th second should be played.
my problem is that despite me giving condition that
if(video_obj.CurrentPosition==endtime) video_obj.Stop();
the video isnt stopping..
the video is playing from 2nd position to end of file.
code is
public static int[] seconds = { 3,8,12,16};
public static int start, end;
private void btnPlay_Click(object sender, EventArgs e)
{
vdo.Owner = panel1;
panel1.Width = 300;
panel1.Height = 150;
vdoTrackBar.Minimum = 0;
vdoTrackBar.Maximum = Convert.ToInt32(vdo.Duration);
if (vdo != null)
{
vdo.Play();
timer1.Start();
}
}
private void vdoTrackBar_Scroll(object sender, EventArgs e)
{
if (vdo != null)
{
vdo.CurrentPosition = vdoTrackBar.Value;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int i;
for (i = 0; i < seconds.Length; i = i + 2)
{
start = seconds[i];
vdo.CurrentPosition = start;
end = seconds[i + 1];
vdoTrackBar.Value = (int)vdo.CurrentPosition;
MessageBox.Show("Starts at " + vdo.CurrentPosition + " and Ends at " + end);
if (vdo.Paused)
vdo.Play();
if (vdo.Playing)
{
if (vdo.CurrentPosition == vdo.Duration)
{
timer1.Stop();
vdo.Pause();
vdoTrackBar.Value = 0;
}
if (vdo.CurrentPosition == end)
{
timer1.Stop();
vdo.Pause();
}
vdoTrackBar.Value += 1;
}
}
Help! Somewhere something is wrong and i have no clue about it
How do i correct it?
Video starts playing when i
So there still isn't enough info in the post to really definitively say whats going wrong. I'm guessing that your timer isn't ticking with enough resolution to happen to tick exactly when the for loop in the timer tick would coincide with the video's current position.
How often does the timer tick? What's the precision of the video's current position?