飞机大战-java基础小程序(初学项目)05

Shoot射击游戏第五天:

1.给类中成员添加访问控制修饰符

2.设计Images图片工具类

知识点:

1.package:

1)作用:避免类名的冲突

2)类的全称:包名.类名,同包中的类不能命名冲突

3)包名常常有层次结构 4)建议:包名所有字母都小写

import:

1)同包中的类可以直接访问 不同包中的类不能直接访问,若想访问:

1.1)先import声明类再访问类-----建议

1.2)类的全称-------------------太繁琐,不建议

2.访问控制修饰符:-------------保护数据的安全

1)public:公开的,任何类

2)private:私有的,本类

3)protected:受保护的,本类、派生类、同包类

4)默认的:什么也不写,本类、同包类------------java不建议默认权限

说明: 1)类的访问权限只能是public或默认的

          2)类中成员的访问权限如上4种都可以

3.final:最终的、不可改变的-----单独应用率极低

1)修饰变量:变量不能被改变

2)修饰方法:方法不能被重写

3)修饰类:类不能被继承

4.static:静态的

1)静态变量:

1.1)由static修饰

1.2)属于类,存储在方法区中,只有一份

1.3)常常通过类名点来访问

1.4)何时用:所有对象所共享的数据(图片、音频、视频等)

2)静态方法:

2.1)由static修饰

2.2)属于类,存储在方法区中,只有一份

2.3)常常通过类名点来访问

2.4)静态方法中没有隐式的this传递,所以不能直接访问实例成员

2.5)何时用:方法的操作与对象无关

3)静态块:

3.1)由static修饰

3.2)属于类,在类被加载期间自动执行, 因类只被加载一次,所以静态块也只执行一次

3.3)何时用:加载/初始化静态资源(图片、音频、视频等)

 

//解释一下就像我们完吃鸡不可能所以人都给一个图,那样服务器早就爆炸了,只能是这个范围里面所有人用一张图

下面我们先创建一个类Images

 

 

 

 

 

 

 

 

 

 

 

 

代码:

package cn;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.image.BufferedImage;

public class World extends JPanel {

	private Hero hero = new Hero();
	private Sky sky = new Sky();
	private FlyingObject[] enemies;// 0

	private Bullet[] bullet = {};

	public void action() {
		BufferedImage img = Images.sky;
		enemies = new FlyingObject[5];
		enemies[0] = new Airplane();
		enemies[1] = new Airplane();
		enemies[2] = new BigAirplane();
		enemies[3] = new BigAirplane();
		enemies[4] = new Bee();
		for (int x = 0; x < enemies.length; x++) {
			FlyingObject f = enemies[x];
			System.out.println(f.x + "," + f.y);
			f.say();
		}
	}


	public static void main(String[] args) {

		JFrame frame = new JFrame();
		World world = new World();
		frame.add(world);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 700);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);

		world.action();

	}

}
package cn;

import java.util.Random;


public class Airplane extends FlyingObject {
	
	private  int speed;//移动速度
	public Airplane(){
		super(48,50);//这里给大家说一下这个数是我随便填的用于测试,但是到最后程序的时候不要随便填,仿照我的来写
		
		
		speed=2;
	}
	public void say(){
		System.out.println("我愿成为你的守护天使");
	}
	
	

}

package cn;

import java.util.Random;



public class Bee extends FlyingObject{
	
	private int xspeed;//x移动速度
	private int yspeed;//y移动速度
	private int awardType;//奖励类型
	public Bee(){
		super(20,50);
		
		Random rand=new Random();
		
		xspeed=1;
		yspeed=2;
		awardType=rand.nextInt(8);
	}
	public void say(){
		System.out.println("我愿成为你的守护天使");
	}
	
	}
package cn;
import java.util.Random;



public class BigAirplane extends FlyingObject{
	
	private  int speed;//移动速度
	public BigAirplane(){
		super(66,89);
	
		
		speed=2;
	}
	
	public void say(){
		System.out.println("我愿成为你的守护天使");
	}

}
package cn;



public class Bullet  extends FlyingObject{
	//子弹

	private int speed;//移动速度
	public Bullet(int x,int y){
		super(8,20,x,y);
		
		
		speed=3;
		
	}
	public void say(){
		System.out.println("我愿成为你的守护天使");
	}
	

}
package cn;

import java.util.Random;

public class FlyingObject {
	protected int width;
	protected int height;
	protected int x;
	protected int y;

	public FlyingObject(int width, int height) {// 专门给小蜜蜂,大敌机,小敌机提供的,以为三种飞行器的x,y都是不同的,所以要写活
		this.width = width;
		this.height = height;
		Random rand = new Random();
		x = rand.nextInt(400 - width);
		y = -height;
	}

	public FlyingObject(int width, int height, int x, int y) {// 专门给英雄机,天空子弹提供的,以为三种飞行器的x,y都是不同的,所以要写活
		this.width = width;
		this.height = height;
		this.x = x;
		this.y = y;
	}

	public void say() {
		System.out.println("飞行物移动了");
	}
}
package cn;


public class Hero  extends FlyingObject{
	private int life;
	private int fire;
	public Hero(){
		super(140,100,140,100);

	
		life=3;
		fire=0;
	}
	public void say(){
		System.out.println("我愿成为你的守护天使");
	}
}
package cn;


import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;


public class Images {
	public static BufferedImage sky;
	public static BufferedImage bullet;
	public static BufferedImage[] heros;
	public static BufferedImage[] airo;
	public static BufferedImage[] airs;
	public static BufferedImage[] bairs;
	public static BufferedImage[] bees;

	static {// 初始化静态资源
		sky =readImage("background.png");
		bullet=readImage("bullet.png");
		heros=new BufferedImage[2];
		heros[0]=readImage("hero0.png");
		heros[0]=readImage("hero1.png");
		airs=new BufferedImage[5];
		bairs=new BufferedImage[5];
		bees=new BufferedImage[5];
		airs[0]=readImage("airplane.png");
		bairs[0]=readImage("bigairplane.png");
		bees[0]=readImage("bee.png");
	for(int i=1;i<airs.length;i++){	
		airs[i]=readImage("bom"+i+".png");
		bairs[i]=readImage("bom"+i+".png");
		bees[i]=readImage("bom"+i+".png");
	}
	
	}
	

	public static BufferedImage readImage(String fileName) {
		try {
			BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName));//读取同包中的
			return img;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
}


package cn;


public class Sky extends FlyingObject{
	
	private int speed;//移动速度
	private int y1;//第2张图的y坐标
	/**构造方法*/
	public Sky(){
		super(400,700,0,0);
		
		speed=1;
		y1=-700;
		
	}
	public void say(){
		System.out.println("我愿成为你的守护天使");
	}
	
	

}

(老规矩写5遍,今天不学习,明天变废物)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public abstract class FlyingObject { public static final int LIFE = 0; //活着呢 public static final int DEAD = 1; //死了的(先爆破) public static final int REMOVE = 2; //删除了(爆破后删除) protected int state = LIFE; //当前状态(默认活着) protected int width; //宽 protected int height; //高 protected int x; //x坐标 protected int y; //y坐标 /** 无参构造方法 */ public FlyingObject(){ } /**专门给小敌机、大敌机、小蜜蜂提供的构造方法 */ public FlyingObject(int width,int height){ this.width = width; this.height = height; Random rand = new Random(); x = rand.nextInt(World.WIDTH-width); //x:0到(窗口-小敌机的宽)之间的随机数 y = -height; //y:负的小敌机的高 } /** 专门给英雄机、子弹、天空提供的构造方法 */ public FlyingObject(int width,int height,int x,int y){ this.width = width; this.height = height; this.x = x; this.y = y; } /** 读取图片 */ public static BufferedImage loadImage(String fileName){ try{ BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName)); return img; }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(); } } /** 飞行物移动了 */ public abstract void step(); public abstract BufferedImage getImage(); /** 判断是否活着 */ public boolean isLife(){ return state==LIFE; } /** 判断是否死了的 */ public boolean isDead(){ return state==DEAD; } /** 判断是否删除的 */ public boolean isRemove(){ return state==REMOVE; } /** 画对象 g:画笔 */ public void paintObject(Graphics g){ g.drawImage(getImage(), x, y, null); } /** 检测飞行物是否越界 */ public abstract boolean outOfBounds(); /** 敌人与子弹/英雄机的碰撞 this:敌人 other:子弹或英雄机 */ public boolean hit(FlyingObject other){ int x1 = this.x-other.width; //x1:敌人的x int x2 = this.x+this.width; //x2:敌人的x int y1 = this.y-other.height; //y1:敌人的y int y2 = this.y+this.height; //y2:敌人的y int x = other.x; //x:子弹的x int y = other.y; //y:子弹的y return x>=x1 && x=y1 && y<=y2; } public void goDead(){ state = DEAD; //修改当前状态为死了的 } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值