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

[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;

}

}

[代码运行效果截图]

06de58acefbac82a02d7c6b1cfe06dae.png

[源代码打包下载]

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值