Timer speed automatically increase - c#

I have following code. When I click on the button picturebox move from right to left and top to bottom. When it move to the end of the panel it again start from right to left and top to bottom. Now the problem is, after first completion of picturebox from right to left timer speed gradually increase though i set it to 200 also it seems that, this line myform.counterTop = myform.counterTop + 5; the value 5 also increase gradually. After first round, it increase a little, after second it increase little more and continues like this. Please tell me why this is happening.
namespace Spaceship_Invaders
{
public partial class Form1 : Form
{
private int invaderlanded = 0;
private int invaderstopped = 0;
private int counterfortop = -60;
private int counterforleft = 415;
private int counterTop = -60;
private int counterLeft = 415;
private bool pictureboxclicked = false;
private int timerinterval = 200;
System.Windows.Forms.Timer mytimer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
Image myImage = Image.FromFile("image/Untitled6.png");
pictureBox1.Image = myImage;
pictureBox1.Top = counterfortop;
pictureBox1.Left = counterforleft;
}
public class Spaceship
{
Form1 myform;
public Spaceship(Form1 form)
{
myform = form;
}
public void mspaceship()
{
myform.mytimer.Tick += new EventHandler(TimerEventProcessor);
myform.mytimer.Interval = myform.timerinterval;
myform.mytimer.Enabled = true;
myform.mytimer.Start();
}
private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
if (myform.pictureboxclicked)
{
myform.mytimer.Interval = 5;
myform.pictureBox1.Top = myform.counterTop;
//myform.pictureBox1.Left = myform.counterLeft;
myform.counterTop = myform.counterTop - 5;
if (myform.counterTop <-60)
{
//myform.pictureBox1.Enabled = false;
//myform.pictureBox1.Hide();
myform.pictureboxclicked = false;
myform.mytimer.Interval = myform.timerinterval;
myform.counterLeft = 415;
myform.counterTop = -60;
myform.mytimer.Stop();
}
} else {
if (myform.counterTop > 370 || myform.counterLeft < 1)
{
//myform.pictureBox1.Enabled = false;
//myform.pictureBox1.Hide();
myform.invaderlanded++;
myform.textBox2.Text = myform.invaderlanded.ToString();
myform.counterLeft = 415;
myform.counterTop = -60;
myform.pictureboxclicked = false;
myform.mytimer.Interval = myform.timerinterval;
myform.mytimer.Stop();
} else {
myform.pictureBox1.Top = myform.counterTop;
myform.pictureBox1.Left = myform.counterLeft;
myform.counterTop = myform.counterTop + 5;
myform.counterLeft = myform.counterLeft - 5;
}
}
}
}
private void button4_Click(object sender, EventArgs e)
{
Spaceship myspaceship = new Spaceship(this);
myspaceship.mspaceship();
}

Every time you call mspaceship(), you add another event handler to the timer.
The second time you click it, you have two event handlers which each move by 5 pixels.
Instead, you should only add the handler once.

Related

Holding down a key pauses the timer

I have an astro game with 2 picture boxes. One with a fireball(asteroid), and another with a UFO. I am moving the UFO with left and right key, handeled in keydown event. The asteroid falls from top to bottom. I am changing Y coordinate in a timer tick event. My problem is that if I hold down a key, the asteroid stops from falling and starts again as soon as I release the key.
UFOpictureBox is 100,100 picture box
Form Class
public partial class Form1 : Form
{
int hearts = 3, score = 0;
Point pozitieUFO, pozitieAsteroid;
Asteroid asteroid = new Asteroid();
Random random = new Random();
public Form1()
{
InitializeComponent();
DoubleBuffered = true;
KeyPreview = true;
pozitieUFO = UFOpictureBox.Location;
}
private void startBtn_Click(object sender, EventArgs e)
{
hearts = 3;
score = 0;
gameOverLabel.Visible = false;
startBtn.Visible = false;
exitBtn.Visible = false;
Controls.Add(asteroid);
pozitieAsteroid = asteroid.Location;
asteroidTimer.Start();
}
private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Left) pozitieUFO.X -= 10;
if (pozitieUFO.X <= 12) pozitieUFO.X = 12;
if (e.KeyData == Keys.Right) pozitieUFO.X += 10;
if (pozitieUFO.X >= 792) pozitieUFO.X = 792;
UFOpictureBox.Invalidate();
UFOpictureBox.Location = pozitieUFO;
}
private void asteroidTimer_Tick(object sender, EventArgs e)
{
pozitieAsteroid.Y += score/5 + 5;
if (pozitieAsteroid.Y > 420)
{
hearts--;
heartsLabel.Text = "Hearts: " + hearts;
if (hearts <= 0)
{
gameOver();
return;
}
pozitieAsteroid.Y = 1;
pozitieAsteroid.X = random.Next(12, 792);
}
if (asteroid.Bounds.IntersectsWith(UFOpictureBox.Bounds))
{
pozitieAsteroid.Y = 1;
pozitieAsteroid.X = random.Next(12, 792);
score++;
scoreLabel.Text = "Score: " + score;
}
asteroid.Invalidate();
asteroid.Location = pozitieAsteroid;
}
private void gameOver()
{
startBtn.Visible = true;
exitBtn.Visible = true;
gameOverLabel.Visible = true;
asteroidTimer.Stop();
}
}
Asteroid Class
class Asteroid : PictureBox
{
Random random = new Random();
public Asteroid()
{
Width = Height = 50;
SizeMode = PictureBoxSizeMode.StretchImage;
BackColor = Color.Transparent;
Image = Properties.Resources.fire;
Left = random.Next(12,792);
Top = 0;
}
}

Move a picture box faster and faster with a timer on click in C#

I have a class assignment to move a picturebox randomly across the form. Once you click on the picturebox, it is supposed to scream and change the picture then change it back to the original picture. When you click again, it is supposed to go faster. I have it working up to the point of making it go faster. Here is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tm1.Interval = 1000;
tm1.Tick += new EventHandler(tm_Tick);
}
Timer tm1 = new Timer();
int X = 0;
int Y = 0;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (timer1.Enabled)
{
timer1.Stop();
pictureBox1.Image = Properties.Resources.Mimikyu;
Application.DoEvents();
pictureBox1.WaitOnLoad = true;
System.Threading.Thread.Sleep(10);
SoundPlayer sp = new SoundPlayer(Properties.Resources.screa);
sp.PlaySync();
pictureBox1.Image = Properties.Resources.Evee;
}
else
timer1.Start();
}
private void tm_Tick(object sender, EventArgs e)
{
int X = ((int)(new Random().Next(0, 1000)));
int Y = ((int)(new Random().Next(0, 500)));
if (X > 1025 - pictureBox1.Width)
{
X = 1025 - pictureBox1.Width;
}
if (Y > 545 - pictureBox1.Height)
{
Y = 545 - pictureBox1.Height;
}
pictureBox1.Location = new Point(X, Y);
}
}
}
Point me to where I need to go to get the interval to move faster and faster after each click Thank you.
decreasing the tm1.Interval should do it
...
else
if (tm1.Interval>10){tm1.Interval -= 10;}
timer1.Start();

How do I display values under the cursor in the second form?

I draw a candlestick chart from the file data.
Create a new form(second form) when you click the button in which you want to display:
time, high, low, open, close.
namespace stock5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.Load += new System.EventHandler(this.CandleStick_Load);
}
private void CandleStick_Load(object sender, EventArgs e)
{
CHART();
}
public void CHART()
{
*************************************************
//The code reads the data from the file is skipped.
chart1.Series.Clear();
Series price = new Series("price");
chart1.Series.Add(price);
chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chart1.ChartAreas[0].AxisY.IsStartedFromZero = false;
chart1.Series["price"].ChartType = SeriesChartType.Candlestick;
chart1.Series["price"]["OpenCloseStyle"] = "Triangle";
chart1.Series["price"]["ShowOpenClose"] = "Both";
chart1.Series["price"]["PointWidth"] = "2.0";
chart1.Series["price"]["PriceUpColor"] = "Blue";
chart1.Series["price"]["PriceDownColor"] = "Red";
chart1.Series["price"].BorderColor = Color.Black;
chart1.Series["price"]["MaxPixelPointWidth"] = "2.0";
for (i = 0; i < count - 1; i++)
{
chart1.Series["price"].Points.AddXY(index[i], mass[i, 1], mass[i, 2], mass[i, 0], mass[i, 3]);//index, high, low, open, close
}
int INDEX = 0;
foreach (DataPoint point in chart1.Series["price"].Points)
{
point.AxisLabel = nums[INDEX].ToString();//Replacing the index values for the time(To avoid empty values when markets are closed on weekends)
INDEX++;
}
}
}
}
The second form in which you want to print values depending on the location of the cursor.
private void button1_Click(object sender, EventArgs e)
{
Form newForm = new Form();
newForm.Show();
newForm.Width = 170;
newForm.Height = 230;
}
And a passing question: how to get instead of indexes in the form of time?
There is multiple ways you can communicate with first form in time and here is one of it:
I assume your Form is your new form? (I will use it in my example as it is)
For this example i will create custom class (object) which i will be returning to newly created form every 0.2 sec.
So new object would be
public class MyNewObject
{
int Id { get; set; }
string Name { get; set; }
int Speed { get; set; }
}
So class of Form will look like this (at least code which is needed to perform this)
public partial class Form : Form
{
Timer t = new Timer();
private YourFirstForm myFirstForm;
public Form(YourFirstForm form)
{
InitializeComponents();
myFirstForm = form;
t.Interval = 200; //0.2 sec
Thread t1 = new Thread(FirstFormListener);
t1.Start();
}
private void FirstFormListner()
{
Timer t = new Timer();
t.Interval = 200; //0.2 sec
t.Tick += new EventHandler(timer1_Tick);
t.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
MyNewObject someData = myFirstForm.GetSomeData();
MessageBox.Show(someData.Name);
}
}
And inside your first form:
public partial class YourFirstForm : Form
{
string someString = "Some random string";
public YourFirstForm()
{
InitializeComponents();
}
public MyNewObject GetSomeData()
{
MyNewObject mno = new MyNewObject();
mno.Name = "Random name";
mno.Id = 1;
mno.Speed = 200;
return mno;
}
}
This is one way of doing it and i think it is practical since you can adjust interval of thick so set update time (on slower pc set it to lower value for faster you can to faster value). Also updating is done on separate thread so it doesn't affect your UI (freeze) and you can continue with job.

How to fade in and fade out (Fading transition ) image on panel(backgroud image)?

I'm sending to my method different images and I want insert some effect to this change.
How can I fade in and fade out images?
private void ShowImage(Image image, ImageLayout imageLayout, int numberOfSeconds)
{
try
{
if (this.image_timer != null)
this.KillImageTimer();
this.customer_form.DisplayImage(image, imageLayout);
this.image_timer = new Timer();
this.image_timer.Tick += (object s, EventArgs a) => NextImage();
this.image_timer.Interval = numberOfSeconds* 1000;
this.image_timer.Start();
}
catch
{
//Do nothing
}
public void DisplayImage(Image image, ImageLayout imageLayout)
{
panel1.BackgroundImage = image;
panel1.BackgroundImageLayout = imageLayout;
}
There are no built-in fading transitions in Winforms.
So you will need to write one yourself.
The simplest one I can think of uses a second Panel, that is layered upon the first one and in fact needs to be inside the first Panel or else the transparency effect won't work..
Here is the setup, using two Panels:
public Form1()
{
InitializeComponent();
pan_image.BackgroundImage = someImage;
pan_layer.Parent = pan_image;
pan_layer.BackColor = pan_image.BackColor;
pan_layer.Size = pan_image.Size;
pan_layer.Location = Point.Empty;
}
For the fading animation I use a Timer. This is a quick code example:
Timer timer1 = new Timer();
int counter = 0;
int dir = 1; // direction 1 = fade-in..
int secondsToWait = 5;
int speed1 = 25; // tick speed ms
int speed2 = 4; // alpha (0-255) change speed
void timer1_Tick(object sender, EventArgs e)
{
// we have just waited and now we fade-out:
if (dir == 0)
{
timer1.Stop();
dir = -speed2;
counter = 254;
timer1.Interval = speed2;
timer1.Start();
}
// the next alpha value:
int alpha = Math.Min(Math.Max(0, counter+= dir), 255);
button1.Text = dir > 0 ? "Fade In" : "Fade Out";
// fully faded-in: set up the long wait:
if (counter >= 255)
{
timer1.Stop();
button1.Text = "Wait";
timer1.Interval = secondsToWait * 1000;
dir = 0;
timer1.Start();
}
// fully faded-out: try to load a new image and set direction to fade-in or stop
else if (counter <= 0)
{
if ( !changeImage() )
{
timer1.Stop();
button1.Text = "Done";
}
dir = speed2;
}
// create the new, semi-transparent color:
Color col = Color.FromArgb(255 - alpha, pan_image.BackColor);
// display the layer:
pan_layer.BackColor = col;
pan_layer.Refresh();
}
I start it in a Button, on which I also show the current state:
private void button1_Click(object sender, EventArgs e)
{
dir = speed2;
timer1.Tick += timer1_Tick;
timer1.Interval = speed1;
timer1.Start();
}
As you can see I use two speeds you can set: One to control the speed of the Timer and one to control the steps by which the transparency changes on each Tick.
The effect is created by simply changing the Color from the BackgroundColor of the image Panel to fully transparent and back, waiting in between for a specified number of seconds.
And the end of the effect I call a function changeImage() to change the images. If this function returns false the Timer is stopped for good..
I'm pretty sure this could be written in a cleaner and more elegant way, but as it is it seems to work..
Update
for flicker-free display use a double-buffered control, like this Panel subclass:
class DrawPanel : Panel
{
public DrawPanel() { DoubleBuffered = true; }
}
Here is a sample implementation for changeImage:
bool changeImage()
{
if (pan_image.BackgroundImage != null)
{
var img = pan_image.BackgroundImage;
pan_image.BackgroundImage = null;
img.Dispose();
}
pan_image.BackgroundImage = Image.FromFile(imageFiles[index++]);
return index < imageFiles.Count;
}
It assumes two class level variables: a List<string> imageFiles filled with file names of images for a slide-show and an int index = 0.

Iterating through an array and play notes

I have an array which is populated by MusicNotes. Each MusicNote is an object with it's properties, e.g. pitch and duration. Duration is created using a timer in the MusicNote class.
The problem is that when I iterate through the array and play all the sounds the duration of each MusicNote is lost and it will play the whole wav file (for each note).
I know that the problem is related to the timer and I know that it maybe related to the Play() method in the MusicNote but I don't have any ideas on how to fix it. I have posted my the code related to this problem.
public class MusicNote : PictureBox
{
Timer tmr1 = new Timer();
int tmr1duration;
public SoundPlayer sp = new SoundPlayer();
public Timer tmr = new Timer();
public int pitch; //The no. of the music key (e.g. the sound freuency).
public int noteDuration; //Shape of note.
public string noteShape;
static int xLoc = 0;
int yLoc = 100;
public MusicNote(int iPitch, int iNoteDuration)
: base()
{
pitch = iPitch;
noteDuration = iNoteDuration;
Size = new Size(40, 40);
this.BackColor = Color.Transparent;
this.MouseClick += new MouseEventHandler(MusicNote_MouseClick);
this.MouseDown += new MouseEventHandler(MusicNote_MouseDown);
this.MouseUp += new MouseEventHandler(MusicNote_MouseUp);
tmr1.Tick += new EventHandler(tmr1_Tick);
tmr.Tick += new EventHandler(ClockTick);
}
public void ShowNote()
{
if (this.noteDuration == 1) noteShape = "Quaver.png";
if (this.noteDuration == 4) noteShape = "Crotchet.png";
if (this.noteDuration == 7) noteShape = "minim.png";
if (this.noteDuration == 10) noteShape = "DotMin.png";
if (this.noteDuration == 12) noteShape = "SemiBreve.png";
this.BackgroundImage = Image.FromFile(noteShape);
this.BackColor = Color.Transparent;
Location = new Point(xLoc, yLoc);
xLoc = xLoc + 40;
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
public void Play()
{
sp.SoundLocation = this.pitch + ".wav";
sp.Play();
//Timer to play the duration
this.tmr.Interval = 100 * this.noteDuration;
this.tmr.Start();
}
void ClockTick(object sender, EventArgs e)
{
sp.Stop();
tmr.Stop();
}
private void MusicNote_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Play();
}
}
}
}
And this is the class were I have the array...
public class MusicStaff: Panel
{
public ArrayList musicNotes = new ArrayList(); //Array to store the Music Notes.
SoundPlayer sp = new SoundPlayer();
public void AddNote(MusicNote newNote) //Method to add the notes.
{
musicNotes.Add(newNote);
}
public int ListSize()
{
return musicNotes.Count; //Returns the size of the list.
}
public void PlayAll()
{
foreach (MusicNote m in musicNotes)
{
m.Play();
}
I have removed some code from the classes which is not replated to the problem so that question is not too long. Any help how can I solve this would be greatly appreciated. Tks.
Try something like this:
public void PlayAll()
{
foreach (MusicNote m in musicNotes)
{
m.sp.Stop();
m.sp.PlaySync();
Thread.Sleep(m.noteDuration); //duration should be in milliseconds
}
}
Should this work you can remove your timer completely.
I suggest you look into the use of Properties in C# to not let fields be public.
By the way I did this assignment two years ago :)

Categories

Resources