Object collision : Animation « 2D Graphics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » 2D Graphics » AnimationScreenshots 
Object collision
Object collision


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

public class Form1 : System.Windows.Forms.Form {
    private int dx = 4;
    private System.Windows.Forms.PictureBox picTarget;
    private System.Windows.Forms.PictureBox picBall;
    private System.Windows.Forms.Timer timer1;

    public Form1() {
        InitializeComponent();
    }
    private void InitializeComponent() {
      this.picTarget = new System.Windows.Forms.PictureBox();
      this.picBall = new System.Windows.Forms.PictureBox();
      this.timer1 = new System.Windows.Forms.Timer(new System.ComponentModel.Container());
      this.SuspendLayout();

      this.picTarget.BackColor = Color.Red;
      this.picTarget.Location = new System.Drawing.Point(160240);
      this.picTarget.Name = "picTarget";
      this.picTarget.Size = new System.Drawing.Size(5656);
      this.picTarget.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
      this.picTarget.TabIndex = 0;
      this.picTarget.TabStop = false;

      this.picBall.Image = new Bitmap("winter.jpg");
      this.picBall.Location = new System.Drawing.Point(24136);
      this.picBall.Name = "picBall";
      this.picBall.Size = new System.Drawing.Size(3232);
      this.picBall.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
      this.picBall.TabIndex = 1;
      this.picBall.TabStop = false;

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

      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.BackColor = System.Drawing.Color.White;
      this.ClientSize = new System.Drawing.Size(392341);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.picBall,
                                                                  this.picTarget});
      this.Name = "Form1";
      this.Text = "Crasher";
      this.ResumeLayout(false);

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

    private void timer1_Tick(object sender, System.EventArgs e) {
      int newX, newY;
      newX = picBall.Location.X + dx;
      newY = picBall.Location.Y + dx;
      
      if (newX > this.Width - picBall.Width){
        dx = - dx;
      
      
      if (newX < 0){
        dx = - dx;
      }
      
      if (picBall.Bounds.IntersectsWith(picTarget.Bounds)){
        this.BackColor = Color.Black;
      else {
        this.BackColor = Color.White;
      }
      
      picBall.Location = new Point(newX, newY);
    }
}


           
       
Related examples in the same category
1.Button click action to move a ballButton click action to move a ball
2.Animates a circleAnimates a circle
3.Uses a thread to Animate a ballUses a thread to Animate a ball
4.Animates an imageAnimates an image
5.Animation and double bufferAnimation and double buffer
6.Timer based animationTimer based animation
7.No Flicker (Flicker Free) AnimationNo Flicker (Flicker Free) Animation
8.Animate ImageAnimate Image
9.Animate DemoAnimate Demo
10.Animate Image with ImageAnimator
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.