⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smoke.java

📁 JAVA2游戏程序设计源码 可以帮助初学者或者只想改改程序就能用的人
💻 JAVA
字号:
// 程序:烟粒子
// 范例文件:Smoke.java

import java.applet.*;
import java.awt.*;

public class Smoke extends Applet implements Runnable
{
   final int Max = 1000;
   sparticle p[]; 
   int AppletWidth,AppletHeight,XCenter,YCenter;
   Image        OffScreen;
   Graphics     drawOffScreen;
   Thread pThread;

   public void init()
   {
      setBackground(Color.black); 

      AppletWidth = getSize().width;
      AppletHeight = getSize().height;

      p = new sparticle[Max];
      for(int i=0; i<Max; i++)
         p[i] = new sparticle(AppletWidth, AppletHeight);
      
      OffScreen     = createImage(AppletWidth,AppletHeight);
      drawOffScreen = OffScreen.getGraphics();
   }

   public void start()
   {
      pThread = new Thread(this);
      pThread.start();
   }

   public void stop()
   {
      pThread = null;
   }
 
   public void update(Graphics g)
   {
       paint(g);
   }

   public void paint(Graphics g)
   {
      g.drawImage(OffScreen,0,0,this);
   }

   public void run()
   {
      int i, speriod = 10, wperiod = 0, c = 255;
      double Wx = 0.0; 


      while(true)
      {
         drawOffScreen.clearRect(0,0,AppletWidth,AppletHeight);

         for(i=0; i < Max; i++)
         {
            drawOffScreen.setColor(new Color(p[i].color, p[i].color, p[i].color));
            drawOffScreen.fillOval((int)p[i].X,(int)p[i].Y,3,3);
            p[i].X += p[i].Vx + Wx; 
            p[i].Y -= p[i].Vy;

            if(p[i].Y < AppletHeight/2)
               p[i].color -= 10; 
 
            if(p[i].color < 0) 
               p[i].reset(AppletWidth,AppletHeight);
          }

         if(wperiod > 0)
            wperiod--;
         else if(wperiod == 0)
         {
            speriod--;
            Wx =0;
            if(speriod == 0)
            {
               Wx = Math.random()*3 - Math.random()*3; 
               wperiod = (int)(Math.random()*10+1); 
               speriod = (int)(Math.random()*10+1); 
            }
         }
         
         repaint();
 
         try {
             Thread.sleep(200);
         }
         catch (InterruptedException e) { }
      }
   }
    
   public void wind()
   {
      
   }
}

class sparticle
{
   double X,Y;      
   double Vx,Vy;   
   int color;    


   public sparticle(int w, int h)
   {
      reset(w, h);
   }


   public void reset(int w, int h)
   {
      X = w / 2;
      Y = h;
      Vx = Math.random()-Math.random();
      Vy = Math.random() * 3 + 1;
      color = 255;
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -