?? .#merg12072cvs
字號:
/* * Bird.java * * Created on 2007年12月19日, 上午11:41 * * To change this template, choose Tools | Template Manager * and open the template in the editor. *實現了鳥類的移動(在move方法實現) *當鳥類飛出界面或者被擊中后掉落到屏幕外,調用reset方法重新刷出一只鳥 *draw函數重載了父類里面的draw方法,實現了圖像的刷新 */package graphics;import SBGameCore.*;import java.awt.Point;import java.util.Random;import java.awt.Graphics;/** * * @author Saerdna */public class Bird extends Comp{ int[] randomYSpeed = new int[]{2,-1,0,-2,1};//{0,0,0,0,0};//Y方向的速度數組 int[] randomXSpeed = new int[]{2,5,9};//{0,0,0};//X方向的速度數組 public static boolean[] pathUsed = new boolean[]{false,false,false,false,false,false,false}; int[] randomPosition = new int[]{60,150,240,330,420,510,600};//出現的路徑的數組,把Y軸分成7個區域 int num;//保存鳥類圖片的序號 int pathID;//保存選擇的路徑的序號 /** * Creates a new instance of Bird */ public Bird(GraphicsPanel a) { this.a = a; w = 100;//圖片的寬度 h = 100;//圖片的高度 Random rbird = new Random();//創建一個隨機數對象 int b = 0; //do{ b = rbird.nextInt(7);//0~6這個整數數組中隨機抽取一個數 img = a.birds[b];//按照這個隨機數選擇一只鳥 num = b;//保存鳥的序號 if(b == 1 || b == 3){//如果是1號或者3號鳥,讓他從左邊飛出 do{ pathID = rbird.nextInt(7);//0~6這個整數數組中隨機抽取一個數 }while(pathUsed[pathID]); px = opx = 0; py = opy = randomPosition[pathID];//按照這個隨機數選擇一條路徑 pathUsed[pathID] = true; face = 1;//代表從左向右飛 }else{//如果是其他編號的鳥,讓它從右邊飛出 do{ pathID = rbird.nextInt(7); }while(pathUsed[pathID]); px = opx = 1000; py = opy = randomPosition[pathID]; pathUsed[pathID] = true; face = -1;//代表從右向左飛 } Random rndv = new Random(); int x = 0; x = rndv.nextInt(3); vx = randomXSpeed[x];//按照身成的隨機數,從x方向速度的數組中抽取一個飛行速度 active = true;//鳥是活鳥 //k = (k+1)%7; /* Random rndv = new Random(); int x = 0; x = rndv.nextInt(3); vx = randomv[x]; try {Thread.sleep(100);} catch(Exception e) {}; int y = rndv.nextInt(3); vy = randomv[y];*/ } public void move() { if(active){//如果是活鳥,就按照下述方法飛行 opx = px; opy = py; px = px + vx * face;//vx*face,判別x的方向 Random rnd = new Random(); int x = 0; x = rnd.nextInt(5); py = randomYSpeed[x] + py;//按照隨機生成的隨機數,決定下一步y軸的飛行方向 if(face == 1&&px == 1100)//如果左邊的鳥飛出右邊邊界 px=0; else if(face == -1 && px == -100)//如果右邊的鳥飛出左邊邊界 px = 1000; }else{ die();//如果是死鳥,就按照die方法進行飛行 } } public void draw(){ set_draw_rectangles(a.paint_area,a.new_area); Graphics bg = a.buffer.getGraphics(); bg.clipRect(a.paint_area.x,a.paint_area.y,w,h); bg.drawImage(a.backdrop,0,0,a); bg.dispose(); bg = a.buffer.getGraphics(); bg.clipRect(a.new_area.x,a.new_area.y,w,h); bg.drawImage(img,a.new_area.x,a.new_area.y,a); bg.dispose(); a.paint_area.add(a.new_area); Graphics g = a.getGraphics(); g.clipRect(a.paint_area.x,a.paint_area.y,a.paint_area.width,a.paint_area.height); g.drawImage(a.buffer,0,0,a); g.dispose(); } public Point getPoint(){//得到鳥的當前所在的坐標 return new Point(px,py); } public void reset(){//重置鳥類的飛行屬性,類似構造方法中的操作 active = true;//讓死鳥變活,以下同構造函數中的描述 Random rbird = new Random(); int b = 0; b = rbird.nextInt(7); num = b; img = a.birds[b]; if(num == 1 || num == 3){ px = opx = 0; py = opy = randomPosition[pathID]; face = 1; }else{ px = opx = 1000; py = opy = randomPosition[pathID]; face = -1; } Random rndv = new Random(); int x = 0; x = rndv.nextInt(3); vx = randomXSpeed[x]; } public void die(){//死鳥的飛行方法 opx = px; opy = py; py = py - 10;//讓他垂直下落 if(py < -100 ) reset();//當掉出屏幕外,重置 } public void changeToDeadImage(){//當活鳥被擊中后,更換圖片 img = a.deadbirds[num]; } public int getScore(){//返回得分 return vx*2; } public int getFace(){//返回鳥類飛行方向 return face; } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -