C#实现气泡

来源:互联网 发布:vb小写数字转大写金额 编辑:程序博客网 时间:2024/04/30 04:34

本节主要讲解气泡,代码如下

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;//导入的命名空间using System.Drawing.Drawing2D;namespace 气泡{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            //设置窗体的属性,注释部分为可省略部分            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;           //   this.BackColor = Color.Red;           // this.Opacity = 0.7;            this.Width = this.Height = 100;         //    this.Location = new Point(0,0);            //使之呈现圆形,此处需要导入命名空间System.Drawing.Drawing2D;            GraphicsPath Gp = new GraphicsPath();            Gp.AddEllipse(0,0,100,100);            this.Region = new System.Drawing.Region(Gp);           //控制气泡的移动的Timer函数,可在编辑界面直接拖入,也可在后台定义            timer1.Start();            //多个气泡只要实例化更多的窗体就行,代码和这个窗体的一样           // Form2 form = new Form2();          //  form.Show();        }        //定义x,y为X轴,Y轴方向速度        int x=10, y=10 ;        private void timer1_Tick(object sender, EventArgs e)        {            //如果碰到屏幕的上面或者下面,那么y轴方向速度变为相反数,x轴方向速度不变,反之亦然            if (this.Bottom>=Screen.PrimaryScreen.WorkingArea.Height||this.Top<0)            {                y = -y;            }            if (this.Left<0||this.Left+this.Width>=Screen.PrimaryScreen.WorkingArea.Width)            {                //此处通过绝对路径导入图片,但最好是相对路径,可确保移动文件时能找到相应的地址                this.BackgroundImage = Image.FromFile(@"G:\练习\气泡1\气泡1\Image\1e70f0ab227242acacee12c82645b44a_sihuosheng-001.jpg");                x = -x;            }            this.Left += x;            this.Top += y;        }    }}

运行结果
通过气泡游戏,可以学到关于窗体的一些属性的设置,Timer空间的使用,以及C#的一些基础的语法
多个气泡相撞的判断,等价于圆心距离的判断

0 0
原创粉丝点击