代码说明:
《星际争霸》这个游戏对于网友们来说并不陌生,但是如果要是用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
【编辑推荐】