程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 代碼分享:用Java開發的《星際爭霸》游戲

代碼分享:用Java開發的《星際爭霸》游戲

編輯:關於JAVA

代碼說明:

《星際爭霸》這個游戲對於網友們來說並不陌生,但是如果要是用Java來編寫出《星際爭霸》不知道大家聽說過沒?下面的就是一位很彪悍的人物用Java寫的《星際爭霸》游戲,而且效果相當逼真。

源代碼附件大家自己下載吧,可以研究研究。導入工程後,src/test目錄下打開TestGameCore.Java可直接運行游戲。


Java代碼(由於代碼比較多,只貼出一部分):

Control.Java

  1. package core;
  2. import Java.awt.Color;
  3. import Java.awt.Component;
  4. import Java.awt.Graphics;
  5. import Java.awt.event.InputEvent;
  6. import Java.awt.event.KeyEvent;
  7. import Java.awt.event.KeyListener;
  8. import Java.awt.event.MouseAdapter;
  9. import Java.awt.event.MouseEvent;
  10. import Java.awt.event.MouseMotionAdapter;
  11. import Javax.swing.KeyStroke;
  12. /**
  13. * 用戶交互控制
  14. * @author Administrator
  15. */
  16. public class Control {
  17. private int x, y, dx, dy, width, height;
  18. private boolean dragged, moveing;
  19. private Component component;
  20. private ControlDragListener dragLister = new ControlDragListener();
  21. private ControlMoveListener moveLister = new ControlMoveListener();
  22. private ControlKeyListener keyLister = new ControlKeyListener();
  23. private DragListener dragListener;
  24. private MoveListener moveListener;
  25. private LeftPressListener leFTPressListener;
  26. private RightPressListener rightPressListener;
  27. private KeyPressListener keyPressListener;
  28. public static final int LEFT_MOUSE = 1;
  29. public static final int RIGHT_MOUSE = 3;
  30. public Control(Component mouseComponent,Component keyComponent) {
  31. this.component = mouseComponent;
  32. component.addMouseListener(dragLister);
  33. component.addMouseMotionListener(moveLister);
  34. keyComponent.addKeyListener(keyLister);
  35. }
  36. public void drag(Graphics g) {
  37. if (dragged && moveing) {
  38. g.setColor(Color.red);
  39. width = Math.abs(dx - x);
  40. height = Math.abs(dy - y);
  41. g.drawRect(Math.min(x, dx), Math.min(y, dy), width, height);
  42. }
  43. }
  44. /**
  45. * 新增鼠標拖放回調
  46. * @param dragCallBack
  47. */
  48. public Control addDragListener(DragListener dragCallBack){
  49. this.dragListener = dragCallBack;
  50. return this;
  51. }
  52. /**
  53. * 新增鼠標移動回調
  54. * @param moveListener
  55. * @return
  56. */
  57. public Control addMoveListener(MoveListener moveListener){
  58. this.moveListener = moveListener;
  59. return this;
  60. }
  61. public Control addLeftPressListener(LeFTPressListener selectCallBack){
  62. this.leFTPressListener = selectCallBack;
  63. return this;
  64. }
  65. public Control addRightPressListener(RightPressListener selectCallBack){
  66. this.rightPressListener = selectCallBack;
  67. return this;
  68. }
  69. public Control addKeyPressListener(KeyPressListener keyPressListener){
  70. this.keyPressListener = keyPressListener;
  71. return this;
  72. }
  73. /**
  74. * 內部類,處理鼠標點擊
  75. */
  76. private class ControlDragListener extends MouseAdapter {
  77. public void mousePressed(MouseEvent e) {
  78. //如果是左鍵
  79. if(e.getButton()==LEFT_MOUSE){
  80. x = e.getX();
  81. y = e.getY();
  82. dragged = true;
  83. if(leFTPressListener!=null){
  84. leFTPressListener.press(x, y);
  85. }
  86. //如果是右鍵
  87. } else if(e.getButton()==RIGHT_MOUSE){
  88. if(rightPressListener!=null){
  89. // System.out.println(e.getX()+","+e.getY());
  90. rightPressListener.press(e.getX(), e.getY());
  91. }
  92. }
  93. }
  94. public void mouseReleased(MouseEvent e) {
  95. //如果需要有回調
  96. if(dragListener!=null&&dragged&&moveing){
  97. int tx = Math.min(x, dx);
  98. int ty = Math.min(y, dy);
  99. int tdx = Math.max(x, dx);
  100. int tdy = Math.max(y, dy);
  101. dragListener.drag(tx, ty, tdx, tdy);
  102. }
  103. x = 0;
  104. y = 0;
  105. dx = 0;
  106. dy = 0;
  107. dragged = false;
  108. moveing = false;
  109. }
  110. }
  111. /**
  112. * 內部類,處理鼠標移動
  113. */
  114. private class ControlMoveListener extends MouseMotionAdapter {
  115. public void mouseDragged(MouseEvent e) {
  116. if (dragged) {
  117. dx = Math.min(e.getX(), component.getWidth());
  118. dy = Math.min(e.getY(), component.getHeight());
  119. moveing = true;
  120. }
  121. }
  122. public void mouseMoved(MouseEvent e) {
  123. if(moveListener!=null){
  124. moveListener.move(e.getX(), e.getY());
  125. }
  126. }
  127. }
  128. private class ControlKeyListener implements KeyListener{
  129. public void keyPressed(KeyEvent e) {
  130. if(e.getKeyCode()==KeyEvent.VK_ESCAPE){
  131. System.exit(-1);
  132. }
  133. if(keyPressListener!=null){
  134. keyPressListener.press(e.getKeyCode());
  135. }
  136. }
  137. public void keyReleased(KeyEvent e) {
  138. //System.out.println(e.getKeyCode());
  139. }
  140. public void keyTyped(KeyEvent e) {
  141. KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK);
  142. }
  143. }
  144. /**
  145. * 內部接口,鼠標拖動回調
  146. */
  147. public static interface DragListener{
  148. public void drag(int x,int y,int dx,int dy);
  149. }
  150. /**
  151. * 內部接口,鼠標移動回調
  152. */
  153. public static interface MoveListener{
  154. public void move(int x,int y);
  155. }
  156. /**
  157. *
  158. *內部接口,左鍵回調
  159. */
  160. public static interface LeFTPressListener{
  161. public void press(int x,int y);
  162. }
  163. /**
  164. * 內部接口,右鍵回調
  165. */
  166. public static interface RightPressListener{
  167. public void press(int x,int y);
  168. }
  169. /**
  170. *
  171. * @author jiangyp
  172. */
  173. public static interface KeyPressListener{
  174. public void press(int keyCode);
  175. }
  176. }

GameCore.Java

  1. package core;
  2. import Java.awt.Color;
  3. import Java.awt.Container;
  4. import Java.awt.Font;
  5. import Java.awt.Graphics2D;
  6. import Java.awt.Point;
  7. import Java.awt.RenderingHints;
  8. import Java.awt.Toolkit;
  9. import Java.awt.image.BufferStrategy;
  10. import Javax.swing.JComponent;
  11. import Javax.swing.JFrame;
  12. /**
  13. * Simple abstract class used for testing. Subclasses should implement the
  14. * draw() method.
  15. */
  16. public abstract class GameCore extends JFrame {
  17. protected static final int FONT_SIZE = 10;
  18. private boolean isRunning;
  19. protected JFrame window;
  20. public void stop() {
  21. }
  22. /**
  23. * Calls init() and gameLoop()
  24. */
  25. public void run() {
  26. init();
  27. gameLoop();
  28. }
  29. /**
  30. * Sets full screen mode and initiates and objects.
  31. */
  32. public void init() {
  33. setUndecorated(true);
  34. setTitle("JStarCraft");
  35. setIconImage(ResourceManager.loadImage("title.png"));
  36. setDefaultCloSEOperation(EXIT_ON_CLOSE);
  37. setSize(800, 600);
  38. setVisible(true);
  39. setIgnoreRepaint(true);
  40. setResizable(false);
  41. setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE));
  42. setBackground(Color.black);
  43. setForeground(Color.white);
  44. createBufferStrategy(2);
  45. isRunning = true;
  46. setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
  47. ResourceManager.loadImage("cur.png"), new Point(0, 0), "cur"));
  48. window = getWindow();
  49. NullRepaintManager.install();
  50. window.setLayout(null);
  51. Container contentPane = getWindow().getContentPane();
  52. ((JComponent) contentPane).setOpaque(false);
  53. }
  54. /**
  55. * Runs through the game loop until stop() is called.
  56. */
  57. public void gameLoop() {
  58. BufferStrategy strategy = getBufferStrategy();
  59. long startTime = System.currentTimeMillis();
  60. long currTime = startTime;
  61. while (isRunning) {
  62. long elapsedTime = System.currentTimeMillis() - currTime;
  63. currTime += elapsedTime;
  64. // update
  65. update(elapsedTime);
  66. // draw the screen
  67. Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
  68. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  69. RenderingHints.VALUE_ANTIALIAS_ON);
  70. g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
  71. RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  72. // g.drawImage(ResourceManager.loadImage("background3.jpg"), 0, 33,
  73. // null);
  74. draw(g);
  75. g.dispose();
  76. if (!strategy.contentsLost()) {
  77. strategy.show();
  78. }
  79. // take a nap
  80. try {
  81. Thread.sleep(5);
  82. } catch (InterruptedException ex) {
  83. }
  84. }
  85. }
  86. /**
  87. * Updates the state of the game/animation based on the amount of elapsed
  88. * time that has passed.
  89. */
  90. public void update(long elapsedTime) {
  91. // do nothing
  92. }
  93. /**
  94. * Draws to the screen. Subclasses must override this method.
  95. */
  96. public abstract void draw(Graphics2D g);
  97. public JFrame getWindow() {
  98. return this;
  99. }
  100. }

圖片效果:


下載地址:http://down.51cto.com/data/321588

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved