C#版下雪

看到有人用其他 语言写了一个下雪的程序  用as3实现下雪效果 ,就想到自己都很久都没用winform写程序了,然后就自己动手写了一个。

我先截个图展示一下:



http://hi.csdn.net/attachment/201107/24/0_1311472567aWCr.gif
下面我贴出我的代码:
  1. 1using System;
  2. 2using System.Drawing;
  3. 3using System.Windows.Forms;
  4. 4
  5. 5namespace snow
  6. 6{
  7. 7    public partial class Form1 : Form
  8. 8    {
  9. 9        public Form1()
  10. 10        {
  11. 11            InitializeComponent();
  12. 12        }
  13. 13        private int[,] snowArray =new int [800,3];
  14. 14        private int snowNum;
  15. 15        Random rand = new Random();
  16. 16        //int [][]a =new int[1][1];
  17. 17        private void Form1_Load(object sender, EventArgs e)
  18. 18        {
  19. 19            
  20. 20            snowNum =rand.Next (800);
  21. 21            int x = rand.Next(800);
  22. 22            int y = rand.Next(600);
  23. 23            for (int i = 0; i < snowNum/10;i++ )
  24. 24            {
  25. 25                snowArray[i, 0] = x;
  26. 26                snowArray[i, 1] = y;
  27. 27                snowArray [i,2]= rand.Next (40);
  28. 28                x = rand.Next(800);
  29. 29                y = rand.Next(600);
  30. 30            }
  31. 31            
  32. 32        }
  33. 33        
  34. 34        private void Paintsnow(object sender, PaintEventArgs e)
  35. 35        {
  36. 36            Graphics paint = e.Graphics;
  37. 37            Bitmap bm = new Bitmap(10, 10);
  38. 38            bm.SetPixel(0, 0, Color.White);
  39. 39            bm.SetPixel(0, 1, Color.White);
  40. 40            bm.SetPixel(1, 0, Color.White);
  41. 41            bm.SetPixel(1, 1, Color.White);
  42. 42
  43. 43            
  44. 44            for (int i = 0; i < snowNum/10; i++)
  45. 45            {
  46. 46                paint.DrawImageUnscaled(bm, snowArray[i, 0], snowArray[i, 1]);
  47. 47            }
  48. 48        }
  49. 49
  50. 50        private void timer1_Tick(object sender, EventArgs e)
  51. 51        {
  52. 52            
  53. 53            for (int i = 0; i < snowNum/10; i++)
  54. 54            {
  55. 55                  if (snowArray [i, 1] > 600)
  56. 56                {
  57. 57                    snowArray[i, 1] = 0;
  58. 58                }
  59. 59                snowArray[i, 1] += snowArray[i, 2];
  60. 60            
  61. 61
  62. 62            }
  63. 63            this.Invalidate();
  64. 64            this.FindForm().Paint+=new PaintEventHandler (Paintsnow);
  65. 65        }
  66. 66    }
  67. 67}
  68. 68
复制代码
有兴趣的人还可以增加效果。。。

源码下载:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值