飞机大战 界面版
1.利用java的Swing界面 界面有两个按钮 其一实现开始游戏,暂停游戏,继续游戏三个功能;其二实现结束游戏功能 ,这两个按钮控制飞机的状态,还有键盘上的上下左右键控制飞机的方向。
2.该程序有6个类 BollUI类主要实现界面的初始化,重绘以及按钮按下后动作监听器实现的功能。
BollUI类的代码如下:
package Dream.thread0712v2;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class BollUI extends JFrame{
public static ArrayList<RobotAir> list=new ArrayList<RobotAir>();
public static ArrayList<Bullet> bullet=new ArrayList<Bullet>();
ArrayList<BackGround> bg=new ArrayList<BackGround>();
MyAir air;
public static void main(String[] args) {
BollUI ui=new BollUI();
ui.InitUI();
}
private MyPanel panel;
private JButton overBt;
private JButton startBt;
public void InitUI(){
this.setTitle("多线程游戏");
this.setSize(450,600);
this.setDefaultCloseOperation(3);
this.setLayout(new FlowLayout());
//小球活动区域
panel=new MyPanel();
//小球控制区域
JPanel bottom=creatBottom();
this.add(panel);
this.add(bottom);
this.setVisible(true);
}
public JPanel creatBottom(){
JPanel bottom=new JPanel();
bottom.setPreferredSize(new Dimension(360,50));
startBt=new JButton("开始游戏");
overBt=new JButton("结束游戏");
startBt.setFocusable(false);
overBt.setFocusable(false);
MyListener lis=new MyListener();
startBt.addActionListener(lis);
overBt.addActionListener(lis);
bottom.add(startBt);
bottom.add(overBt);
return bottom;
}
class MyPanel extends JPanel{
public MyPanel(){
this.setBackground(Color.black);
this.setPreferredSize(new Dimension(360,500));
LineBorder bl=new LineBorder(Color.red);
this.setBorder(bl);
}
public void paint(Graphics g){
super.paint(g);
//画背景
for(int i=0;i<bg.size();i++){
BackGround bk=bg.get(i);
bk.draw(g);
}
//画敌人飞机
for(int i=0;i<list.size();i++){
RobotAir ball=list.get(i);
ball.draw(g);
}
//画飞机
if(air!=null)
air.draw(g);
//画子弹
for(int i=0;i<bullet.size();i++){
Bullet bt=bullet.get(i);
if(bt.isOver){
bullet.remove(i);
}else{
bt.draw(g);
bt.draw1(g);
bt.draw2(g);
}
}
}
}
class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
if(str.equals("开始游戏")){
//刷新界面
StartThread st=new StartThread(panel);
st.start();
//背景
for(int i=0;i<100;i++){
BackGround bk=new BackGround();
bk.start();
bg.add(bk);
}
//敌人飞机
for(int i=0;i<5;i++){
RobotAir ball=new RobotAir();
ball.start();
list.add(ball);
}
//玩家飞机
air=new MyAir();
air.start();
//给窗体添加键盘监听器
MyAirListener listener=new MyAirListener(air);
BollUI.this.addKeyListener(listener);
startBt.setText("暂停游戏");
}else if(str.equals("暂停游戏")){
RobotAir.pauseRobot();
startBt.setText("继续游戏");
}else if(str.equals("继续游戏")){
RobotAir.resumeBall();
startBt.setText("暂停游戏");
}else if(str.equals("结束游戏")){
RobotAir.gameOver();
//结束游戏后要清空队列里面的数据
list.clear();
bullet.clear();
startBt.setText("开始游戏");
}
}
}
}
3.MyAir类,RobotAir类,Bullet类,BackGround类用到相同的知识点,线程,要重写线程里面的run方法
以MyAir类为列介绍程序的内容,该类有画玩家飞机的方法(玩家飞机为图片,背景画的是圆,子弹也为圆),飞机移动的方法,飞机自动发子弹的方法等。
相应的程序代码如下:
package Dream.thread0712v2;
import java.awt.Graphics;
import javax.swing.ImageIcon;
/*
* 创建我方飞机
*/
public class MyAir extends Thread{
int x,y;
int i;
int xc=0,yc=0;
int width,height;
ImageIcon myAir;
public boolean left=false,right=false,up=false,down=false;
public MyAir(){
//玩家飞机
myAir=new ImageIcon("myair.jpg");
width=myAir.getIconWidth()/2;
height=myAir.getIconHeight()/2;
x=(360-width)/2;
y=500-height;
}
public void run(){
while(!RobotAir.isStop){
if(!RobotAir.isPause)
i++;
if(i%6==0)
fire();
mov();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//发子弹
public void fire(){
Bullet bt=new Bullet(x+width/2,y);
bt.start();
BollUI.bullet.add(bt);
}
public void mov(){
//左右方向移动
if(left && x>=0){
xc=-2;
}else if(right && x<=(360-width)){
xc=2;
}else{
xc=0;
}
//上下方向移动
if(up && y>=0){
yc=-2;
}else if(down && y<=(500-height)){
yc=2;
}else{
yc=0;
}
x+=xc;
y+=yc;
}
public void draw(Graphics g){
g.drawImage(myAir.getImage(), x, y, width, height, null);
}
}
程序运行效果图:
在飞机发子弹的过程中播放声音可以用
InputStream in = new FileInputStream("xxx.wav");
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
此方法,若出现Access restriction: The type AudioStream is not accessible due to restriction on required library E:\jdk1.7\jre\lib\rt.jar错误要先remove (移除)Build Path里面的JRE再重新导入。
此程序尚未实现敌机碰到子弹消失的情况,以及敌机发子弹的情形。