代码架构:程序分两个包,共两个类,一个实现大多功能的主程序类,一个图片读取类
操作效果图:
ImageUtil.java:游戏加载图片的工具类
package com.demo.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* 游戏加载图片的工具类
*/
public class ImageUtil {
/**
* 加载磁盘指定图片文件进入到程序的内存中
* @param name
* @return
*/
public static BufferedImage readImage(String name){
try {
return ImageIO.read(new File("images",name));
} catch (IOException e) {
e.printStackTrace();
}
return null; //若找不到图片返回null
}
}
GameStart.java:主程序
package com.demo.model;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.demo.util.ImageUtil;
/**
* 飞机大战小游戏(一):UI界面
* @author suoyue_zhan
*/
public class GameStart extends JPanel {
// 背景图片
private BufferedImage background1 = ImageUtil.readImage("background1.jpg");
private BufferedImage backgroundA = ImageUtil.readImage("background2.png");
private BufferedImage backgroundB = ImageUtil.readImage("background2.png");
//游戏状态图片
private BufferedImage start = ImageUtil.readImage("start.png");
private BufferedImage pause = ImageUtil.readImage("pause.png");
private BufferedImage gameover = ImageUtil.readImage("gameover.png");
//游戏状态
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;//结束状态
private int state = START; //默认是开始状态
// 重写JPanel的绘制方法-->>所有图片都在该方法上执行
@Override
public void paint(Graphics g) {
if(state == RUNNING) {
//绘制动态背景
this.paintBackground(g);
}
//非RUNNING状态执行
this.paintState(g);
}
/**
* 绘制游戏状态
* @param g
*/
private void paintState(Graphics g){
if(state == START) {
g.drawImage(start, 0, 0, null);
}else if(state == PAUSE) {
g.drawImage(pause, 0, 0, null);
}else if (state == GAME_OVER) {
g.drawImage(gameover, 0, 0, null);
}
}
//绘制背景
private int yA = -backgroundA.getHeight();
private int yB = 0;
public void paintBackground(Graphics g) {
// 方法:将背景图画到画板上
// 参数:图片,x轴,y轴,忽略
g.drawImage(background1, 0, 0, null);
g.drawImage(backgroundA, 0, yA, null);
g.drawImage(backgroundB, 0, yB, null);
}
/**
*********游戏逻辑方法--------------
*/
//背景移动方法-->>图片的循环下移
private void backgroundMove(){
yA++;
yB++;
if(yB >= backgroundA.getHeight()){
yB = -backgroundA.getHeight();
}
if(yA >= backgroundA.getHeight()){
yA = -backgroundA.getHeight();
}
}
/**
* 窗口初始化
*/
public void init() {
JFrame jFrame = new JFrame("飞机大战"); // 设置窗口标题
jFrame.add(this); // this表示类UI,也表示画板
jFrame.setSize(400, 654); // 设置窗口大小
jFrame.setLocationRelativeTo(null); // 设置窗口居中
jFrame.setAlwaysOnTop(true); // 设置窗口总是在顶端
jFrame.setResizable(false); // 设置不允许拖拉
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序
jFrame.setIconImage(new ImageIcon("./images/icon.png").getImage()); // 设置图标
jFrame.setVisible(true); // 使窗口显示出来
initListener(); //安装程序监听器
initTimer(); //设置定时器,用于实现游戏中所有的动态逻辑效果
}
/**
* 监控鼠标的监听器
*/
private void initListener() {
MouseAdapter adapter = new MouseAdapter() {
//鼠标移动监听方法
@Override
public void mouseMoved(MouseEvent e) {
System.out.println("x:"+e.getX()+"y:"+e.getY());
}
//鼠标点击事件
@Override
public void mouseClicked(MouseEvent e) {
//鼠标点击画板时执行
if(state == START){
state = RUNNING;
}else if(state == RUNNING){
state = GAME_OVER;
}else if(state == GAME_OVER){
state = START;
}
}
//鼠标移出去时事件
@Override
public void mouseExited(MouseEvent e) {
if(state == RUNNING){
state = PAUSE;
}
}
//鼠标移进来时事件
@Override
public void mouseEntered(MouseEvent e) {
if(state == PAUSE){
state = RUNNING;
}
}
};
//安装监听器
this.addMouseListener(adapter);
this.addMouseMotionListener(adapter);
}
/**
* 定时器
*/
public void initTimer() {
Timer timer = new Timer(); //定时器对象
long delay = 10;
long period = 10;
//定时执行操作
//参数1:每隔一段时间执行的操作
//参数2:几毫米之后执行定时器
//参数3:每个几毫米执行一次
timer.schedule(new TimerTask() {
@Override
public void run() {
backgroundMove(); //背景移动
repaint(); //从新绘制页面
}
}, delay,period);
}
/*
* 程序入口main()
*/
public static void main(String[] args) {
System.out.println("游戏开始了.....");
GameStart gameStart = new GameStart();
gameStart.init(); //显示主界面
}
}