1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 简易飞机大作战——java图形化界面实现

简易飞机大作战——java图形化界面实现

时间:2024-03-08 05:41:13

相关推荐

简易飞机大作战——java图形化界面实现

一、描述

游戏规则很简单,应该所有人都玩过。

该游戏主要使用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() {@Overridepublic 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() {@Overridepublic 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;}}@Overridepublic 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;}}});}//重写方法 用画板绘画@Overrideprotected 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 .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{@Overrideprotected 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() {@Overridepublic void actionPerformed(ActionEvent e) {PlayGames playGames = new PlayGames();playGames.card.next(playGames.panel);}});}}

三、界面展示

找不到图片合适图片,所有画面可能不美观

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。