Animer une image et créer un effet de collision

Author:

Modifier,le,mode,de,composition,d'une,image
{filelink=16662}

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class ImageAnimation : System.Windows.Forms.Form
  {
    private int frequence = 6; // Régulateur de la vitesse de déplacement
    private System.Windows.Forms.Timer MonTimer; // Timer pour faire déplacer l'image
    private System.Windows.Forms.PictureBox MaBalle; // Image à déplacer
    private System.Windows.Forms.PictureBox imageCible; // Image de collision

   // Changer l'image du Frame lorsqu'une collision se produire entre 2 images 

    public ImageAnimation()
    {
        InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.imageCible = new System.Windows.Forms.PictureBox();
      this.MaBalle = new System.Windows.Forms.PictureBox();
      this.MonTimer = new System.Windows.Forms.Timer(new System.ComponentModel.Container());
      this.SuspendLayout();

      this.imageCible.BackColor = Color.Black;
      this.imageCible.Location = new System.Drawing.Point(this.Width/2, this.Width/2);
      this.imageCible.Name = "imageCible";
      this.imageCible.Size = new System.Drawing.Size(56, 56);
      this.imageCible.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
      this.imageCible.TabIndex = 0;
      this.imageCible.TabStop = false;

      this.MaBalle.Image = new Bitmap("half.png");
      this.MaBalle.Location = new System.Drawing.Point(this.Width / 2, this.Width / 2);
      this.MaBalle.Name = "MaBalle";
      this.MaBalle.Size = new System.Drawing.Size(45, 45);
      this.MaBalle.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
      this.MaBalle.TabIndex = 1;
      this.MaBalle.TabStop = false;

      this.MonTimer.Enabled = true;
      this.MonTimer.Tick += new System.EventHandler(this.timer1_Tick);

      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.BackColor = System.Drawing.Color.White;
      this.ClientSize = new System.Drawing.Size(392, 341);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.MaBalle,
                                                                  this.imageCible});
      this.Name = "ImageAnimation";
      this.Text = "Exemple Effet de Collision";
      this.ResumeLayout(false);

    }
    private void timer1_Tick(object sender, System.EventArgs e)
    {
      int coteX, coteY;
      coteX = MaBalle.Location.X + frequence;
      coteY = MaBalle.Location.Y + frequence;

      if (coteX > this.Width - MaBalle.Width)
      {
        frequence = - frequence;
       } 

      if (coteX < 0)
      {
        frequence = - frequence;

      }

      if (MaBalle.Bounds.IntersectsWith(imageCible.Bounds)){
        this.BackColor = Color.Black;
      } else {
        this.BackColor = Color.White;
      }

      MaBalle.Location = new Point(coteX, coteY);
    }  

    [STAThread]
    static void Main()
    {
      Application.Run(new ImageAnimation());
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *