源码获取:博客首页 "资源" 里下载!
功能简介:
星际争霸游戏项目,该项目实现了单人模式和多人合作模式,可记录游戏进度,新建游戏,载入历史记录等功能,多人模式下可以创建一个区,然后邀请玩家加入一起玩
游戏服务页面:
/**
* 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();
}
}
源码获取:博客首页 "资源" 里下载!