代碼說明:
《星際爭霸》這個游戲對於網友們來說並不陌生,但是如果要是用Java來編寫出《星際爭霸》不知道大家聽說過沒?下面的就是一位很彪悍的人物用Java寫的《星際爭霸》游戲,而且效果相當逼真。
源代碼附件大家自己下載吧,可以研究研究。導入工程後,src/test目錄下打開TestGameCore.Java可直接運行游戲。
Java代碼(由於代碼比較多,只貼出一部分):
Control.Java
- package core;
- import Java.awt.Color;
- import Java.awt.Component;
- import Java.awt.Graphics;
- import Java.awt.event.InputEvent;
- import Java.awt.event.KeyEvent;
- import Java.awt.event.KeyListener;
- import Java.awt.event.MouseAdapter;
- import Java.awt.event.MouseEvent;
- import Java.awt.event.MouseMotionAdapter;
- import Javax.swing.KeyStroke;
- /**
- * 用戶交互控制
- * @author Administrator
- */
- public class Control {
- private int x, y, dx, dy, width, height;
- private boolean dragged, moveing;
- private Component component;
- private ControlDragListener dragLister = new ControlDragListener();
- private ControlMoveListener moveLister = new ControlMoveListener();
- private ControlKeyListener keyLister = new ControlKeyListener();
- private DragListener dragListener;
- private MoveListener moveListener;
- private LeftPressListener leFTPressListener;
- private RightPressListener rightPressListener;
- private KeyPressListener keyPressListener;
- public static final int LEFT_MOUSE = 1;
- public static final int RIGHT_MOUSE = 3;
- public Control(Component mouseComponent,Component keyComponent) {
- this.component = mouseComponent;
- component.addMouseListener(dragLister);
- component.addMouseMotionListener(moveLister);
- keyComponent.addKeyListener(keyLister);
- }
- public void drag(Graphics g) {
- if (dragged && moveing) {
- g.setColor(Color.red);
- width = Math.abs(dx - x);
- height = Math.abs(dy - y);
- g.drawRect(Math.min(x, dx), Math.min(y, dy), width, height);
- }
- }
- /**
- * 新增鼠標拖放回調
- * @param dragCallBack
- */
- public Control addDragListener(DragListener dragCallBack){
- this.dragListener = dragCallBack;
- return this;
- }
- /**
- * 新增鼠標移動回調
- * @param moveListener
- * @return
- */
- public Control addMoveListener(MoveListener moveListener){
- this.moveListener = moveListener;
- return this;
- }
- public Control addLeftPressListener(LeFTPressListener selectCallBack){
- this.leFTPressListener = selectCallBack;
- return this;
- }
- public Control addRightPressListener(RightPressListener selectCallBack){
- this.rightPressListener = selectCallBack;
- return this;
- }
- public Control addKeyPressListener(KeyPressListener keyPressListener){
- this.keyPressListener = keyPressListener;
- return this;
- }
- /**
- * 內部類,處理鼠標點擊
- */
- private class ControlDragListener extends MouseAdapter {
- public void mousePressed(MouseEvent e) {
- //如果是左鍵
- if(e.getButton()==LEFT_MOUSE){
- x = e.getX();
- y = e.getY();
- dragged = true;
- if(leFTPressListener!=null){
- leFTPressListener.press(x, y);
- }
- //如果是右鍵
- } else if(e.getButton()==RIGHT_MOUSE){
- if(rightPressListener!=null){
- // System.out.println(e.getX()+","+e.getY());
- rightPressListener.press(e.getX(), e.getY());
- }
- }
- }
- public void mouseReleased(MouseEvent e) {
- //如果需要有回調
- if(dragListener!=null&&dragged&&moveing){
- int tx = Math.min(x, dx);
- int ty = Math.min(y, dy);
- int tdx = Math.max(x, dx);
- int tdy = Math.max(y, dy);
- dragListener.drag(tx, ty, tdx, tdy);
- }
- x = 0;
- y = 0;
- dx = 0;
- dy = 0;
- dragged = false;
- moveing = false;
- }
- }
- /**
- * 內部類,處理鼠標移動
- */
- private class ControlMoveListener extends MouseMotionAdapter {
- public void mouseDragged(MouseEvent e) {
- if (dragged) {
- dx = Math.min(e.getX(), component.getWidth());
- dy = Math.min(e.getY(), component.getHeight());
- moveing = true;
- }
- }
- public void mouseMoved(MouseEvent e) {
- if(moveListener!=null){
- moveListener.move(e.getX(), e.getY());
- }
- }
- }
- private class ControlKeyListener implements KeyListener{
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode()==KeyEvent.VK_ESCAPE){
- System.exit(-1);
- }
- if(keyPressListener!=null){
- keyPressListener.press(e.getKeyCode());
- }
- }
- public void keyReleased(KeyEvent e) {
- //System.out.println(e.getKeyCode());
- }
- public void keyTyped(KeyEvent e) {
- KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK);
- }
- }
- /**
- * 內部接口,鼠標拖動回調
- */
- public static interface DragListener{
- public void drag(int x,int y,int dx,int dy);
- }
- /**
- * 內部接口,鼠標移動回調
- */
- public static interface MoveListener{
- public void move(int x,int y);
- }
- /**
- *
- *內部接口,左鍵回調
- */
- public static interface LeFTPressListener{
- public void press(int x,int y);
- }
- /**
- * 內部接口,右鍵回調
- */
- public static interface RightPressListener{
- public void press(int x,int y);
- }
- /**
- *
- * @author jiangyp
- */
- public static interface KeyPressListener{
- public void press(int keyCode);
- }
- }
GameCore.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;
- }
- }
圖片效果:
下載地址:http://down.51cto.com/data/321588