飞机大战编程过程 java飞机大战小游戏( 三 )


/**
* 遏制铁鸟疏通
*/
public class PlaneController extends KeyAdapter {
FightPlane fightPlane;
public PlaneController(FightPlane fightPlane){
this.fightPlane=fightPlane;
}
@Override
public void keyPressed(KeyEvent e) {
diretion(e.getKeyCode());
}
//遏制铁鸟挪动
public void diretion(int code){
switch(code){
case 37://对应键盘左键
fightPlane.x-=15;
break;
case 38://对应键盘上键
fightPlane.y-=15;
break;
case 39://对应键盘右键
fightPlane.x+=15;
break;
case 40://对应键盘下键
fightPlane.y+=5;
break;
}
}
}
4.utils东西层
4.1铁鸟恒量类
package com.utils;
/**
* 用来设置运用到的恒量类
*/
public class P {
public static int HEIGHT=800,WEIGHT=800;
}
4.2加载图片类
package com.utils;
import javax.swing.*;
import java.awt.*;
import java.util.Objects;
/**
* 加载图片东西类
*/
public class PUtils {
public static Image getImage(String url){
ImageIcon icon=new ImageIcon(PUtils.class.getClassLoader().getResource(url));
return icon.getImage();
}
一:玩耍展现
铁鸟大战小玩耍咱们都玩过,经过挪动铁鸟来打敌机,这边给大师展现一卑劣戏功效:哇哇呜因为gif只能上传5M巨细,以是就不许给大师展现操纵了,即使大师有爱好不妨本人本人做出来再玩哟 。
这内里一切的铁鸟、枪弹、敌机、爆裂功效都不妨随便替代成本人想要的图片 。大师不妨大肆表现 。也不妨创造无敌版 。
比方我不妨把敌机替代成王八:
大师不妨随便表现哦 。
二:玩耍教程
1.View视图层
名目构造:
1.1创造玩耍面板类
package com.view;
import com.controller.PlaneController;
import com.eneity.EnemyPlane;
import com.eneity.FightPlane;
import com.utils.P;
import javax.swing.*;
/**
* 窗口类
*/
public class FrameWindow {
/**
* 结构本领
*/
public FrameWindow() {
launch();
}
/**
* 创造窗口界面
*/
public void launch() {
FightPlane fightPlane = new FightPlane();
PlaneController controller = new PlaneController(fightPlane);//铁鸟遏制类
GamePanel gamePanel = new GamePanel(fightPlane);
JFrame jFrame = new JFrame();
jFrame.add(gamePanel);
jFrame.setTitle(“铁鸟大战”);
jFrame.addKeyListener(controller);//给窗口树立监听事变
jFrame.setSize(P.WEIGHT, P.HEIGHT);//树立窗口巨细
jFrame.setVisible(true);//表露在窗口
jFrame.setResizable(false);//使窗口巨细恒定
jFrame.setLocationRelativeTo(null);//初始化的界面表露的场所,null为实质自符合
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击封闭之后后盾也会封闭
}
}
1.2.创造玩耍实质表露类
package com.view;
import com.eneity.CreateEnemy;
import com.eneity.EnemyPlane;
import com.eneity.FightPlane;
import com.utils.P;
import com.utils.PUtils;
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
【飞机大战编程过程java飞机大战小游戏】import java.util.List;
import java.util.Random;
/**
* 玩耍面板
*/
public class GamePanel extends JPanel {
public static long count = 0;//祛除敌机数目
public static List<EnemyPlane> EnemyPlaneArrayList = new LinkedList<EnemyPlane>();
private FightPlane fightPlane;//战役机
public ImageIcon bz2 = new
ImageIcon(PUtils.getImage(“images/dd2.png”));
//结构本领,启用这个线程
public GamePanel(FightPlane fightPlane) {
this.fightPlane = fightPlane;
new updateGame().start();//连接重画出画布
new CreateEnemy().start();//画出10架敌机
}
boolean flage = true;
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, P.WEIGHT, P.HEIGHT);
g.setColor(Color.red);
g.drawString(“杀龟数目:” + count, 650, 50);
//画出战役机
if (fightPlane != null) {
fightPlane.drawMe(g);
}
//画出敌机
for (int i = 0; i < EnemyPlaneArrayList.size(); i++) {
if (EnemyPlaneArrayList.get(i).flat == 1) {
int x = EnemyPlaneArrayList.get(i).x;
int y = EnemyPlaneArrayList.get(i).y;
g.drawImage(bz2.getImage(), x, y, 150, 150, null);
EnemyPlaneArrayList.get(i).flat–;
GamePanel.count++;
System.out.println(count);
if (EnemyPlaneArrayList.get(i).flat == -3) {

推荐阅读