用C#编写物体碰撞的程序

时间:2021-05-02

本文讲解了使用C#编写物体碰撞的一个源代码,本实例来自一个老外的网站,从源代码就可以看出,编写的很专业,大家可以学一学。

usingSystem;usingSystem.Drawing;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Windows.Forms;usingSystem.Data;publicclassForm1:System.Windows.Forms.Form{privateintdx=4;privateSystem.Windows.Forms.PictureBoxpicTarget;privateSystem.Windows.Forms.PictureBoxpicBall;privateSystem.Windows.Forms.Timertimer1;publicForm1(){InitializeComponent();}privatevoidInitializeComponent(){this.picTarget=newSystem.Windows.Forms.PictureBox();this.picBall=newSystem.Windows.Forms.PictureBox();this.timer1=newSystem.Windows.Forms.Timer(newSystem.ComponentModel.Container());this.SuspendLayout();this.picTarget.BackColor=Color.Red;this.picTarget.Location=newSystem.Drawing.Point(160,240);this.picTarget.Name="picTarget";this.picTarget.Size=newSystem.Drawing.Size(56,56);this.picTarget.SizeMode=System.Windows.Forms.PictureBoxSizeMode.StretchImage;this.picTarget.TabIndex=0;this.picTarget.TabStop=false;this.picBall.Image=newBitmap("winter.jpg");this.picBall.Location=newSystem.Drawing.Point(24,136);this.picBall.Name="picBall";this.picBall.Size=newSystem.Drawing.Size(32,32);this.picBall.SizeMode=System.Windows.Forms.PictureBoxSizeMode.StretchImage;this.picBall.TabIndex=1;this.picBall.TabStop=false;this.timer1.Enabled=true;this.timer1.Tick+=newSystem.EventHandler(this.timer1_Tick);this.AutoScaleBaseSize=newSystem.Drawing.Size(5,13);this.BackColor=System.Drawing.Color.White;this.ClientSize=newSystem.Drawing.Size(392,341);this.Controls.AddRange(newSystem.Windows.Forms.Control[]{this.picBall,this.picTarget});this.Name="Form1";this.Text="Crasher";this.ResumeLayout(false);}[STAThread]staticvoidMain(){Application.Run(newForm1());}privatevoidtimer1_Tick(objectsender,System.EventArgse){intnewX,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=newPoint(newX,newY);}}

本文源自:翔宇亭——IT乐园(http://),转载请保留此信息!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章