一、描述
游戏规则很简单,应该所有人都玩过。
该游戏主要使用java的swing组件实现。虽然已过时,但是还要学。
二、代码
1、游戏面板
package com.Games.PlanesWars;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
/**
* @author: Mae.W
* 游戏面板:游戏规则……
*/
public class GamePanel extends JPanel {
//定义飞机的X轴坐标
private int planeX;
//定义飞机的Y轴坐标
private int planeY;
//定义飞机运动速度 x y 相同
private int planeSpeed;
//定义炮弹运动速度 x y 相同
private int shellSpeed;
//定义炮弹个数
int shellsCount = 60;
//将炮弹x轴坐标存放在一个长度为shellsCount的数组中
private int[] shellX = new int[shellsCount];
//将炮弹y轴坐标存放在一个长度为shellsCount的数组中
private int[] shellY = new int[shellsCount];
//定义每一个炮弹飞行的弧度
private double[] degrees = new double[shellsCount];
//游戏 两个状态 开始 暂停
private boolean isStart = false; //默认游戏为暂停
//定义飞机 飞的方向
private boolean left, right, up, down;
//定义一个定时器
private Timer timer;
//定义变量 判断飞机的生死
private boolean isDie;
//游戏开始时间
private long startTime;
//游戏结束时间
private long endTime;
//初始化
public void init() {
//初始 飞机为生
isDie = false;
//开始时间
startTime = System.currentTimeMillis();
/* 初始化炮弹个数
shellsCount=20; */
//初始化飞机坐标
planeX = 400;
planeY = 500;
//初始化飞机速度 每 移动6个像素
planeSpeed = 12;
//初始化炮弹速度 每 移动8个像素
shellSpeed = 20;
//初始化炮弹坐标 给每个炮弹设置随机弧度
Random r = new Random();
for (int i = 0; i < shellsCount; i++) {
shellX[i] = r.nextInt(800) ;
shellY[i] = r.nextInt(250) ;
//炮弹弧度 随机生成 [0,2π];
degrees[i] = r.nextInt((int) (2 * Math.PI));
}
//初始化定时器:每50ms 执行actionPerformed 中的逻辑
timer = new Timer(50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (isStart == true && isDie == false) {//只有游戏开始时,飞机才会动
if (left && planeX >= 0) {
planeX -= planeSpeed;
}
if (right && planeX <= 720) {
planeX += planeSpeed;
}
if (up && planeY >= 0) {
planeY -= planeSpeed;
}
if (down && planeY <= 720) {
planeY += planeSpeed;
}
//炮弹运动 x y方向
for (int i = 0; i < shellsCount; i++) {
shellX[i] += Math.cos(degrees[i]) * shellSpeed;
shellY[i] += Math.sin(degrees[i]) * shellSpeed;
//炮弹超过边界后 从另一个边界过来
if (shellX[i] <= 0) {
shellX[i] = 800;
}
if (shellX[i] >= 800) {
shellX[i] = 0;
}
if (shellY[i] <= 0) {
shellY[i] = 800;
}
if (shellY[i] >= 800) {
shellY[i] = 0;
}
//飞机和炮弹 有碰撞(即图片有接触)则游戏结束
boolean flag = new Rectangle(planeX, planeY, 29, 30).intersects(new Rectangle(shellX[i], shellY[i], 48, 70));
if (flag) {
//结束游戏
endTime = System.currentTimeMillis();
isDie = true;
}
}
//重绘画面
repaint();
}
}
});
//将定时器启动
timer.start();
}
public GamePanel() {
//进行初始化
init();
//将焦点放在面板上
this.setFocusable(true);
//加入监听效果 监听开始/暂停
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//键盘按下键后
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if (isDie) {
//飞机死亡 按下空格键 重新初始化
init();
isDie = false;
} else {
//按下空格键 游戏将变为 开始/暂停
isStart = !isStart;
}
//重新绘制页面
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = true;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
//按下键释放后
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = false;
}
}
});
}
//重写方法 用画板绘画
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//给面板设置背景图片
Images.BGD_Img.paintIcon(this, g, 0, 0); //this 表示当前对象为面板 直接在面板上绘画
//画飞机
Images.planeImg.paintIcon(this, g, planeX, planeY);
//画炮弹
for (int i = 0; i < shellsCount; i++) {
Images.shellImg.paintIcon(this, g, shellX[i], shellY[i]);
}
//游戏是暂停的 面板中间打印一行文字
if (!isStart) {
//画笔颜色
g.setColor(new Color(141, 22, 184, 255));
//文字样式 文字加粗 字号
g.setFont(new Font("微软雅黑", Font.BOLD, 34));
//写入的文字
g.drawString("按下空格开始游戏", 250, 350);
}
if (isDie) {
Images.boomImg.paintIcon(this,g,planeX,planeY);
//画笔颜色
g.setColor(new Color(141, 22, 184, 255));
//文字样式 文字加粗 字号
g.setFont(new Font("微软雅黑", Font.BOLD, 34));
//写入的文字
g.drawString("游戏结束!您的分数:" + (endTime - startTime) / 100, 250, 350);
timer.stop();
}
}
}
2、图片类
package com.Games.PlanesWars;
import javax.swing.*;
import java.net.URL;
/**
* @author: Mae.W
* 获取图片文件
*/
public class Images {
//把图片地址封装为一个具体的对象
static URL planeURL =Images.class.getResource("/images/Plane.jpg");
static URL shellURL =Images.class.getResource("/images/Shell.jpg");
static URL BGD_URL=Images.class.getResource("/images/Backg.jpg");
static URL spaceURL=Images.class.getResource("/images/Space.jpg");
static URL boomURL=Images.class.getResource("/images/Boom.jpg");
//把图片封装为一个对象
public static ImageIcon planeImg=new ImageIcon(planeURL);
public static ImageIcon shellImg=new ImageIcon(shellURL);
public static ImageIcon BGD_Img=new ImageIcon(BGD_URL);
static ImageIcon spaceImg=new ImageIcon(spaceURL);
public static ImageIcon boomImg=new ImageIcon(boomURL);
}
3、游戏中画面
package com.Games.PlanesWars;
import javax.swing.*;
import java.awt.*;
/**
* @author: Mae.W
* 启动游戏
* 游戏中画面
*/
public class PlayGames {
//构建一个窗体
static JFrame jf=new JFrame("飞机大作战");
static Panel panel=new Panel();;
static StartGames startGames=new StartGames();
static GamePanel gamePanel=new GamePanel();
//设置卡片布局
static CardLayout card=new CardLayout();
public static void main(String[] args) {
panel.setLayout(card);
/* panel.add(startGames);*/
panel.add(gamePanel);
//设置窗体的弹出位置
/*
求出屏幕的宽和高 将窗体放置中央
Toolkit.getDefaultToolkit().getScreenSize().width/height
*/
//屏幕的宽和高
int width= Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
//设置窗体大小
jf.setBounds((width-800)/2,(height-800)/2,800,800);
//并且可按X将窗体关闭
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗体不可随便放大缩小
jf.setResizable(false);
//设置窗体可见
jf.setVisible(true);
/* GamePanel gp=new GamePanel();
jf.add(gp);*/
//将面板放入窗口中
/* jf.add(new StartGames());*/
jf.add(panel);
}
}
4、启动游戏
package com.Games.PlanesWars;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author: Mae.W
* 启动游戏
*/
public class StartGames extends JPanel{
@Override
protected void paintComponent(Graphics g) {
//给面板设置背景图片
Images.spaceImg.paintIcon(this, g, 0, 0); //this 表示当前对象为面板 直接在面板上绘画
//画笔颜色
g.setColor(new Color(10, 9, 9, 255));
//文字样式 文字加粗 字号
g.setFont(new Font("微软雅黑", Font.BOLD, 40));
//写入的文字
g.drawString("飞 机 大 作 战", 250, 80);
//添加按钮
JButton startGame=new JButton("开始游戏");
startGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PlayGames playGames = new PlayGames();
playGames.card.next(playGames.panel);
}
});
}
}
三、界面展示
找不到图片合适图片,所有画面可能不美观