JAVA-制作飞机大战遇到的问题


这学期java最后让编一个程序,第一次写这么多行的程序,收获很大。
写的第一个博客,以后要经常写,记录自己遇到的问题和解决方法。

问题

1.闪烁

解决方法

1.使用双缓冲,但是没有效果
2. 继承JPanel类,成功

2.子弹不能多发

解决方法

建立子弹数组,由开火函数写在飞机类中,所有画图都调到游戏主窗口类的paint中。

全部代码

1.主菜单

package com.lhyltzj.www;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/*
 * 主菜单,主方法类
 */
public class Welcome extends JFrame{
   
	
	Image background2=GameUtile.getImage("image/menuback.png");
	public Welcome() {
   
		//大小
		this.setSize(800,600);
		//标题
		this.setTitle("雷电");
		//窗口居中
		this.setLocationRelativeTo(null); 
		//可见
		this.setVisible(true);
		
		JButton startGame=new JButton("开始游戏");
		JButton infoGame=new JButton("玩法介绍");
		JButton endGame=new JButton("退出");
		startGame.setBounds(350, 290, 100, 50);
		infoGame.setBounds(350, 350, 100, 50);
		endGame.setBounds(350, 410, 100, 50);

		this.add(startGame);
		this.add(infoGame);
		this.add(endGame);
		this.setLayout(null);		
		
		ImageIcon img1=new ImageIcon("src/image/menuback.png");
        JLabel la3=new JLabel(img1);
	    la3.setBounds(0,0,800,600);
	    this.getLayeredPane().add(la3,new Integer(Integer.MIN_VALUE)); 
	    getContentPane().add(la3);
	    this.setResizable(false);

		
	    
		startGame.addActionListener(new ActionListener() {
   
			
			@Override
			public void actionPerformed(ActionEvent e) {
   
				// TODO Auto-generated method stub
				/*点开始游戏,销毁主菜单窗口
				 * 创建选择模式窗口
				 */
				dispose();
				ChooseModel cm=new ChooseModel();
			}
		});
		
		infoGame.addActionListener(new ActionListener() {
   
			
			@Override
			public void actionPerformed(ActionEvent e) {
   
				// TODO Auto-generated method stub
				/*
				 * 点击玩法介绍,创建玩法介绍窗口
				 */
				InfoFrame infoframe=new InfoFrame();
			}
		});
		
		endGame.addActionListener(new ActionListener() {
   
			
			@Override
			public void actionPerformed(ActionEvent e) {
   
				// TODO Auto-generated method stub
				dispose();
			}
		});
	}
	
	public static void main(String[] args) {
   
		// TODO Auto-generated method stub
		
		Welcome welcome=new Welcome();
		//		GameWindow gw=new GameWindow();
		//		gw.MyWindow();
        //gw.setVisible(true);
        
	}

}

2.模式选择菜单

package com.lhyltzj.www;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/*
 * 模式选择类
 * 
 */
public class ChooseModel extends JFrame{
   

	public ChooseModel(){
   
		
		this.setSize( 500, 400);
		this.setTitle("模式选择");
		this.setVisible(true);
		this.setLocationRelativeTo(null); 
		this.setLayout(null);
		
		
		JButton normal=new JButton("闯关模式");
		JButton endless=new JButton("无尽模式");
		normal.setBounds(200, 100, 100, 50);
		endless.setBounds(200, 200, 100, 50);
		
		this.add(normal);
		this.add(endless);
		
		ImageIcon img1=new ImageIcon("src/image/chooseback.png");
		JLabel la3=new JLabel(img1);
	    la3.setBounds(0,0,700,800);
	    this.getLayeredPane().add(la3,new Integer(Integer.MIN_VALUE)); 
	    getContentPane().add(la3);
	    this.setResizable(false);
		
		this.setLayout(null);
		normal.addActionListener(new ActionListener() {
   
			
			@Override
			public void actionPerformed(ActionEvent e) {
   
				// TODO Auto-generated method stub
				/*
				 * 闯关模式
				 * 传参数false
				 */
				dispose();
				GameWindow gw=new GameWindow();
				gw.MyWindow(false);
			}
		});
		endless.addActionListener(new ActionListener() {
   
			
			@Override
			public void actionPerformed(ActionEvent e) {
   
				// TODO Auto-generated method stub
				/*
				 * 无尽模式
				 * 传参数ture
				 */
				dispose();
				GameWindow gw=new GameWindow();
				gw.MyWindow(true);
			}
		});
		
	}
	
}

3.游戏主窗口

package com.lhyltzj.www;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


/*
 * 游戏运行窗口
 */
public class GameWindow extends JPanel {
   

	//创建全局JFrame对象
	JFrame frame=new JFrame("Thunder");
	
	int j=4;
	//标记游戏失败结束,ture为游戏结束
	public  boolean GAMEOVER;
	//标记游戏胜利结束,ture为游戏结束
	public  boolean VECTORY;
	//判断游戏模式
	boolean model;
	//闯关模式关卡
	int level=1;
	//显示文字延迟
	int timeNum=0;
	//终极技能能量值
	public int power=0;
	//玩家战机初始坐标
	static int x=250;
	static int y=600;
	//窗口大小
	public static final int GAME_WIDTH=600;
	public static final int GAME_HEIGHT=750;
	//分数
	int score=0;
	
//	int enemy_x=110;
//	int enemy_y=-20;
	//子弹集合
	List<Bullet> bulletarr=new ArrayList<Bullet>();
//	List<EnemyBullet> enemybulletarr=new ArrayList<EnemyBullet>();
	
	//敌机,BOSS,血包 集合
	List<Plane> enemyarr=new ArrayList<Plane>();
	//爆炸集合
	List<Bomb> bombs=new ArrayList<Bomb>();
	//创建玩家战机
	Plane myplane=new Plane(x, y,true,Plane.Direction.STOP,this,100);
	//用工具类转化图片资源
	Image image=GameUtile.getImage("image/myplane.png");
	Image background=GameUtile.getImage("image/background.png");
	Image background2=GameUtile.getImage("image/background2.png");
	//背景竖坐标,用于背景图片滚动
	int back_y=-background.getHeight(null)+background.getHeight(null);
	//public GameWindow() {
   


	public void MyWindow(boolean model){
   

		
		this.setSize(GAME_WIDTH,GAME_HEIGHT);
		this.setVisible(true);
		this.model=model;
		//System.out.println("dsdkskdj");
		
		//GameWindow win=new GameWindow();
		//把GameWindow类放到全局frame里
		frame.add(this);
		//win.MyWindow();
			// 设置大小
		frame.setSize(GAME_WIDTH,GAME_HEIGHT);
        //frame.setAlwaysOnTop(true); // 设置其总在最上
		
		// 默认关闭操作
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗体初始位置居中
        frame.setLocationRelativeTo(null);
        //添加键盘监听器
        frame.addKeyListener(new KeyMonitor());
        //可见
        frame.setVisible(true);
        //不可改变大小
        frame.setResizable(false);
        //开启线程
		new Thread(new PaintThread()).start();
		

	public void setLevel() {
   
		//无尽模式
		if(model){
   
			for(int i = 0; i < j; i++) {
   
			this.enemyarr.add(new Plane(300+50*i, -200,false,Plane.Direction.D,this,20));
			
			}
			j++;
		}
		//闯关模式
		else{
   
		switch (level) {
   
		//第一关
		case 1:
//			this.enemyarr.add(new Plane(110,-150,false,Plane.Direction.D,this,1000,true));
			for(int i = 0; i < 2; i++) {
   
			this.enemyarr.add(new Plane(300+50*i, -200,false,Plane.Direction.D,this,20));
		 }
			break;
		//第二关
		case 2:
			this.enemyarr.add(new Plane(10, 30,false,Plane.Direction.D,this,20,false,true));
			
  • 11
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值