C# 圣诞桌面飘雪小游戏

8 篇文章 0 订阅

源码下载:http://download.csdn.net/detail/llftc/3990138

根据论坛中的问题制作的小游戏,是圣诞节桌面下雪的小程序,喜欢的请下载。

要生产的雪花飘动,并随即生成多种大小的雪花,需要单独定义一个雪花类,

然在Timer事件中进行雪花移动的处理,代码如下:

private void timer_Tick(object sender, EventArgs e)
        {
            Tick++;

            if (Tick % 10 == 0)
            {
                SnowFlake s = new SnowFlake();
                Random rd = new Random();
                s.X = rand.Next(-20, this.Width + 20);
                s.Y = 0f;
                s.XVelocity = (float)(rand.NextDouble() - 0.5f) * 2f;
                s.YVelocity = (float)(rand.NextDouble() * 3) + 1f;
                s.Rotation = rand.Next(0, 359);
                s.RotVelocity = rand.Next(-3, 3) * 2;
                s.image = Image.FromFile(@"Resources\" + rd.Next(1, 4) + ".gif");
                if (s.RotVelocity == 0)
                {
                    s.RotVelocity = 3;
                }
                s.Scale = (float)(rand.NextDouble() / 2) + 0.75f;
                SnowFlakes.Add(s);
            }

            Graphics g = Graphics.FromImage(screenImage);
            g.Clear(Color.Transparent);
            g.SmoothingMode = SmoothingMode.HighSpeed;

            for (int i = 0; i < SnowFlakes.Count; i++)
            {
                SnowFlake s1 = SnowFlakes[i];
                s1.X += s1.XVelocity;
                s1.Y += s1.YVelocity;
                s1.Rotation += s1.RotVelocity;
                s1.XVelocity += ((float)rand.NextDouble() - 0.5f) * 0.7f;
                s1.XVelocity = Math.Max(s1.XVelocity, -2f);
                s1.XVelocity = Math.Min(s1.XVelocity, +2f);

                if (s1.Y > this.Height)
                {
                    SnowFlakes.RemoveAt(i);
                }
                else
                {

                    g.ResetTransform();
                    g.TranslateTransform(-16, -16, MatrixOrder.Append); //pan
                    g.ScaleTransform(s1.Scale, s1.Scale, MatrixOrder.Append); //scale
                    g.RotateTransform(s1.Rotation, MatrixOrder.Append); //rotate
                    g.TranslateTransform(s1.X, s1.Y, MatrixOrder.Append); //pan
                    g.DrawImage(s1.image, 0, 0); //draw
                }
            }
            g.Dispose();
            SetBackground(screenImage);
        }

如果直接获取windows桌面的句柄,并在上面画图的话,画出来的图很容易被刷新掉。

所以我使用下列方法,将一个大的Form涂层Top到桌面顶端。

try
            {
                Bitmap bitmap = (Bitmap)img;
                if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
                {
                    throw new ApplicationException();
                }
                IntPtr hObject = IntPtr.Zero;
                IntPtr zero = IntPtr.Zero;
                IntPtr hDC = Win32.GetDC(IntPtr.Zero);
                IntPtr ptr2 = Win32.CreateCompatibleDC(hDC);
                try
                {
                    hObject = bitmap.GetHbitmap(Color.FromArgb(0));
                    zero = Win32.SelectObject(ptr2, hObject);
                    Win32.Size size2 = new Win32.Size(bitmap.Width, bitmap.Height);
                    Win32.Size psize = size2;
                    Win32.Point point3 = new Win32.Point(0, 0);
                    Win32.Point pprSrc = point3;
                    point3 = new Win32.Point(base.Left, base.Top);
                    Win32.Point pptDst = point3;
                    Win32.BLENDFUNCTION pblend = new Win32.BLENDFUNCTION();
                    pblend.BlendOp = 0;
                    pblend.BlendFlags = 0;
                    pblend.SourceConstantAlpha = 0xff;
                    pblend.AlphaFormat = 1;
                    Win32.UpdateLayeredWindow(this.Handle, hDC, ref pptDst, ref psize, ptr2, ref pprSrc, 0, ref pblend, 2);
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    throw exception;
                }
                finally
                {
                    Win32.ReleaseDC(IntPtr.Zero, hDC);
                    if (hObject != IntPtr.Zero)
                    {
                        Win32.SelectObject(ptr2, zero);
                        Win32.DeleteObject(hObject);
                    }
                    Win32.DeleteDC(ptr2);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

附截图:



先写这些吧,源码我已经传到CSDN上,下载地址如下:

http://download.csdn.net/detail/llftc/3990138

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值