用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java版山寨《星际争霸》(可联机多人游戏)

2013-01-28 作者: 小蜜锋举报

[java]代码库

package core;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;

import javax.swing.JComponent;
import javax.swing.JFrame;

/**
 * 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;
	}
}

[代码运行效果截图]


java版山寨《星际争霸》(可联机多人游戏)

[源代码打包下载]




网友评论    (发表评论)

共26 条评论 2/2页 <   1  2

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...