复习1.创建窗口对象
2.创建画板对象
3.类的设计 – 确定类中目前需要的成员变量和成员方法
4.成员方法目前只有一个 – move(),研究不同类的移动方法
5.创建类需要不同的构造方法 – 初始化成员变量
a -> static-> 静态代码块 需要静态成员变量
b –> 获取父类中的成员变量
- 如果父类中的成员变量是protected修饰的 子类可以直接获取
- 如果父类中的成员变量是private修饰的 调用有参构造方法/get set
今天主要写程序 把“飞机大战”写了一部分
paint部分
import PlaneFight.Bullet;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.*;
import java.util.Timer;
public class JPaintImage extends JPanel {
private Bullet[] bullets = {};
private Hero h1 = new Hero();
ArrayList<Plane> flying = new ArrayList<>();
static BufferedImage bigPlane;
static BufferedImage bg;
static BufferedImage smallPlane;
static BufferedImage Bee;
static BufferedImage hero0;
static BufferedImage hero1;
static BufferedImage bullet;
public static BufferedImage start;
public static BufferedImage pause;
public static BufferedImage gameover;
private int state;
private static final int START = 0;
private static final int RUNNING = 1;
private static final int PAUSE = 2;
private static final int GAME_OVER = 3;
static{
try {
bg = ImageIO.read(Objects.requireNonNull(ImageDemo.class.getResource("pic/background.png")));
smallPlane=ImageIO.read(Objects.requireNonNull(ImageDemo.class.getResource("pic/airplane.png")));
bigPlane = ImageIO.read(Objects.requireNonNull(ImageDemo.class.getResource("pic/bigplane.png")));
Bee = ImageIO.read(Objects.requireNonNull(ImageDemo.class.getResource("pic/bee.png")));
hero0 = ImageIO.read(Objects.requireNonNull(ImageDemo.class.getResource("pic/hero0.png")));
hero1 = ImageIO.read(Objects.requireNonNull(ImageDemo.class.getResource("pic/hero1.png")));
bullet = ImageIO.read(Objects.requireNonNull(ImageDemo.class.getResource("pic/bullet.png")));
start = ImageIO.read(Objects.requireNonNull(ImageDemo.class.getResource("pic/start.png")));
pause = ImageIO.read(Objects.requireNonNull(ImageDemo.class.getResource("pic/pause.png")));
gameover = ImageIO
.read(Objects.requireNonNull(ImageDemo.class.getResource("pic/gameover.png")));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
super.paint(g);
//画背景
g.drawImage(bg,0,0,this);
//设置字体
setFont(g);
//生成飞机
paintBullets(g);
drawHero(g);
drawFlying(g);
paintState(g);
}
java.util.Timer timer = new Timer();
public void action(){
MouseAdapter l = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) { // 鼠标移动
if (state == RUNNING) {
int x = e.getX();
int y = e.getY();
h1.moveTo(x, y);
}
}
@Override
public void mouseEntered(MouseEvent e) { // 鼠标进入
if (state == PAUSE) { // 暂停状态下运行
state = RUNNING;
}
}
@Override
public void mouseExited(MouseEvent e) { // 鼠标退出
if (state != GAME_OVER&&state!=START) { // 游戏未结束,则设置其为暂停
state = PAUSE;
}
}
@Override
public void mouseClicked(MouseEvent e) { // 鼠标点击
switch (state) {
case START:
state = RUNNING; // 启动状态下运行
break;
case GAME_OVER: // 游戏结束,清理现场
flying.clear(); // 清空飞行物
bullets = new Bullet[0]; // 清空子弹
h1= new Hero(); // 重新创建英雄机
// 清空成绩
state = START; // 状态设置为启动
break;
}
}
};
this.addMouseListener(l); // 处理鼠标点击操作
this.addMouseMotionListener(l); // 处理鼠标滑动操作
timer.schedule(new TimerTask() {
@Override
public void run() {
if (state == RUNNING) {
h1.move();
shootAction();
//enterAction();
//stepAction();
creatPlanes();
flyingMove();
outOfBoundsAction();
}repaint();
}
}, 100, 40);
}
/*int flyEnteredIndex = 0;
public void enterAction() {
flyEnteredIndex++;
if (flyEnteredIndex % 40 == 0) { // 400毫秒生成一个飞行物--10*40
Plane obj = nextOne(); // 随机生成一个飞行物
Planes = Arrays.copyOf(Planes, Planes.length + 1);
Planes[Planes.length - 1] = obj;
}
}
public void drawPlanes(Graphics g){
for (int i = 0; i < Planes.length; i++) {
Plane obj = Planes[i];
g.drawImage(obj.getImg(), obj.getX(),obj.getY(),this);
}
}
public void stepAction() {
for (int i = 0; i < Planes.length; i++) { // 飞行物走一步
Plane f = Planes[i];
f.move();
}
}*/
int flyingCount = 0;
public void creatPlanes(){
flyingCount++;
if(flyingCount%40==0) {
int random = (int) (Math.random() * 20);
Plane fly;
if (random == 0) {
fly = new Bee();
} else if (random == 1 || random == 2 || random == 3) {
fly = new BigPlane();
} else {
fly = new SmallPlane();
}
flying.add(fly);
}
}
public void paintBullets(Graphics g) {
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
g.drawImage(b.getImage(), b.getX() - b.getWidth() / 2, b.getY(),
null);
}
}
public void drawHero(Graphics g){ g.drawImage(h1.getImg(),h1.x,h1.y,this); }
public void drawFlying(Graphics g){
Plane fly ;
for (int i = 0; i < flying.size(); i++) {
fly = flying.get(i);
g.drawImage(fly.getImg(), fly.getX(),fly.getY(),null);
}
}
public void paintState(Graphics g) {
switch (state) {
case START: // 启动状态
g.drawImage(start, 0, 0, null);
break;
case PAUSE: // 暂停状态
g.drawImage(pause, 0, 0, null);
break;
case GAME_OVER: // 游戏终止状态
g.drawImage(gameover, 0, 0, null);
break;
}
}
int shootIndex = 0;
public void shootAction() {
shootIndex++;
if (shootIndex % 30 == 0) { // 300毫秒发一颗
Bullet[] bs = h1.shoot(); // 英雄打出子弹
bullets = Arrays.copyOf(bullets, bullets.length + bs.length); // 扩容
System.arraycopy(bs, 0, bullets, bullets.length - bs.length,
bs.length); // 追加数组
}
}
public void flyingMove(){
for (int i = 0; i < flying.size(); i++) {
Plane fly = flying.get(i);
fly.move();
}
for (int i = 0; i < bullets.length; i++) { // 子弹走一步
Bullet b = bullets[i];
b.step();
}
}
public void outOfBoundsAction(){
for (int i = 0; i < flying.size(); i++) {
Plane f = flying.get(i);
if(f.outOfBounds()){
flying.remove(f);
i--;
}
}
int index = 0; // 索引重置为0
Bullet[] bulletLives = new Bullet[bullets.length];
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
if (!b.outOfBounds()) {
bulletLives[index++] = b;
}
}
bullets = Arrays.copyOf(bulletLives, index);
}
public void setFont(Graphics g){
Font font = new Font("宋体",Font.BOLD,15);
g.setFont(font);
Color c = Color.black;
g.setColor(c);
g.drawString("score : ",10,20);
}
/*public static Plane nextOne() {
Random random = new Random();
int type = random.nextInt(20); // [0,20)
if (type == 0) {
return new Bee();
} else if(type == 1||type ==2){
return new BigPlane();
}else return new SmallPlane();
}*/
}
抽象主类plane
public abstract class Plane {
public BufferedImage img;
int x,y,blood;
int width,height;
public void setX(int x) {
this.x = x;
}
public BufferedImage getImg() {
return img;
}
public void setImg(BufferedImage img) {
this.img =img;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public boolean outOfBounds(){
return false;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public abstract void move();
public abstract void death() throws IOException;
}
bullet类
public class Bullet extends Plane{
Bullet(){}
Bullet(int x,int y){
this.x = x;
this.y = y;
this.img = JPaintImage.bullet;
}
@Override
public BufferedImage getImg() {
return this.img;
}
@Override
public int getX() {
return this.x;
}
@Override
public int getY() {
return this.y;
}
@Override
public void move() {
y = y-3;
}
@Override
public void death() throws IOException {
}
public boolean outOfBounds(){
return y<0;
}
}
Hero类
public class Hero extends Plane{
private int index = 0;
private int doubleFire;
int score;
private BufferedImage[] images = {};
public Hero(){
doubleFire = 0;
img = JPaintImage.hero0;
images = new BufferedImage[]{JPaintImage.hero0, JPaintImage.hero1};
width = img.getWidth();
height = img.getHeight();
blood = 3;
x = 150;
y = 400;
}
@Override
public BufferedImage getImg() {
return img;
}
public void moveTo(int x,int y){
this.x = x - width/2;
this.y = y - height/2;
}
@Override
public void setImg(BufferedImage img) {
super.setImg(img);
}
public void addDoubleFire(){
doubleFire = 40;
}
@Override
public void move() {
//图片的切换效果
if(images.length>0){
img = images[index++/10%images.length]; //切换图片hero0,hero1
}
}
@Override
public void death() throws IOException {
}
public PlaneFight.Bullet[] shoot(){
int xStep = width/4; //4半
int yStep = 20; //步
if(doubleFire>0){ //双倍火力
PlaneFight.Bullet[] bullets = new PlaneFight.Bullet[2];
bullets[0] = new PlaneFight.Bullet(x+xStep,y-yStep); //y-yStep(子弹距飞机的位置)
bullets[1] = new PlaneFight.Bullet(x+3*xStep,y-yStep);
return bullets;
}else{ //单倍火力
PlaneFight.Bullet[] bullets = new PlaneFight.Bullet[1];
bullets[0] = new Bullet(x+2*xStep,y-yStep);
return bullets;
}
}
}
大敌机类
public class BigPlane extends Plane{
int score;
BufferedImage img;
public BigPlane() {
img = JPaintImage.bigPlane;
y=0;
x=(int)(Math.random()*350);
blood = 5;
width = img.getWidth();
height = img.getHeight();
}
@Override
public void move() {
y=y+3;
}
@Override
public BufferedImage getImg() {
return this.img;
}
@Override
public int getX() {
return this.x;
}
@Override
public int getY() {
return this.y;
}
public boolean outOfBounds(){
return y>MainFrame.HEIGHT;
}
@Override
public void death() throws IOException {
}
}
奖励蜜蜂类
public class Bee extends Plane{
BufferedImage img;
int speed_x=1;
int speed_y=1;
Bee(){
img = JPaintImage.Bee;
width = img.getWidth();
height = img.getHeight();
y=0;
x=(int)(Math.random()*350);
blood = 3;
}
@Override
public void move() {
y+=speed_y;
x+=speed_x;
if(x>MainFrame.WIDTH-this.width-40){
speed_x = -1;
}
if(x<0){
speed_x = 1;
}
}
@Override
public void death() throws IOException {
}
@Override
public BufferedImage getImg() {
return this.img;
}
public boolean outOfBounds(){
return y>MainFrame.HEIGHT;
}
@Override
public int getX() {
return this.x;
}
@Override
public int getY() {
return this.y;
}
}
小敌机类
public class SmallPlane extends Plane{
int score;
BufferedImage img;
public SmallPlane(){
img = JPaintImage.smallPlane;
y=0;
x=(int)(Math.random()*350);
blood = 3;
width = img.getWidth();
height = img.getHeight();
}
@Override
public void move() {
y=y+5;
}
@Override
public void death() throws IOException {
}
public boolean outOfBounds(){
return y>MainFrame.HEIGHT;
}
@Override
public BufferedImage getImg() {
return img;
}
}