文章目录
- 项目-飞机大战
- 窗口的创建
- 背景图片的添加/点击事件的启动
- 游戏物体父类的编写
- 背景的移动
- 双缓存技术--解决文字闪动
- 背景图片循环出现
- 我方战斗机的添加和鼠标控制
- 添加首颗子弹
- 批量添加子弹
- 敌方飞机的批量添加功能
- 我方子弹与敌方飞机的碰撞检测
- 我方子弹与敌方飞机碰撞时的处理
- 我方子弹与敌方飞机碰撞时碰撞坠毁
- 敌方boss的添加和移动
- 为敌方boss添加子弹
- 我方飞机和敌方飞机子弹碰撞检测
- 我方飞机与敌方boss的碰撞检测
- 计分板的编写
- 设定游戏规则
- 为敌方boss添加生命值
- 游戏的暂停功能
- 子弹和敌机的越界消失
- 为敌方小飞机添加爆炸效果
- 为敌方boss添加生命血条
项目-飞机大战
窗口的创建
package GUI.project1;
import javax.swing.*;
public class GameWin extends JFrame {
public void launch(){
//设置窗口是否可见
this.setVisible(true);
//设置窗口大小
this.setSize(600,600);
//设置窗口位置
this.setLocationRelativeTo(null);
//设置窗口标题
this.setTitle("aircraft battle");
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
背景图片的添加/点击事件的启动
GameUtil.java
package project1;
import java.awt.*;
public class GameUtils {
//背景图片
public static Image bgImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\bg.jpg");
//boss图片
public static Image bossImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\boss.png");
//爆炸图片
public static Image explodeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\explode\\e6.gif");
}
GameWin.java
package project1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWin extends JFrame {
//游戏状态 0未开始 1游戏中 2暂停 3通关失败 4通关成功
static int state = 0;
public void launch(){
//设置窗口是否可见
this.setVisible(true);
//设置窗口大小
this.setSize(600,600);
//设置窗口位置
this.setLocationRelativeTo(null);
//设置窗口标题
this.setTitle("aircraft battle");
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//e.getButton()==1:按下鼠标左键
if (e.getButton()==1 && state==0){
state = 1;
repaint();
}
}
});
}
@Override
public void paint(Graphics g) {
if (state == 0){
g.drawImage(GameUtils.bgImg,0,0,this);
g.drawImage(GameUtils.bossImg,220,120,this);
g.drawImage(GameUtils.explodeImg,270,350,this);
g.setColor(Color.yellow);
g.setFont(new Font("仿宋",Font.BOLD,40));
g.drawString("点击开始游戏",180,300);
}
if (state == 1){
g.drawImage(GameUtils.bgImg,0,0,this);
g.drawImage(GameUtils.bossImg,220,120,this);
g.drawImage(GameUtils.explodeImg,270,350,this);
g.setColor(Color.yellow);
g.setFont(new Font("仿宋",Font.BOLD,40));
g.drawString("开始游戏了",180,300);
}
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
游戏物体父类的编写
GameObj.java
package project1;
import java.awt.*;
public class GameObj {
Image img;
int x;
int y;
int width;
int height;
double speed;
GameWin frame;
public GameObj() {
}
public GameObj(Image img, int x, int y, double speed) {
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
}
public GameObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
this.img = img;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = speed;
this.frame = frame;
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
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 double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public GameWin getFrame() {
return frame;
}
public void setFrame(GameWin frame) {
this.frame = frame;
}
public void paintSelf(Graphics gImage){
gImage.drawImage(img,x,y,null);
}
public Rectangle getRec(){
return new Rectangle(x,y,width,height);
}
}
背景的移动
package project1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWin extends JFrame {
//游戏状态 0未开始 1游戏中 2暂停 3通关失败 4通关成功
static int state = 0;
//背景图对象
BgObj bgObj = new BgObj(GameUtils.bgImg,0,-2000,2);
public void launch(){
//设置窗口是否可见
this.setVisible(true);
//设置窗口大小
this.setSize(600,600);
//设置窗口位置
this.setLocationRelativeTo(null);
//设置窗口标题
this.setTitle("aircraft battle");
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//e.getButton()==1:按下鼠标左键
if (e.getButton()==1 && state==0){
state = 1;
repaint();
}
}
});
while (true){
repaint();
try{
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics g) {
if (state == 0){
g.drawImage(GameUtils.bgImg,0,0,this);
g.drawImage(GameUtils.bossImg,220,120,this);
g.drawImage(GameUtils.explodeImg,270,350,this);
g.setColor(Color.yellow);
g.setFont(new Font("仿宋",Font.BOLD,40));
g.drawString("点击开始游戏",180,300);
}
if (state == 1){
bgObj.paintSelf(g);
}
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
双缓存技术–解决文字闪动
修改GameWin.java
package project1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWin extends JFrame {
//游戏状态 0未开始 1游戏中 2暂停 3通关失败 4通关成功
static int state = 0;
Image offScreenImage = null;
int width = 600;
int height = 600;
//背景图对象
BgObj bgObj = new BgObj(GameUtils.bgImg,0,-2000,2);
public void launch(){
//设置窗口是否可见
this.setVisible(true);
//设置窗口大小
this.setSize(width,height);
//设置窗口位置
this.setLocationRelativeTo(null);
//设置窗口标题
this.setTitle("aircraft battle");
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//e.getButton()==1:按下鼠标左键
if (e.getButton()==1 && state==0){
state = 1;
repaint();
}
}
});
while (true){
repaint();
try{
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics g) {
if (offScreenImage == null){
offScreenImage = createImage(width,height);
}
Graphics gImage = offScreenImage.getGraphics();
gImage.fillRect(0,0,width,height);
if (state == 0){
gImage.drawImage(GameUtils.bgImg,0,0,this);
gImage.drawImage(GameUtils.bossImg,220,120,this);
gImage.drawImage(GameUtils.explodeImg,270,350,this);
gImage.setColor(Color.yellow);
gImage.setFont(new Font("仿宋",Font.BOLD,40));
gImage.drawString("点击开始游戏",180,300);
}
if (state == 1){
bgObj.paintSelf(gImage);
}
g.drawImage(offScreenImage,0,0,null);
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
背景图片循环出现
在BgObj.java中设置临界条件
我方战斗机的添加和鼠标控制
PlaneObj.java
package project1;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PlaneObj extends GameObj{
@Override
public Image getImg() {
return super.getImg();
}
public PlaneObj() {
super();
}
public PlaneObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
super(img, x, y, width, height, speed, frame);
this.frame.addMouseListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
PlaneObj.super.x = e.getX() - 11;
PlaneObj.super.y = e.getY() - 16;
}
});
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
bug:飞机没动静
添加首颗子弹
批量添加子弹
GameWin.java
在这里插入代码片package project1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWin extends JFrame {
//游戏状态 0未开始 1游戏中 2暂停 3通关失败 4通关成功
static int state = 0;
Image offScreenImage = null;
int width = 600;
int height = 600;
//背景图对象
BgObj bgObj = new BgObj(GameUtils.bgImg,0,-2000,2);
//飞机对象
PlaneObj planeObj = new PlaneObj(GameUtils.planeImg,290,550,20,30,0,this);
//游戏的重绘次数
int count = 1;
public void launch(){
//设置窗口是否可见
this.setVisible(true);
//设置窗口大小
this.setSize(width,height);
//设置窗口位置
this.setLocationRelativeTo(null);
//设置窗口标题
this.setTitle("aircraft battle");
GameUtils.gameObjList.add(bgObj);
GameUtils.gameObjList.add(planeObj);
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//e.getButton()==1:按下鼠标左键
if (e.getButton()==1 && state==0){
state = 1;
repaint();
}
}
});
while (true){
if (state == 1){
createObj();
repaint();
}
repaint();
try{
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics g) {
if (offScreenImage == null){
offScreenImage = createImage(width,height);
}
Graphics gImage = offScreenImage.getGraphics();
gImage.fillRect(0,0,width,height);
if (state == 0){
gImage.drawImage(GameUtils.bgImg,0,0,this);
gImage.drawImage(GameUtils.bossImg,220,120,this);
gImage.drawImage(GameUtils.explodeImg,270,350,this);
gImage.setColor(Color.yellow);
gImage.setFont(new Font("仿宋",Font.BOLD,40));
gImage.drawString("点击开始游戏",180,300);
}
if (state == 1){
//运行中
for (int i =0;i<GameUtils.gameObjList.size();i++){
GameUtils.gameObjList.get(i).paintSelf(gImage);
}
}
g.drawImage(offScreenImage,0,0,null);
count++;
}
void createObj(){
if (count % 15 == 0){
//我方子弹
GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg,planeObj.getX()+3,planeObj.getY()-16,14,29,5,this));
//通过索引把shellObjList的最后一个元素,加入到gameObjList中 ???
GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size()-1));
}
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
GameUtils.java
package project1;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class GameUtils {
//背景图片
public static Image bgImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\bg.jpg");
//boss图片
public static Image bossImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\boss.png");
//爆炸图片
public static Image explodeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\explode\\e6.gif");
//我方战斗机图片
public static Image planeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\plane.png");
//我方子弹图片
public static Image shellImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\shell.png");
//所有游戏物体的集合
public static List<GameObj> gameObjList = new ArrayList<>();
//我方子弹的集合
public static List<ShellObj> shellObjList = new ArrayList<>();
}
问题:
1.GUI中添加集合元素的用法
2.gameObjList的意义
解答:看下一节课的代码
敌方飞机的批量添加功能
1.创建EnemyObj.java
package project1;
import java.awt.*;
public class EnemyObj extends GameObj{
public EnemyObj() {
super();
}
public EnemyObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
y += speed;
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
2.在GameUtil.java中添加敌方图片和集合
package project1;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class GameUtils {
//背景图片
public static Image bgImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\bg.jpg");
//boss图片
public static Image bossImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\boss.png");
//爆炸图片
public static Image explodeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\explode\\e6.gif");
//我方战斗机图片
public static Image planeImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\plane.png");
//我方子弹图片
public static Image shellImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\shell.png");
//敌机图片
public static Image eneImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\enemy.png");
//所有游戏物体的集合
public static List<GameObj> gameObjList = new ArrayList<>();
//我方子弹的集合
public static List<ShellObj> shellObjList = new ArrayList<>();
//敌机的集合
public static List<EnemyObj> enemyObjList = new ArrayList<>();
}
3.在GameWin.java中像上一步操作中一样,添加enemyObjList和gameObjList中
x值的设置:可看视频讲解
void createObj(){
if (count % 15 == 0){
//我方子弹
GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg,planeObj.getX()+3,planeObj.getY()-16,14,29,5,this));
//通过索引把shellObjList的最后一个元素,加入到gameObjList中 ???
GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size()-1));
}
if (count % 15 == 0){
GameUtils.enemyObjList.add(new EnemyObj(GameUtils.eneImg,(int) (Math.random()*12)*50,0,49,36,5,this));
GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.enemyObjList.size()-1));
}
}
我方子弹与敌方飞机的碰撞检测
EnemyObj.java中修改
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
y += speed;
for (ShellObj shellObj:GameUtils.shellObjList){
if (this.getRec().intersects(shellObj.getRec())){
System.out.println("碰撞了");
}
}
}
我方子弹与敌方飞机碰撞时的处理
我方子弹与敌方飞机碰撞时碰撞坠毁
EnemyObj.java
GameWin.java
因为鼠标移动的那个bug,所以没办法达到碰撞坠毁的效果
敌方boss的添加和移动
为敌方boss添加子弹
1.新建BulletObj类
package project1;
import java.awt.*;
public class BulletObj extends GameObj{
public BulletObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
2.在GameUtils.java中添加图片和集合
//敌方子弹的集合
public static Image bulletImg = Toolkit.getDefaultToolkit().getImage("C:\\IntelliJ IDEA Community Edition 2022.2.3\\study\\src\\imgs\\bullet.png");
//敌方子弹的集合
public static List<BulletObj> bulletObjList = new ArrayList<>();
3.在GameWin.java中加入集合类中
if (count % 15 == 0){
GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImg,bossObj.getX()+76,bossObj.getY()+85,15,25,5,this));
GameUtils.gameObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size()-1));
}
4.再回BulletObj,java中添加子弹速度
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
y += speed;
}
我方飞机和敌方飞机子弹碰撞检测
在BulletObj.java中设置state为3
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
y += speed;
//敌方子弹于我方飞机的碰撞检测
if (this.getRec().intersects(this.frame.planeObj.getRec())){
GameWin.state = 3;
}
}
我方飞机与敌方boss的碰撞检测
在PlaneObj.java中设置state为3
计分板的编写
1.在GameWin.java中定义游戏得分变量
2.在 GameUtil.java中绘制字符串工具类
3.在主类中替换相同代码
4.在主类中绘制计分板
5.在EnemyObj.java中检测碰撞时score++
设定游戏规则
当敌机出现100架后,Boss才出现
1.在GameWin.java中设定
2.GameWin.java中,生成一辆敌机,enemyCount就++
3.增加if判断条件,判断bossobj的生成条件
4.增加boss的子弹生成判断
5.bug:报错 --去PlaneObj.java中的碰撞检测加上判断
为敌方boss添加生命值
1.设定生命值
2.设置敌方飞机和我方子弹的碰撞检测
3.去GameWin.java中添加通关提示语
游戏的暂停功能
子弹和敌机的越界消失
因为子弹和敌机在窗口外依然被绘制,所以会降低游戏运行速度
1.子弹跑出窗口后的处理
2.敌机子弹消失判断
3.在GameWin.java中观察长度
输出:最后稳定
为敌方小飞机添加爆炸效果
1.ExplodeObj类的实现
2.GameObj.java中构造器的添加
3.ExplodeObj类中构造器的继承
4.将爆炸图放在图片数组中
2.添加爆炸图片的判断
3.在主类中添加爆炸集合类
4.在EnemyObj.java中绑定爆炸类
5.从GameWin.java添加到gameobjList中
6.在EnemyObj.java添加到removeList中
为敌方boss添加生命血条
在BossObj.java中添加