[leJOS] 彈跳球

力維用leJOS改寫宗翰老師的NXC彈跳球

http://tw.myblog.yahoo.com/lego-caveschool/article?mid=4137&prev=4145&next=4103&l=f&fid=18

程式可直接複製或按我下載

=============================================

import lejos.nxt.*;
import lejos.util.Delay;
import javax.microedition.lcdui.Graphics;
class ball
{
 public static void main(String args[])
 {
  Button.ESCAPE.addButtonListener(new ButtonListener()
  {
   public void buttonPressed(Button b){System.exit(1);}
   public void buttonReleased(Button b){}
  });
  Graphics g = new Graphics();
     int ball_x = (int)(Math.random()*61)+20;
        int ball_y = (int)(Math.random()*31)+20;
        int del_x = 1;
        int del_y = 1;
  int r = 2;
  g.fillArc(ball_x,ball_y,r,r,0,360);
  while(true)
        {
         LCD.clear();
      ball_x = ball_x + del_x;
            ball_y = ball_y + del_y;
   g.fillArc(ball_x,ball_y,r,r,0,360);
   if( ball_x <= 2 || ball_x>=96)
   {
       del_x = del_x*-1;
                Sound.playTone(440,1);
         }
            if( ball_y <= 5 || ball_y >= 61)
            {
       del_y = del_y*-1;
                Sound.playTone(440,1);
   }
   Delay.msDelay(40);
  }
 }
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *