J2ME小特效之閃電中的城市

來源:互聯網
上載者:User

黑夜降臨 烏雲滿天 閃電橫空出現

GAMELOFT 世界大戰 遊戲中也出現過怪物 閃電攻擊

現在我們就來寫個閃電小特效 大家稍加應用就可以 變換不同效果

 


代碼如下

class DrawCanvas extends Canvas implements Runnable{
 
 
private Image img;
 /** 建構函式 */
 private int[] light;
 boolean kaishi=true;
 Random sj=new Random();
 public DrawCanvas (){
  try {
   img = Image.createImage("/sd.png");
  }catch(java.io.IOException e) {}
  new Thread(this).start();
                 }
  
                
  protected void paint(Graphics g){
   g.setColor(0x00000000);
   g.fillRect(0,0,getWidth(),getHeight());
   creatThunder();
   g.setColor(0x00ffffff);//白色閃電
 
   g.drawString(String.valueOf(light[0]),0,0,0);
   g.drawImage(img,0,0,0);//畫城市
   //畫閃電
   for(int j=0;j<light.length-1;j++){
    
   g.drawLine(light[j],5+j,light[j+1],6+j);
                                     
                                     }
   
 }
 void creatThunder(){
   int len=Math.abs(sj.nextInt())%20+60;//閃電長度可以自己控制
   light =new int[len];
   light[0]=Math.abs(sj.nextInt())%getWidth();//X座標這裡你可以控制閃電的X座標在你需要的範圍內
   //創造閃電數組
   for(int i=1;i<len;i++){
     light[i]=light[i-1]+((sj.nextInt()>0)?2:-2);
    }
    }
   public void run(){
    while(kaishi){
     try {
    repaint();    //再描繪
    Thread.sleep(500);  //等待一定時間
        
    }
   catch(InterruptedException e){}
    }
   }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.