import javax.swing.JFrame;
import javax.swing.JPanel;
//整个世界
public class World extends JPanel{
private Sky sky = new Sky(); //天空对象
private Hero hero = new Hero(); //英雄机对象
private FlyingObject[] enemies = {}; //敌人(小敌机、大敌机、小蜜蜂)数组
private Bullet[] bullets = {}; //子弹数组
public void action(){ //测试代码
enemies = new FlyingObject[3];
enemies[0] = new Airplane();
enemies[1] = new BigAirplane();
enemies[2] = new Bee();
bullets = new Bullet[1];
bullets[0] = new Bullet(100,200);
}
public static void main(String[] args) {
JFrame frame = new JFrame(); //创建窗口对象
World world = new World(); //创建面板对象
frame.add(world); //将面板添加到窗口中
frame.setSize(400,700); //设置窗口的大小
frame.setLocationRelativeTo(null); //设置窗口居中显示
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭窗口时退出程序
frame.setVisible(true); //设置窗口可见
world.action(); //测试
}
}
-------------------------------------------------------------------------------
package cn.tedu.shoot;
import java.awt.image.BufferedImage;
/** 天空: 是飞行物 */
public class Sky extends FlyingObject{
private static BufferedImage image; //图片
static{
image = loadImage("background.png");
}
private int speed; //移动速度
private int y1; //y坐标(第2张图的)
/** 构造方法 */
public Sky(){
super(400,700,0,0);
speed = 1;
y1 = -height; //y1:负的高
}
/** 重写step() */
public void step(){
System.out.println("天空的y坐标和y1坐标移动了:"+speed);
}
}
------------------------------------------------------------------------------------------
package cn.tedu.shoot;
import java.util.Random;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/** 飞行物 */
public class FlyingObject {
protected int width; //宽
protected int height; //高
protected int x; //x坐标
protected int y; //y坐标
//专门给英雄机、子弹、天空提供的
public FlyingObject(int width,int height,int x,int y){
this.width = width;
this.height = height;
this.x = x;
this.y = y;
}
//专门给小敌机、大敌机、小蜜蜂提供的
public FlyingObject(int width,int height){
this.width = width;
this.height = height;
Random rand = new Random();
x = rand.nextInt(400-this.width); //x:0到(窗口宽-敌人宽)之内的随机数
y = -this.height; //y:负的敌人的高
}
//在构造方法的下面,行为公开化,
/** 读取图片 */
public static BufferedImage loadImage(String fileName){
try{
BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName)); //同包中读取fileName资源
return img;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException();
}
}
/** 飞行物移动 */
public void step(){
System.out.println("飞行物移动啦!");
}
}
-------------------------------------------------------------------------------------
package cn.tedu.shoot;
import java.awt.image.BufferedImage;
/** 英雄机: 是飞行物 */
public class Hero extends FlyingObject {
private static BufferedImage[] images; //图片数组
static{
images = new BufferedImage[6]; //6张图片
for(int i=0;i<images.length;i++){
images[i] = loadImage("hero"+i+".png");
}
}
private int life; //命
private int doubleFire; //火力值
/** 构造方法 */
public Hero(){
super(97,124,140,400);
life = 3;
doubleFire = 0;
}
/** 英雄机随着鼠标移动 x/y:鼠标的x坐标/y坐标 */
public void moveTo(int x,int y){
System.out.println("英雄机随着鼠标移动啦!");
}
/** 重写step() */
public void step(){
System.out.println("英雄机切换图片啦!");
}
}
--------------------------------------------------------------
package cn.tedu.shoot;
import java.awt.image.BufferedImage;
/** 小敌机: 是飞行物 */
public class Airplane extends FlyingObject {
private static BufferedImage[] images; //图片数组
static{
images = new BufferedImage[5]; //5张图片
for(int i=0;i<images.length;i++){
images[i] = loadImage("airplane"+i+".png");
}
}
private int speed; //移动速度
/** 构造方法 */
public Airplane(){
super(49,36);
this.speed = 2;
}
/** 重写step() */
public void step(){
System.out.println("小敌机的y坐标移动了:"+speed);
}
}
package cn.tedu.shoot;
import java.awt.image.BufferedImage;
/** 大敌机: 是飞行物 */
public class BigAirplane extends FlyingObject {
private static BufferedImage[] images;
static{
images = new BufferedImage[5]; //5张图片
for(int i=0;i<images.length;i++){
images[i] = loadImage("bigplane"+i+".png");
}
}
private int speed; //移动速度
/** 构造方法 */
public BigAirplane(){
super(69,99);
speed = 2;
}
/** 重写step() */
public void step(){
System.out.println("大敌机的y坐标移动了:"+speed);
}
}
package cn.tedu.shoot;
import java.util.Random;
import java.awt.image.BufferedImage;
/** 小蜜蜂: 是飞行物 */
public class Bee extends FlyingObject {
private static BufferedImage[] images;
static{
images = new BufferedImage[5]; //5张图片
for(int i=0;i<images.length;i++){
images[i] = loadImage("bee"+i+".png");
}
}
private int xSpeed; //x坐标的移动速度
private int ySpeed; //y坐标的移动速度
private int awardType; //奖励类型
/** 构造方法 */
public Bee(){
super(60,50);
xSpeed = 1;
ySpeed = 2;
Random rand = new Random();
awardType = rand.nextInt(2); //0到1之间的随机数
}
/** 重写step() */
public void step(){
System.out.println("小蜜蜂的x坐标移动了:"+xSpeed+",y坐标移动了:"+ySpeed);
}
}
package cn.tedu.shoot;
import java.awt.image.BufferedImage;
/** 子弹: 是飞行物 */
public class Bullet extends FlyingObject {
private static BufferedImage image; //图片
static{
image = loadImage("bullet.png");
}
private int speed; //移动速度
/** 构造方法 x/y:不是固定的 */
public Bullet(int x,int y){
super(8,14,x,y);
speed = 3;
}
public void step(){
System.out.println("子弹的y坐标移动了:"+speed);
}
}