Java项目:星际争霸游戏(java+swing+awt界面编程+IO输入输出流+socket+udp网络通信)

源码获取:博客首页 "资源" 里下载!

功能简介:

星际争霸游戏项目,该项目实现了单人模式和多人合作模式,可记录游戏进度,新建游戏,载入历史记录等功能,多人模式下可以创建一个区,然后邀请玩家加入一起玩

 

游戏服务页面:

/**
 * Simple abstract class used for testing. Subclasses should implement the
 * draw() method.
 */
public abstract class GameCore extends JFrame {

	protected static final int FONT_SIZE = 10;

	private boolean isRunning;

	protected JFrame window;

	public void stop() {

	}

	/**
	 * Calls init() and gameLoop()
	 */
	public void run() {
		init();
		gameLoop();
	}

	/**
	 * Sets full screen mode and initiates and objects.
	 */
	public void init() {

		setUndecorated(true);
		setTitle("JStarCraft");
		setIconImage(ResourceManager.loadImage("title.png"));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(800, 600);
		setVisible(true);
		setIgnoreRepaint(true);
		setResizable(false);
		setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE));
		setBackground(Color.black);
		setForeground(Color.white);
		createBufferStrategy(2);
		isRunning = true;
		setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
				ResourceManager.loadImage("cur.png"), new Point(0, 0), "cur"));
		window = getWindow();
		NullRepaintManager.install();
		window.setLayout(null);
		Container contentPane = getWindow().getContentPane();
		((JComponent) contentPane).setOpaque(false);

	}

	/**
	 * Runs through the game loop until stop() is called.
	 */
	public void gameLoop() {

		BufferStrategy strategy = getBufferStrategy();
		long startTime = System.currentTimeMillis();
		long currTime = startTime;
		while (isRunning) {

			long elapsedTime = System.currentTimeMillis() - currTime;
			currTime += elapsedTime;

			// update
			update(elapsedTime);

			// draw the screen
			Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
					RenderingHints.VALUE_TEXT_ANTIALIAS_ON);		
			// g.drawImage(ResourceManager.loadImage("background3.jpg"), 0, 33,
			// null);
			draw(g);
			g.dispose();

			if (!strategy.contentsLost()) {
				strategy.show();
			}

			// take a nap
			try {
				Thread.sleep(5);
			} catch (InterruptedException ex) {
			}
		}
	}

	/**
	 * Updates the state of the game/animation based on the amount of elapsed
	 * time that has passed.
	 */
	public void update(long elapsedTime) {
		// do nothing
	}

	/**
	 * Draws to the screen. Subclasses must override this method.
	 */
	public abstract void draw(Graphics2D g);

	public JFrame getWindow() {
		return this;
	}
}

游戏核心:

/**
    Simple abstract class used for testing. Subclasses should
    implement the draw() method.
*/
public abstract class FullGameCore {

    protected static final int FONT_SIZE = 24;

    private static final DisplayMode POSSIBLE_MODES[] = {
        new DisplayMode(800, 600, 16, 0),
        new DisplayMode(800, 600, 32, 0),
        new DisplayMode(800, 600, 24, 0),
        new DisplayMode(640, 480, 16, 0),
        new DisplayMode(640, 480, 32, 0),
        new DisplayMode(640, 480, 24, 0),
        new DisplayMode(1024, 768, 16, 0),
        new DisplayMode(1024, 768, 32, 0),
        new DisplayMode(1024, 768, 24, 0),
    };

    private boolean isRunning;
    protected ScreenManager screen;

    /**
        Signals the game loop that it's time to quit
    */
    public void stop() {
        isRunning = false;
    }


    /**
        Calls init() and gameLoop()
    */
    public void run() {
        try {
            init();
            gameLoop();
        }
        finally {
            screen.restoreScreen();
            lazilyExit();
        }
    }


    /**
        Exits the VM from a daemon thread. The daemon thread waits
        2 seconds then calls System.exit(0). Since the VM should
        exit when only daemon threads are running, this makes sure
        System.exit(0) is only called if neccesary. It's neccesary
        if the Java Sound system is running.
    */
    public void lazilyExit() {
        Thread thread = new Thread() {
            public void run() {
                try {
                    Thread.sleep(2000);
                }
                catch (InterruptedException ex) { }
                System.exit(0);
            }
        };
        thread.setDaemon(true);
        thread.start();
    }


    /**
        Sets full screen mode and initiates and objects.
    */
    public void init() {
    	
        screen = new ScreenManager();
        DisplayMode displayMode = screen.findFirstCompatibleMode(POSSIBLE_MODES);
        screen.setFullScreen(displayMode);
        JFrame frame = screen.getFullScreenWindow();
        frame.setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE));
        frame.setBackground(Color.white);
        frame.setForeground(Color.white);
        frame.setTitle("JStarCraft");
        frame.setIconImage(ResourceManager.loadImage("title.png"));
        isRunning = true;
        frame.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
				ResourceManager.loadImage("cur.png"), new Point(0, 0), "cur"));
    	NullRepaintManager.install();
		frame.setLayout(null);
		((JComponent) frame.getContentPane()).setOpaque(false);
		
    }



    /**
        Runs through the game loop until stop() is called.
    */
    public void gameLoop() {
        long startTime = System.currentTimeMillis();
        long currTime = startTime;

        while (isRunning) {
            long elapsedTime =
                System.currentTimeMillis() - currTime;
            currTime += elapsedTime;

            // update
            update(elapsedTime);

            // draw the screen
            Graphics2D g = screen.getGraphics();
            draw(g);
            g.dispose();
            screen.update();

//             don't take a nap! run as fast as possible
            try {
                Thread.sleep(5);
            }
            catch (InterruptedException ex) { }
        }
    }


    /**
        Updates the state of the game/animation based on the
        amount of elapsed time that has passed.
    */
    public void update(long elapsedTime) {
        // do nothing
    }


    /**
        Draws to the screen. Subclasses must override this
        method.
    */
    public abstract void draw(Graphics2D g);
    
    public int getHeight(){
    	
    	return  screen.getFullScreenWindow().getHeight();
    
    }
    
    public int getWidth(){
    	
    	return  screen.getFullScreenWindow().getWidth();
    	
    }
    
   public JFrame getWindow(){
	   return screen.getFullScreenWindow();
   }
}

源码获取:博客首页 "资源" 里下载!

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq1334611189

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值