【JAVA】飞机大战
短信预约 -IT技能 免费直播动态提醒
代码和图片放在这个地址了:
https://gitee.com/r77683962/fighting/tree/master
最新的代码运行,可以有两架飞机,分别通过WASD(方向),F(发子弹);上下左右(控制方向),空格(发子弹)
但是敌人的飞机还没有。
代码写的比较匆忙,不太好。
运行效果:
import javax.swing.*;import java.awt.*;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;class MyThread extends Thread{ GameFrame gameFrame; public MyThread() { gameFrame = new GameFrame(); } public void run() { System.out.println("run"); Boolean runFlag = true; while (runFlag) { runFlag = gameFrame.Run(); //System.out.println("runFlag: " + runFlag); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); throw new RuntimeException(e); } } } public static void main(String[] args) { MyThread gameFrame = new MyThread(); gameFrame.start(); }}public class GameFrame extends JFrame{ private Boolean runFlag; private final Graphics graphics; private final Image planeImage; private final Image bulletImage; private final Image backgroundImage1; private final Image backgroundImage2; //plane x private int planeX; //plane y private int planeY; private int bulletX; private int bulletY; private boolean bulletFlag; private int backgroundY; //bullet move step private final int moveStep; private final int width; private final int height; public GameFrame() { setTitle("Fighting"); setLayout(null); moveStep = 4; width = 629; height = 990; setSize(width, height); setLocationRelativeTo(null); setVisible(true); KeyCapture(); backgroundY = 0; bulletFlag = false; planeY = height - 200; planeX = width / 2; runFlag = true; graphics = getContentPane().getGraphics(); planeImage = Toolkit.getDefaultToolkit().getImage("D:\\Code\\JAVA\\class\\Fighting\\images\\bullet05.png"); bulletImage = Toolkit.getDefaultToolkit().getImage("D:\\Code\\JAVA\\class\\Fighting\\images\\bullet_02.png"); backgroundImage1 = Toolkit.getDefaultToolkit().getImage("D:\\Code\\JAVA\\class\\Fighting\\images\\mapback.png"); backgroundImage2 = Toolkit.getDefaultToolkit().getImage("D:\\Code\\JAVA\\class\\Fighting\\images\\mapback.png"); } public void KeyCapture() { this.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); System.out.println("keyCode: " + keyCode); switch (keyCode) { case KeyEvent.VK_UP: planeY -= moveStep; break; case KeyEvent.VK_DOWN: planeY += moveStep; break; case KeyEvent.VK_LEFT: planeX -= moveStep; break; case KeyEvent.VK_RIGHT: planeX += moveStep; break; case KeyEvent.VK_SPACE: bulletX = planeX; bulletY = planeY; bulletFlag = true; break; case KeyEvent.VK_Q: runFlag = false; break; default: System.out.println("KeyCode error!");break; } } }); } public Boolean Run() { //System.out.println("x: " + planeX + " y:" + planeY); // background graphics.drawImage(backgroundImage2, 0, backgroundY, width, height, this); graphics.drawImage(backgroundImage1, 0, backgroundY - height, width, height, this); // plane graphics.drawImage(planeImage, planeX, planeY, null); //bullet if (bulletFlag) { graphics.drawImage(bulletImage, bulletX, bulletY, null); bulletY -= 2 * moveStep; } backgroundY += 2; return runFlag; }}
来源地址:https://blog.csdn.net/r77683962/article/details/133363763
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341