【Java学习笔记】GUI界面之小游戏

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训a>、期待与您交流! -------


为了加深自己对GUI界面编程的理解,查阅了一些资料,自己尝试做了一个小游戏——雷电。花了一天的时间写代码,运行效果不怎么好。但是,至少加深了对绘画操作的理解。


代码主要分为

1>飞机类。分为玩家和敌机

飞机类定义了飞机的速度,飞行方向(向上或向下),飞机的大小

2>Game类。继承自JFream定义游戏窗口等基本信息

3>GamePanel类。继承自JPanel类,实现了Runnable接口,MouseListener,MouseMotionListener以及MouseEvent等鼠标事件

GamePanel类内定义了游戏背景,界面以及射击和得分规则等游戏具体操作。


package exp;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Random;

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

/**
 * 雷电游戏
 * 
 * @author VisionDo
 *
 */
public class Game extends JFrame {

	
	private static final int WIDTH = 400;
	private static final int HEIGHT = 650;

	public static void main(String[] args) {
		Game myGame = new Game();
		myGame.ShowGameWindow();
	}
	
	
	//构造函数,初始化程序界面
	public Game(){
		this.setSize(WIDTH, HEIGHT);
		this.setTitle("雷电");
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	
	//开启游戏线程
	private void ShowGameWindow(){
		GamePanel gamePanel = new GamePanel();
		this.add(gamePanel);
		this.addMouseListener(gamePanel);
		this.addMouseMotionListener(gamePanel);
		Thread th = new Thread(gamePanel);
		th.start();
		
	}

}


/**
 * 
 * Panel类,实现绘制,射击等具体操作
 * 
 */
class GamePanel extends JPanel implements Runnable,MouseListener,MouseMotionListener{

	private static final int EnemyBulletNum = 40;//敌机数量
	private static final int ShootBulletNum = 20;//子弹数
	
	private ArrayList EnemyBullet;//存储敌机
	private ArrayList ShootBullet;//存储子弹对象
	private int Score = 0;//得分
	private int ShootedNum = 0;//已经射击的子弹数
	
	private int helth = 100;//定义生命值
	
	private Bullet Player;//玩家对象
	
	private boolean IsShooting = false;//初始化开火状态为false
	
	private boolean IsWin = false;
	
	//初始化对象
	public GamePanel(){
		this.setBackground(Color.BLACK);//设置游戏背景
		EnemyBullet = new ArrayList();
		ShootBullet = new ArrayList();
		Random rd = new Random();
		int x,y,speed = 15,r = 20;
		Color c;
		for(int i = 0; i this.getHeight()-100){
					b.setPoisition_y(-Bullet_Position);
				}
			}
			
		
			//射击状态判断
			Bullet Enemy,Shoot;
			for(int i = 0; i < EnemyBullet.size(); i++){
				Enemy = EnemyBullet.get(i);
				for(int j = 0; j< ShootBullet.size(); j++){
					Shoot = ShootBullet.get(j);
					if(IsShooted(Shoot,Enemy)){//如果玩家击中敌机
						ShootBullet.remove(j);//移除子弹
						EnemyBullet.remove(i);//移除被击中的敌机
						Score += 10;//得分+10
						i--;//敌机数-1
						j--;//玩家子弹数-1
					}
					
				}
			}
			
			try {
				Thread.sleep(2);//敌机两秒后继续进攻
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if(EnemyBullet.size() == 0){
				IsWin = true;
				break;
			}else{
				ShootedNum++;
			}
			this.repaint();//重新绘制界面
			
		}
		
		
	}
	
	/**
	 * 
	 * @param a 子弹
	 * @param b 子弹
	 * @return 两子弹是否相撞
	 */
	private boolean IsShooted(Bullet a,Bullet b){
		
		int dis_x =//x1-x2
				a.getPoisition_x()+a.getBullet_r()
				- (b.getPoisition_x()+b.getBullet_r());
		int dis_y = //y1-y2
				(int) (a.getPoisition_y()+a.getBullet_r()
		        -(b.getPoisition_y()+b.getBullet_r()));
		
		int dis_r = a.getBullet_r()+b.getBullet_r();
		
		int dis = dis_x*dis_x+dis_y*dis_y;
		
		return dis*dis > dis_r*dis_r;
	} 
	
	
//---------------------鼠标事件----------------------------
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		IsShooting = true;
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		IsShooting = false;
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		int x = e.getX() - Player.getBullet_r();
		int y = (int) (e.getY() - Player.getPoisition_y());
		Player.setPoisition_x(x);
		Player.setPoisition_y(y);
		this.repaint();
		
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		int x = e.getX() - Player.getBullet_r();
		int y = (int) (e.getY() - Player.getPoisition_y() - 5);
		Player.setPoisition_x(x);
		Player.setPoisition_y(y);
		this.repaint();
		
	}
	
}


//飞机类
class Bullet{
	
	private int speed;
	private int poisition_x;
	private double poisition_y;
	private int bullet_r;
	private Color color;
	
	private static final int UP = 0;
	private static final int DOWN = 1;
	
	private static final int ENEMY = 0;
	private static final int SHOOT = 1;
	
	public Bullet(){}
	
	/**
	 * 
	 * @param speed 速度
	 * @param r 半径
	 * @param x 横坐标
	 * @param y 纵坐标
	 * @param c 颜色
	 */
	public Bullet(int speed,int r,int x,int y,Color c){
		this.speed = speed;
		this.bullet_r = r;
		this.poisition_x = x;
		this.poisition_y = y;
		this.color = c;
	}
	
	public void DrawBullet(Graphics g,int Kinds){
		g.setColor(color);
		switch(Kinds){
		
		case ENEMY:
			g.fillOval(poisition_x, (int) poisition_y, bullet_r/2, bullet_r/2);
			break;
		case SHOOT:{
			g.setColor(Color.WHITE);
			g.fillOval(poisition_x, (int) poisition_y, bullet_r/4, bullet_r/4);
			}
			break;
		default:
			break;
		}
		
	}

	
	public void Move(int flag){
		if(flag == UP)
			this.poisition_y--;
		else
			this.poisition_y++;
	}
	
	
	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public int getPoisition_x() {
		return poisition_x;
	}

	public void setPoisition_x(int poisition_x) {
		this.poisition_x = poisition_x;
	}

	public double getPoisition_y() {
		return poisition_y;
	}

	public void setPoisition_y(double bullet_Position) {
		this.poisition_y = bullet_Position;
	}

	public int getBullet_r() {
		return bullet_r;
	}

	public void setBullet_r(int bullet_r) {
		this.bullet_r = bullet_r;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}
	
	
	
}

运行结果:

【Java学习笔记】GUI界面之小游戏_第1张图片【Java学习笔记】GUI界面之小游戏_第2张图片 【Java学习笔记】GUI界面之小游戏_第3张图片

【小结】

程序界面比较粗糙,只是实现了简单的效果。通过这次编程练习,我对如何使用线程以及在线程内执行操作有了较深的认识。

你可能感兴趣的:(初级学习-代码实例)