📄 .#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 + -