代码和图片放在这个地址了:
https://gitee.com/r77683962/fighting/tree/master
20231003:
1、添加背景音乐;
20231001:
1、现在子弹打中飞机,飞机会挂(打敌人的飞机,或者友方的飞机被打中都会挂);
2、把敌方飞机发子弹的频率调低了下,不然打不过;
20230930:
1、添加了随机添加敌机,这样敌机比较多了(友军发的红色子弹,敌方发射蓝色子弹);
2、把刷新的速度调快了些;
3、子弹的Vector有数量限制(30个);
4、敌机数量也有限制(8个);
运行效果:
20230929:
1、当前友方飞机和敌方飞机支持自动发子弹;
2、友方飞机1可以通过AWSD和F分别控制方向和发射子弹;友方飞机2可以通过上下左右和空格分别控制方向和发射子弹;
3、未监测子弹飞出屏幕外;
4、当前子弹只能竖直向上飞和向下飞;
5、当前敌机只有两辆,不能自动生成;
6、还有很多未开发的;
最新的代码运行,可以有两架飞机,分别通过WASD(方向),F(发子弹);上下左右(控制方向),空格(发子弹)
但是敌人的飞机还没有。
代码写的比较匆忙,不太好。
文章来源:https://uudwc.com/A/dbxNv
文章来源地址https://uudwc.com/A/dbxNv
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;
}
}