2017-07-15 25 views
-1

我試圖在c#窗體中創建一個紫色的雨,就像這個視頻中的一個一樣https://www.youtube.com/watch?v=KkyIDI6rQJI Hes使用一個叫java的編程語言處理ide。C中的紫雨#

這裏是到目前爲止我的代碼:

using System; 
using System.Diagnostics; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Windows.Forms; 

namespace PurpleRain 
{ 

public partial class MainForm : Form 
{ 
    public MainForm() 
    { 

    } 
    float x=150; 
    float y=1; 
    float yspeed=1; 

    public void fall() 
    { 
     y=y+yspeed; 
     if (y>=350) 
     { 
      y=0; 
     } 
    } 
    public void show(float a,float b) 
    { 
     Graphics g; 
     g = this.CreateGraphics(); 
     Pen myPen = new Pen(Color.MediumPurple); 
     myPen.Width = 2; 
     Pen myErase = new Pen(Color.Lavender); 
     myErase.Width = 2; 
     g.DrawLine(myErase, a, b-1, a, b+15); 
     g.DrawLine(myPen, a, b, a, b+15); 
    } 

    void draw() 
    { 
     for (int i=0;i<10;i++){ 
      show(x,y); 
      fall(); 
     } 
    } 

    void Timer1Tick(object sender, EventArgs e) 
    { 
     draw(); 
    } 
} 

這段代碼的作用是繪製一個紫色線並使其通過擦除以前繪製的線落到底部。我的問題是增加這條紫色線可能是一百個模擬像視頻中的雨,並讓他們從隨機的x和y位置開始。我試過循環,列表無濟於事。

+1

您應該封裝位置的長度和速度的自定義類。爲這個類提供繪圖功能。創建這些對象的列表,填寫數據並開始繪製:-) – Stefan

+0

他沒有使用Java。處理是它自己的編程語言,它基於Java。有人可能會說,它比語言更接近框架或高級抽象庫,但它肯定不是IDE。 – stybl

+0

正如@Stefan所說,你的Drop類在哪裏?它在視頻中顯示如何使用它,爲什麼不實施它? – oerkelens

回答

0

不是最好的代碼,但它可能是更好的東西一個良好的開端:

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     List<Drop> rain = new List<Drop>(); // keeps all drops in one place 
     Random rnd = new Random();   // for generating random numbers 


     public Form1() 
     { 
      InitializeComponent(); 

      for (int i = 0; i < 100; i++) // creates 100 drops at random position and with random speed 
       rain.Add (CreateRandomDrop()); 
     } 


     private Drop CreateRandomDrop() 
     { 
      return new Drop // create drop with random position and speed 
      { 
       Position = new PointF (rnd.Next (this.Width), rnd.Next (this.Height)), 
       YSpeed = (float) rnd.NextDouble() * 3 + 2 // 2..5 
      }; 
     } 


     private void UpdateRain() // changes Y position for each drop (falling), also checks if a drop is outside Main form, if yes, resets position to 0 
     { 
      foreach (var drop in rain) 
      { 
       drop.Fall(); 

       if (drop.Position.Y > this.Height) 
        drop.Position.Y = 0; 
      } 
     } 


     private void RenderRain() 
     { 
      using (var grp = this.CreateGraphics()) // using will call IDisposable.Dispose 
      { 
       grp.Clear (Color.DarkBlue); 

       foreach (var drop in rain) 
        grp.DrawLine (Pens.White, drop.Position, new PointF (drop.Position.X, drop.Position.Y + 3)); 
      } 
     } 


     private void timer1_Tick (object sender, EventArgs e) 
     { 
      UpdateRain(); 
      RenderRain(); 
     } 
    } 


    class Drop 
    { 
     public PointF Position; 
     public float YSpeed; 

     public void Fall() => Position.Y += YSpeed; 
    } 
} 
+0

謝謝! @apocalypse。我設法讓它工作。對於剛接觸c#和麪向對象編程的人來說,這有很多幫助。 :) –