软件工程—黄金矿工

代码篇(粘贴部分代码),最终代码会在最后一篇博客展出!

package minegame;
import javax.swing.*;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
/**
 * Created by lzc on 4/2/16.
 */
//JFrame
public class GoldMiner extends JFrame{
    Stage stage;
    static boolean tag = true;
//    static final double TIME_STEP = 1.0; //单位事件步长
    static final double PERIOD = 20.0;


    public GoldMiner() throws IOException{
    	setTitle("Gold Miner");
        setSize(800,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        stage = new Stage();
		stage.setFocusable(true);
		stage.requestFocusInWindow();
        stage.addMouseListener(new MouseAdapter() {
                                   @Override
                                   public void mouseClicked(MouseEvent e) {
                                       super.mouseClicked(e);
                                       int x=e.getX(), y=e.getY();
                                       if(stage.stageState==Stage.StageState.MENU){

                                           if(x>340&&x<490 && y>200 && y<250){
                                               try{
                                                   stage.load(0);
                                                   stage.start();
                                               }catch (IOException e1){
                                                   e1.printStackTrace();
                                               }
                                           }else if(x>340&&x<490&& y>280 && y<330){
                                               //游戏设置
                                               stage.setting();
                                           }else if (x > 340&&x < 490 && y > 360 && y < 410){
                                               stage.openOptions();
                                           }
                                           else if (x > 340 && x < 490 && y > 440 && y < 490){
                                               dispose();
                                           }
                                       }else if(stage.stageState== Stage.StageState.GAME_OVER){
                                           int distance = (x-412)*(x-412)+(y-432)*(y-432);
                                           if (x > 10 && x < 81 && y > 10 && y < 50){
                                               stage.openMenu();
                                           }
                                           if(distance<1024){
                                               try{
                                                   stage.load(0);
                                                   stage.start();
                                                   stage.hook = new Hook(stage.width, 180);
                                               }catch (IOException e1){
                                                   e1.printStackTrace();
                                               }
                                           }

                                       }else if (stage.stageState == Stage.StageState.GAME_SETTING){
                                           int distance1 = (x - 365) * (x - 365) + (y - 315) * (y - 315);
                                           int distance2 = (x - 445) * (x - 445) + (y - 315) * (y - 315);
                                           if (x > 10 && x < 81 && y > 10 && y < 50){
                                               stage.openMenu();
                                           }
                                           if (distance1 <= 625){
                                               Stage.setFlag(true);
                                               tag = true;
                                               stage.openMenu();
                                               System.out.println("123");
                                           }
                                           if (distance2 <= 625){
                                               Stage.setFlag(false);
                                               tag = false;
                                               stage.openMenu();
                                           }
                                       }else if (stage.stageState == Stage.StageState.GAME_OPTIONS){
                                           if(x>340&&x<490&& y>280 && y<330){
                                               stage.keyplain();
                                           }else if (x > 340&&x < 490 && y > 360 && y < 410){
                                               stage.aboutGame();
                                           }else if (x > 10 && x < 81 && y > 10 && y < 50){
                                               stage.openMenu();
                                           }
                                       }else if (stage.stageState == Stage.StageState.GAME_MOVE){
                                           if (x > 10 && x < 81 && y > 10 && y < 50){
                                               stage.openOptions();
                                           }
                                       }else if (stage.stageState == Stage.StageState.GAME_EXPLAIN){
                                           if (x > 10 && x < 81 && y > 10 && y < 50){
                                               stage.openOptions();
                                           }
                                       }

                                   }
                               }
        );


        stage.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_SPACE:
                        stage.hook.launch();
                        break;
                    case KeyEvent.VK_P:
                        stage.pause();
                        break;
                    case KeyEvent.VK_ESCAPE:
                        stage.configure();
                        break;
                }
            }
        });

        stage.stageState = Stage.StageState.MENU;
        add(stage);


        //stage.start();
    }

    public static void main(String[] args) throws IOException{
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
			        GoldMiner goldMiner = new GoldMiner();
			        goldMiner.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
    }

}

package minegame;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;

/**
 * Created by czj
 */

class Bomb {
	double x;
	double y;
	double r;
	Stage stage;
	List<Bomb> chainReaction = new ArrayList<Bomb>();
	boolean marked2Explode;

	//炸药
	Bomb(double x, double y, double r, Stage stage) {
		this.x = x;
		this.y = y;
		this.r = r;
		this.stage = stage;
		this.marked2Explode = false;
	}


	//画炸药
	void paint(Graphics g) {
		Image icon = new ImageIcon("res/images/mine_tnt.png").getImage();
		g.drawImage(icon, (int)(x-r), (int)(y-r), 
				(int)(2*r), (int)(2*r), null);
	}

	//爆炸
	void explode(boolean flag) {
		for (int i = 0, len = stage.mineralList.size(); i < len; ++i) {
			if (distance(stage.mineralList.get(i).x, 
					stage.mineralList.get(i).y, x, y) < 5 * r) {
				stage.mineralList.remove(i);
				--len;
				--i;
			}
		}
		
		/*播放声音*/
    	if (flag){
			Thread playSound = new Thread(new SoundPlayer("res/sounds/explosive.wav"));
			playSound.start(); //SoundPlayer定义在Hook.java中
		}


		//移除炸弹
		for (int i=0, len = stage.bombList.size(); i<len; ++i) {
			Bomb testBomb = stage.bombList.get(i);
			if(distance(x,y,testBomb.x, testBomb.y) < (r + testBomb.r)
					&& testBomb.marked2Explode == false) {
				stage.bombList.remove(i);
				len = stage.bombList.size();
				--i;
				testBomb.marked2Explode = true;
				chainReaction.add(testBomb);
				/* 利用一个List来储存要爆的炸弹,目的是避免递归引爆炸弹的时候造成bombList不可预计的变化,
				 * 比如后排元素因为前排的爆炸往前移,而下标i不能准确知道递归引爆后应该从哪里继续检查可爆炸弹,
				 * 从而miss掉一些应该爆掉的炸弹*/
			}
		}
		for (int i=0; i<chainReaction.size(); ++i) {
			chainReaction.get(i).explode(flag);
		}
		chainReaction.clear();
		stage.explodeEffectList.add(new ExplodeEffect(x, y, r));
	}
	private static double distance(double x1, double y1, double x2, double y2){
        return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    }
}


class ExplodeEffect {
	double x;
	double y;
	double r;
	int effectCount;
	
	ExplodeEffect(double x, double y, double r) {
		this.x = x;
		this.y = y;
		this.r = 3 * r;
		this.effectCount = 1;
	}
	
	void paint(Graphics g) {
		Image icon = new ImageIcon("res/images/effect_blast_" 
				+ effectCount + ".png").getImage();
		g.drawImage(icon, (int)(x-r), (int)(y-r), (int)(3*r), (int)(3*r), null);
		++this.effectCount;
	}
	
	boolean isEnd() {
		return effectCount == 6; //一共5张图
	}
}

猜你喜欢

转载自blog.csdn.net/mithzau/article/details/80557809