I wanna write an application to do below job:
We have 15 different location (that we present them with point (x1,y1) for first location, point (x1,y2) for second location and so on point (x1,y15) for last location).
100 image must to be shown at this locations (with specific order) with pictureBox.
Each image that shown, must start to move horizontally to reach new point (e.g. point (Xc, Yc) ) and then move vertically and so on…
When the image reach to specific point (e.g. point (Xm,Ym) ), we decide that it must continue moving or it must destroy (with probability 20%) (which means that we can create next image at that initial location).
For example, an image create at location (Xi,Yi) with pictureBox1. Then it’s not allowed to create any more image at location (Xi,Yi) until pictureBox1 destroy or return to its initial location.
What I have written so far is:
create 15 location.
move one image to reach point (Xc,Yc).
I’ve some problems:
I use one timer to move an image. But I want to move 100 images. (from each 15 location, we create images and move them until we destroy them and then create next image).
So what I must to do? Use 15 timer for each location?!but How?
at specific point (e.g. point (Xk,Yk) ), the image must stop moving for random seconds and then continue moving. How I should do this? With another timer?!But how?
when image reach point (Xc,Yc) and with we decide to destroy it or not, nothing happen in my code…I don’t know why!
I’ve add an picture of what I want HERE
Here is my code so far:
public partial class Form1 : Form
{
Random r = new Random();
// falg to prevent create just one image at each location
private Boolean[] createNext = {true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true};
private void setImage()
{
int i = 1 + r.Next() % 15;
while (createNext[i] != true)
i = 1 + r.Next() % 15;
switch (i)
{
case 1: pictureBox1.ImageLocation = "grin.png";
break;
case 2: pictureBox2.ImageLocation = "grin.png";
break;
case 3: pictureBox3.ImageLocation = "grin.png";
break;
case 4: pictureBox4.ImageLocation = "grin.png";
break;
case 5: pictureBox5.ImageLocation = "grin.png";
break;
case 6: pictureBox6.ImageLocation = "grin.png";
break;
case 7: pictureBox7.ImageLocation = "grin.png";
break;
case 8: pictureBox8.ImageLocation = "grin.png";
break;
case 9: pictureBox9.ImageLocation = "grin.png";
break;
case 10: pictureBox10.ImageLocation = "grin.png";
break;
case 11: pictureBox11.ImageLocation = "grin.png";
break;
case 12: pictureBox12.ImageLocation = "grin.png";
break;
case 13: pictureBox13.ImageLocation = "grin.png";
break;
case 14: pictureBox14.ImageLocation = "grin.png";
break;
case 15: pictureBox15.ImageLocation = "grin.png";
break;
}
}
private int k = 0;
private Boolean destroy = true;
// timer that move pictureBox1
void timer_Tick(object sender, EventArgs e)
{
k++;
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (k <= 150)//go right 300 in 150 ticks
pictureBox1.Location = new Point(x + 2, y);
else if (k <= 300)
{
if (y < 200)
pictureBox1.Location = new Point(x, y + 1);
if (y > 200)
pictureBox1.Location = new Point(x, y - 1);
}
else if (k <= 400)
pictureBox1.Location = new Point(x + 2, y);
else if (k <= 550)
{
if (destroy == false)
pictureBox1.Location = new Point(x + 2, y);
if (destroy == true)
pictureBox1.Location = new Point(x, y - 3);
}
else if (k <= 650)
pictureBox1.Location = new Point(x, y - 1);
else if (k <= 850)
pictureBox1.Location = new Point(x - 2, y);
else if (k <= 950)
pictureBox1.Location = new Point(x, y + 1);
else
timer1.Stop();
}
public Form1()
{
InitializeComponent();
for (int i = 0; i < 15; i++)
{
setImage();
timer1.Start();
timer1.Interval = 15;
timer1.Tick += new EventHandler(timer_Tick);
}
}
Please help me to complete this application.
Thanks in advance.
You should probably just use one timer, and make a number of instances of a class that represents your moving picturebox. (This class would contain the points it will go to, ordered to the first point is always your next destination. Also it will contain the picturebox.. etc).
enum Action
{
None,
Stop,
CheckDestroy
}
class InterestingPoint
{
Point m_Point;
Action m_Action;
}
class MovingImage
{
public bool DestroyMe {get; private set; }
PictureBox m_PictureBox;
List<InterestingPoint> m_PointsToVisit;
int m_StoppedUntil = 0;
MovingImage(...)
{
// Constructor
}
void Update(int k)
{
if(m_StoppedUntil > k) return;
//Move m_PictureBox towards m_PointsToVisit.First
//when m_PictureBox.Location == m_PointToVisit.First, check if the InterestingPoint
//has a action you need to handle,
//like if the action is stop, you set m_StoppedUntil to k+ the number of frames you
//want the picturebox to stay still. (m_StoppedUntil = k+10 => doesnt update for 10 frames)
//if the action is checkdestroy, see if it shall be destroyed and set DestroyMe to true
}
So, you initialize your MovingImage objects with a picturebox and list of point it should move to, these points contain a action for the object to execute when it arrives there.
Keep your MovingImages in a list of some kind and iterate over them to update them.
In your Timer_Tick you do something like
void timer_Tick(object sender, EventArgs e)
{
k++
foreach(MovingImage m in m_MyMovingImages)
{
m.Update(k);
if(m.DestroyMe)
{
//Destroy it.
}
}
}
Related
What I'm trying to do is this:
I want the 4 pictureBoxes I got in the form to get the faces of card - turn them over I mean. a random card from the 4 will be chosen, turned over and show the face of the card being chosen also randomley.
Once a card is turned over, it cant be turend over again in the next timer interval, and once all cards had been turned over, a messageBox appears and once the user presses ok, it all restarts.
Problem is: the messageBox keeps appearing over and over again, because of the flag positive value. I don't know which variable should I use to prevent that from happening.
Relevant code:
//This function is the timer's function, it starts every time interval:
private void cardsChangingTimer_Tick(object sender, EventArgs e)
{
int chosenImage = rnd.Next(1, 17);
int chosenCard = rnd.Next(0, 4);
if (bucketArr[chosenCard] == 0)
{
bucketArr[chosenCard]++;
switch (chosenCard)
{
case 0:
card1Pic.Image = Image.FromFile("cards\\" + chosenImage + ".png");
break;
case 1:
card2Pic.Image = Image.FromFile("cards\\" + chosenImage + ".png");
break;
case 2:
card3Pic.Image = Image.FromFile("cards\\" + chosenImage + ".png");
break;
case 3:
card4Pic.Image = Image.FromFile("cards\\" + chosenImage + ".png");
break;
}
}
gameEnded = true;
for (int i = 0; i < bucketArr.Length; i++)
{
if (bucketArr[i] == 0)
{
gameEnded = false;
break;
}
}
if (gameEnded)
{
DialogResult dialog = MessageBox.Show("All 4 cards were turned over...");
if (dialog == DialogResult.OK)
{
card1Pic.Image = Image.FromFile("..\\..\\17.png");
card2Pic.Image = Image.FromFile("..\\..\\17.png");
card3Pic.Image = Image.FromFile("..\\..\\17.png");
card4Pic.Image = Image.FromFile("..\\..\\17.png");
}
gameEnded = false;
for(int i = 0; i < bucketArr.Length; i++)
bucketArr[i] = 0;
}
}
Thanks alot for any help...
The problem you are having is the timer is continuing to run while the dialog box is shown. Simply stop the timer before the MessageBox is shown then restart the timer after the box returns.
private void cardsChangingTimer_Tick(object sender, EventArgs e)
{
int chosenImage = rnd.Next(1, 17);
int chosenCard = rnd.Next(0, 4);
/// ... Rest of the code goes here
break;
}
}
if (gameEnded)
{
//Get a reference to the timer and stop it.
var timer = (Timer)sender;
timer.Stop();
DialogResult dialog = MessageBox.Show("All 4 cards were turned over...");
if (dialog == DialogResult.OK)
{
card1Pic.Image = Image.FromFile("..\\..\\17.png");
card2Pic.Image = Image.FromFile("..\\..\\17.png");
card3Pic.Image = Image.FromFile("..\\..\\17.png");
card4Pic.Image = Image.FromFile("..\\..\\17.png");
}
gameEnded = false;
for(int i = 0; i < bucketArr.Length; i++)
bucketArr[i] = 0;
//start the timer here after everything has been re-initialized.
timer.Start();
}
}
I have this simple code in C# that basically implements a blur filter to an image based on the value of a Trackbar in Windows Forms.
private void BarraBlur_Scroll_1(object sender, EventArgs e)
{
// I take the image inside picture box and use my own Effects class
Bitmap image = new Bitmap(pictureBox1.Image);
Effects foto = new Effects(image);
switch (BarraBlur.Value)
{
case 0:
pictureBox1.Image = imagens; // This reestablishes the image to its original state
break;
case 1:
// Each case reestablishes the image to its original state
// and then applies the blur filter with a given depth
pictureBox1.Image = imagens;
pictureBox1.Image = foto.BlurEffect(1);
break;
case 2:
pictureBox1.Image = imagens;
pictureBox1.Image = foto.BlurEffect(2);
break;
case 3:
pictureBox1.Image = imagens;
pictureBox1.Image = foto.BlurEffect(3);
break;
case 4:
pictureBox1.Image = imagens;
pictureBox1.Image = foto.BlurEffect(4);
break;
case 5:
pictureBox1.Image = imagens;
pictureBox1.Image = foto.BlurEffect(5);
break;
default:
break;
}
}
What I want is that, depending on the value of the trackbar whenever it is set, to apply the blur filter to the image. The BlurEffect method takes an int argument so that the filter is applied in such value of depth.
The problem is that, if for example, the user sets the trackbar in the second position, it works fine, but when it sets it back to the first position, instead of returning the image to its original state and then applying the blur filter with depth 1, it applies a blur of depth 1 to the already blurred image with depth 2.
In other words, I want the trackbar to augment the depth of the filter each time the tick goes to the right and diminish the depth of the blur each time the tick of the trackbar goes left.
I have tried this with switch cases and if statements, but neither have worked.
Thanks a lot in advance.
This is the Effects class with the BlurEffect method.
class Effects
{
private Bitmap imagen;
// Constructor
public Effects(Bitmap item)
{
imagen = item;
}
public Bitmap BlurEffect(int depth)
{
for (int k = 0; k < depth; k++)
{
for (int i = 2; i < imagen.Width; i++)
{
for (int j = 2; j < imagen.Height; j++)
{
try
{
Color antX1 = imagen.GetPixel(i - 1, j);
Color antX2 = imagen.GetPixel(i - 2, j);
Color desX1 = imagen.GetPixel(i + 1, j);
Color desX2 = imagen.GetPixel(i + 2, j);
Color antY1 = imagen.GetPixel(i, j - 1);
Color antY2 = imagen.GetPixel(i, j - 2);
Color desY1 = imagen.GetPixel(i, j + 1);
Color desY2 = imagen.GetPixel(i, j + 2);
int promR = (int)((antX1.R + antX2.R + desX1.R + desX2.R + antY1.R + antY2.R + desY1.R + desY2.R + imagen.GetPixel(i, j).R) / 9);
int promG = (int)((antX1.G + antX2.G + desX1.G + desX2.G + antY1.G + antY2.G + desY1.G + desY2.G + imagen.GetPixel(i, j).G) / 9);
int promB = (int)((antX1.B + antX2.B + desX1.B + desX2.B + antY1.B + antY2.B + desY1.B + desY2.B + imagen.GetPixel(i, j).B) / 9);
imagen.SetPixel(i, j, Color.FromArgb(promR, promG, promB));
}
catch (Exception) { }
}
}
}
return imagen;
}
}
Firstly, you don't need the big switch with the repeated code. You can reduce your code to this:
if (BarraBlur.Value = 0)
{
pictureBox1.Image = imagens; // This reestablishes the image to its original state
}
else {
pictureBox1.Image = foto.BlurEffect(BarraBlur.Value);
}
However, this won't fix your problem. I don't know what class foto is, but it sounds like it is a wrapper for a Bitmap, and the BlurEffect() function applies a blur effect to that bitmap, before returning it. Your code appears to be written with the assumption that BlurEffect returns a blurred copy of the bitmap.
You can probably fix your code by changing it to construct a new foto every time you change the blur:
if (BarraBlur.Value = 0)
{
pictureBox1.Image = imagens; // This reestablishes the image to its original state
}
else {
var blur = new Foto(imagens); // or however the foto is constructed
pictureBox1.Image = blur.BlurEffect(BarraBlur.Value);
}
Since you've indicated that you wrote/control the foto class (named Effects), you could also change how that class works (instead of doing the above).
Sounds like currently it looks something like:
class Effects
{
private Bitmap _image;
public Effects(Bitmap image)
{
_image = image;
}
public Bitmap Blur(int amount)
{
// .. code to perform blur on _image ..
return _image;
}
}
And you should probably change it to be more like:
class Effects
{
private Bitmap _image;
public Effects(Bitmap image)
{
_image = image;
}
public Bitmap Blur(int amount)
{
var copy = _image.Clone();
// .. code to perform blur on copy ..
return copy;
}
}
pictureBox1.Image = imagens;
if (BarraBlur.Value == 0) return;
pictureBox1.Image = foto.BlurEffect(BarraBlur.Value);
Above replaces your whole code. Your problem is withing BlurEffect method. For that reason whenever you use 0 it will work because you will return image to the original. You need to reload image to the original as well at the start of BlurEffect method instead of what I'm guessing reusing picture.
I have a PictueBox and I have some dice, I would like to play an animation for the "rolling" of the dice, I did a .gif with the dice, but after the dice stop rolling, I want the actual dice number that I got, I have a random funcion that handles that.
My question is, I press the "Roll Dice" button, it plays the animation and after the animation ends I should set int the picturebox the dice that actually came. but it immediately chnages to the dice number that actually came, skipping the animation;
This is how it works:
dice1.Image = Resources.DiceAnimation; //Here the gif is called to be played
int x = rollDice(); //Here I roll the dice
switch (x){
case 1: dice.Image = resources.diceFace1; //Image set depending on x
break
case 2: //etc...
}
There might be two things needed to do that.
Firstly, you may need to ensure that your PictureBox receives a gif image and it knows it. To do this, please check this answer and this answer. The posts have code to show GifImage frame by frame:
public class GifImage
{
private Image gifImage;
private FrameDimension dimension;
private int frameCount;
private int currentFrame = -1;
private bool reverse;
private int step = 1;
public GifImage(string path)
{
gifImage = Image.FromFile(path);
//initialize
dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
//gets the GUID
//total frames in the animation
frameCount = gifImage.GetFrameCount(dimension);
}
public bool ReverseAtEnd {
//whether the gif should play backwards when it reaches the end
get { return reverse; }
set { reverse = value; }
}
public Image GetNextFrame()
{
currentFrame += step;
//if the animation reaches a boundary...
if (currentFrame >= frameCount || currentFrame < 1) {
if (reverse) {
step *= -1;
//...reverse the count
//apply it
currentFrame += step;
}
else {
currentFrame = 0;
//...or start over
}
}
return GetFrame(currentFrame);
}
public Image GetFrame(int index)
{
gifImage.SelectActiveFrame(dimension, index);
//find the frame
return (Image)gifImage.Clone();
//return a copy of it
}
}
Use it like this (note that you need a Timer object):
private GifImage gifImage = null;
private string filePath = #"C:\Users\Jeremy\Desktop\ExampleAnimation.gif";
public Form1()
{
InitializeComponent();
//a) Normal way
//pictureBox1.Image = Image.FromFile(filePath);
//b) We control the animation
gifImage = new GifImage(filePath);
gifImage.ReverseAtEnd = false; //dont reverse at end
}
private void button1_Click(object sender, EventArgs e)
{
//Start the time/animation
timer1.Enabled = true;
}
//The event that is animating the Frames
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image = gifImage.GetNextFrame();
}
Secondly, to know how long you want to run your GIF image, you may need to Get Frame Duration of GIF image like this:
double delayIn10Ms; //declare somewhere
//Initialize on your form load
PropertyItem item = img.GetPropertyItem (0x5100); // FrameDelay in libgdiplus
// Time is in 1/100th of a second
delayIn10Ms = (item.Value [0] + item.Value [1] * 256) * 10;
Then use the delayIn10Ms time plus, probably, a little bit more time to stop your timer. You may also want to check when was the last time your timer Ticks and store it. If it exceeds the given delay time, then you should stop your timer and start it again on dice roll, after image assignment in your switch case.
DateTime currentTick = DateTime.Min;
DateTime startTick = DateTime.Min;
private void timer1_Tick(object sender, EventArgs e)
{
currentTick = DateTime.Now;
if ((currentTick - startTick).TotalSeconds / 100 < delayIn10Ms)
pictureBox1.Image = gifImage.GetNextFrame();
else
timer1.Stop(); //stop the timer
}
//And somewhere else you have
timer1.Start(); //to start the timer
int x = rollDice(); //Here I roll the dice
switch (x){
case 1: dice.Image = resources.diceFace1; //Image set depending on x
break
case 2: //etc...
}
You can make a timer with the Interval property set to the length of the animation and set it's Tag to 0 and in the timer write the code:
if(timer.Tag == "0")
timer.Tag == "1";
else if(timer.Tag == "1")
{
int x = rollDice();
switch (x)
{
case 1: dice.Image = resources.diceFace1; break;
case 2: //etc...
}
timer.Tag == "0";
timer.Stop();
}
I have two forms Form 1 and Form 2.
Form 2 is inheriting from Form 1. In Form 1 I have a method on_Paint for a particular panel, in which I am drawing an image. Since Form 2 is inheriting from Form 1, the panel is being shown as painted before I even run the program (on the design of Form 2).
The problem is, that now I need to paint some things over this image after the image is painted in Form 2. How can I make this possible?
This is some code to make the problem more clear.
// This is Form 1 on_Paint method
private void grid_Paint(object sender, PaintEventArgs e)
{
Image img = Image.FromFile(resourcesPath+ "grid.fw.png");
gridGraphics = grid.CreateGraphics();
gridGraphics.DrawImage(img, 0, 0, 650, 550);
}
// This is the method which I want to be invoked after the grid is painted.. This is located in Form 1:
public void paintSprites(int row, int column, int value)
{
int yLoc = 0;
int xLoc = 0;
Graphics g = grid.CreateGraphics();
switch (row)
{
case 0: yLoc = 435; break;
case 1: yLoc = 355; break;
case 2: yLoc = 275; break;
case 3: yLoc = 195; break;
case 4: yLoc = 115; break;
case 5: yLoc = 35; break;
}
switch (column)
{
case 0: xLoc = 35; break;
case 1: xLoc = 120; break;
case 2: xLoc = 205; break;
case 3: xLoc = 290; break;
case 4: xLoc = 375; break;
case 5: xLoc = 460; break;
case 6: xLoc = 545; break;
}
if (value == 1)
{
g.DrawImage(red, xLoc, yLoc, 65, 65);
}
else g.DrawImage(gold, xLoc, yLoc, 65, 65);
}
This method is located in Form 2 and calls the method above:
public void checkWhichToPaint()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
if (gridMap[i, j] != 0)
{
paintSprites(i, j, gridMap[i, j]);
}
}
}
}
and I am calling the method checkWhichToPaint() after creating a new instance of Form 2 and Showing it:
Tournament tournament = new Tournament();
tournament.LoadGame();
tournament.Show();
tournament.checkWhichToPaint();
where can I call this method such as it is painted after the grid_Paint is invoked?
Thanks for your help :)
This is the answer to whoever might need it!
In Form 1:
protected virtual void grid_Paint(object sender, PaintEventArgs e)
{
Image img = Image.FromFile(resourcesPath+ "grid.fw.png");
gridGraphics = grid.CreateGraphics();
gridGraphics.DrawImage(img, 0, 0, 650, 550);
}
In Form 2:
protected override void grid_Paint(object sender, PaintEventArgs e)
{
checkWhichToPaint();
base.grid_Paint(null, null);
}
Enjoy ! :)
Override the paint method in the second form and call base paint method before you run your form 2 code.
I'm kinda new to both Keys event and timers in my code but for some reason there is this weird bug. (read the last edit)
public partial class Form1 : Form
{
bool start = false;
Snake ormen = new Snake(10);
short direction = 3;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Hide();
timer1.Enabled = true;
start = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
if (start)
{
ormen.rita(g);
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
direction = 3;
break;
case Keys.Down:
direction = 1;
break;
case Keys.Left:
direction = 2;
break;
case Keys.Right:
direction = 0;
break;
default:
break;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
ormen.flytta(direction);
Invalidate();
}
}
}
I also tried to use debug at the key event but it does not even activate until after the time period. Can anyone please tell me why? Also if there are any obvious mistakes i would appreciate it if you could mention them. I have also tried to make the timer interval higher to check if the program can't keep up but it can. (read the last edit)
[EDIT] I don't think it will help but here is the code for the class "Snake". I doubt it's a problem in there but anyways.
private short diameter = 0;
//0 = Right, 1 = Down, 2 = Left, 3 = Up
public Snake(short diameter)
{
this.diameter = diameter;
ormensPlatser.Enqueue(new Point(0, 0));
ormensPlatser.Enqueue(new Point(0 + diameter, 0));
ormensPlatser.Enqueue(new Point(0 + diameter + diameter, 0));
}
Queue<Point> ormensPlatser = new Queue<Point>();
public void rita(Graphics g)
{
Point temp;
for (int i = 0; i < ormensPlatser.Count; i++)
{
temp = ormensPlatser.Dequeue();
g.FillEllipse(new SolidBrush(Color.Red), temp.X, temp.Y, diameter, diameter);
ormensPlatser.Enqueue(temp);
}
}
private void ormNäraKant(short direction,ref Point ormBit)
{
switch (direction)
{
case 0:
ormBit.X = ormBit.X + diameter >= 380 ? 0 : ormBit.X + diameter;
break;
case 1:
ormBit.Y = ormBit.Y + diameter >= 360 ? 0 : ormBit.Y + diameter;
break;
case 2:
ormBit.X = ormBit.X - diameter < 0 ? 370 : ormBit.X - diameter;
break;
case 3:
ormBit.Y = ormBit.Y - diameter < 0 ? 350 : ormBit.Y - diameter;
break;
}
}
public void flytta(short direction)
{
Point temp;
int temp1 = ormensPlatser.Count - 1;
for (int i = 0; i < ormensPlatser.Count; i++)
{
if (i == temp1)
{
switch(direction)
{
case 0:
temp = ormensPlatser.Dequeue();
ormNäraKant(0, ref temp);
ormensPlatser.Enqueue(temp);
break;
case 1:
temp = ormensPlatser.Dequeue();
ormNäraKant(1, ref temp);
ormensPlatser.Enqueue(temp);
break;
case 2:
temp = ormensPlatser.Dequeue();
ormNäraKant(2, ref temp);
ormensPlatser.Enqueue(temp);
break;
case 3:
temp = ormensPlatser.Dequeue();
ormNäraKant(3, ref temp);
ormensPlatser.Enqueue(temp);
break;
}
}
else
{
ormensPlatser.Dequeue();
temp = ormensPlatser.Peek();
ormensPlatser.Enqueue(temp);
}
}
}
}
}
[EDIT2] Okey, apparently it wasn't about the time or anything like that, the code worked but i think it has something to do with what slawekwin said in the comments about the focus when the program starts. So the problem is still the same but if I ALT+tab to a another program then back to the snake program it works.
SO i finally figure it out, i thought that the problem was with the timer event and the key input event when they worked together but my assumption was wrong. So what i did is that i went through all the properties that the form has and i found a setting called "KeyPrevied" and it was false so i tried to set it to true and it worked :).