hi all .. just want to ask if anyone have an idea how can i move image down throug keyboard cursors up an down .. i know that there is the keydown event and the keyup event but really i don't know how to write the code .. other qusetion how can i show images randomly with timer ?

thanks

Recommended Answers

All 13 Replies

that depends on how you are drawing your images. If you have them in a pictuerbox then you need to move the picturebox. If you are painting them to a control then you need to adjust the x,y values of the point where you draw the image.

As for drawing a random image on timer, you need to look into random number generator, give each image a number, then on the timer tick event, generate a random number and paint the relevant image.

yes .. there is no picture box just the picture .. i know if i want to let the picture goes down i should increament the y .. and up decrease the x .. but what is the condition ?

I already gave you an example snippet in a previous thread.
Perhaps it was to complicated I don't know. Did you study it?
Give you a new simpler example here:
Create a new windowsformsapp and drop a Panel on it, then fill in this code in the Forms1.cs file.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int cTimerInterval = 25;  // millisecs
        const int cBallSize = 16;

        Bitmap bm;
        Point pt;
        Timer clock;

        public Form1()
        {
            InitializeComponent();            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // start a timer and install the tick event
            clock = new Timer();
            clock.Interval = cTimerInterval;
            clock.Tick += new EventHandler(clock_Tick);
            clock.Start();
            pt.X = 0;
            pt.Y = 0;
            // create bitmap or get one from a file or resourcefile
            bm = new Bitmap(cBallSize, cBallSize);
            Graphics gr = Graphics.FromImage(bm);
            gr.Clear(BackColor);
            gr.FillEllipse(Brushes.Red, pt.X, pt.Y, cBallSize, cBallSize);
            gr.Dispose();     
        }

        void clock_Tick(object sender, EventArgs e)
        {             
            // we come here every 25 millisecs
            pt.X += 1;
            pt.Y += 1; //increment position
            // for simplicity we generate an update of the whole window
            // this causes the paint event of the panel to happen
            this.Refresh();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            //draw the bitmap at position pt
            e.Graphics.DrawImage(bm, pt); 
        }      
    }
}

Generate the form load event by double clicking the form in design mode, the panel paint event is also generated by double clicking.
This will only work if you have an uniform background.

private void tick_method(object s,EventArgs  e)
        {
            int [] pic = new int [1];
            pic[0] = int.Parse ("C:\\Documents and Settings\\maher\\Desktop\\WindowsApplication8\\straw .jpg");
            pic[1] = int.Parse("C:\\Documents and Settings\\maher\\Desktop\\WindowsApplication8\\apple.jpg");
            Random rand = new Random();
            rand.Next(0, 1);
            Invalidate();
      //  this piece of code i want two picture to display randomly is this correct ?
        
        }

No this is far from correct, but don't panic.
int [] pic = new int [1];
This means create an array named pic with 1 element. You can call this with pic[0] (arrays start at index 0) pic[1] would give an index out of range exception.
You cannot use int.Parse on a jpg file(would also give some exception)
I would advise you to use my example.
On line 51, instead of bm, fill in one of your file paths and see what happens.

ok thanks alot and a i really appreciate that you want to help me .. but really that isn't the thing that i wanted .. lsn i have 3 picture .. 2 of them should raise randomly ... and the remained picture should just move up and down using keybored cursors .. it's just like a game ..

Of course my example is not what you want, but did you try it? Did you experiment with different values for the Point variable?
Do this in lines 41 and 42. You can use a random method here to put your images anywere on the screen.
Change this Point variable in a KeyDown event for your arrow buttons etc. Experiment! See what happens, you will learn from it believe me.

are the 2 pictures that appear randomly going to be in the same place every time they appear? if so you could just place pictureboxes with the images in them and hide/show them based on a random number generator.

Also, cant stress ddanbe's point enough. It is so important to experiment with code. Its the best way to learn how it works. Figure out what its doing and try to work out what you need to change to make it do what you need.

commented: Glad you agree:) +5

no the 2 pictures should appears randomly each 1 second and then the they move to the left

You seem to constantly ignore the advise we give you.
Did or did you not try out the code I gave you?
Do you expect we give you the code you want to have?
I don't have to type this in, I don't have to do this at all.
I do this because I'm a learner myself (learned alot here already) and I like, if possible, that others can benefit from my knowlege.
So, please show some effort, try something for yourself and if does not work, show us your code. Please?:icon_smile:

public partial class Form1 : Form
    {
        List<Apples> app;
        List<strawbery> straw;
        const int cTimerInterval = 90;

        Point p = new Point(0, 90);

        Random rand = new Random();
        public Form1()
        {
            InitializeComponent();
        }



        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
              p.Y = p.Y + 8;
               Invalidate();  


        }

       // private void collision ()
       // {


       // }


        private void tick_method(object s, EventArgs e)
        {


        }

        private void Form1_Load(object sender, EventArgs e)
        {





        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Image img = Image.FromFile("C:/Documents and Settings/maher/Desktop/WindowsApplication8/straw berry.jpg");
            g.DrawImage(img, p);
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (p.Y == panel3.Location.Y + panel3.Height)
            {

                p.Y = p.Y + 8;
            }
            if (e.KeyCode == Keys.Up)
               p.Y = p.Y - 8 ;

            Invalidate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int timeleft = int.Parse(label4.Text);
            timeleft--;
            label4.Text = timeleft.ToString();

        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }


    }
}

this is my work .. i just answered his question ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.