ClientControler.java
上传用户:jhzhutan
上传日期:2021-03-28
资源大小:374k
文件大小:5k
源码类别:

射击游戏

开发平台:

Java

  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. //Download:http://www.codefans.net
  4. //this class deal with  the input from the Client view frame
  5. public class ClientControler{
  6. public boolean serverConnected;;
  7. public boolean gameStarted;
  8. public boolean gamePaused;
  9. public ClientView view;
  10. public ClientModel model;
  11. public int helpMessageCount = 1;
  12. public ClientControler(ClientView thisview, ClientModel thismodel){
  13. view = thisview;
  14. model = thismodel;
  15. //handel sendMessage button actions
  16. view.sendMessage.addActionListener(new ActionListener(){
  17. public void actionPerformed(ActionEvent e) {
  18. if(!model.gameStarted){
  19. model.addMessage("还没有和主机端玩家联上, 无法发送对话");
  20. return;
  21. }
  22. if(!view.messageField.getText().equals("")){
  23. model.addMessage("用户端玩家说:" + view.messageField.getText());
  24. model.playerTypedMessage += "e" + view.messageField.getText() + ";";
  25. view.messageField.setText("");
  26. }else{
  27. model.addMessage("对话内容不能为空");
  28. }
  29. }
  30. }
  31. );
  32. //handel connectServer button actions
  33. view.connectServer.addActionListener(new ActionListener(){
  34. public void actionPerformed(ActionEvent e) {
  35. if(!model.serverConnected){
  36. model.serverIP = view.IPfield.getText();
  37. model.t.start();
  38. }
  39. }
  40. }
  41. );
  42. //handel pauseAndResume button actions
  43. view.pauseAndResume.addActionListener(new ActionListener(){
  44. public void actionPerformed(ActionEvent e) {
  45. if(!model.gameOver && model.gameStarted){
  46. model.pausePressed = true;
  47. if(!model.gamePaused){
  48. model.gamePaused = true;
  49. model.addMessage("用户端玩家暂停了游戏");
  50. }else{
  51. model.gamePaused = false;
  52. model.addMessage("用户端玩家取消了暂停");
  53. }
  54. }
  55. }
  56. }
  57. );
  58. //handel help button actions
  59. view.help.addActionListener(new ActionListener(){
  60. public void actionPerformed(ActionEvent e) {
  61. model.addMessage("-------------------------------坦克大战 1.0-----------------------------------");
  62. model.addMessage("帮助: 按 s 键开火,  按键盘的方向键来控制坦克的移动");
  63. model.addMessage("如果按键没有反应请 1. 关闭大写功能; 2. 用 tab键切换 ");
  64. model.addMessage("到控制界面如果您在使用对话界面.");
  65. model.addMessage("--------------------------------------------------------------------------------------");
  66. }
  67. }
  68. );
  69. //handel exit button actions
  70. view.exit.addActionListener(new ActionListener(){
  71. public void actionPerformed(ActionEvent e) {
  72. System.exit(0);
  73. }
  74. }
  75. );
  76. //handel input from the keyboard
  77. view.messageField.addKeyListener( new KeyAdapter(){
  78. public void keyPressed(KeyEvent e){
  79. if(helpMessageCount  > 0){
  80. model.addMessage("提示:用"tab"键可以自由切换于控制界面和对话界面");
  81. model.addMessage("提示:按回车键可以直接发送您的对话");
  82. helpMessageCount--;
  83. }
  84. if(e.getKeyCode()==e.VK_ENTER){
  85. if(!view.messageField.getText().equals("")){
  86. model.addMessage("用户端玩家说:" + view.messageField.getText());
  87. model.playerTypedMessage += "e" + view.messageField.getText() + ";";
  88. view.messageField.setText("");
  89. }else{
  90. model.addMessage("对话内容不能为空");
  91. }
  92. }
  93. }
  94. });
  95. JPanel temp = view.mainPanel;
  96. temp.addKeyListener( new KeyAdapter(){
  97. public void keyPressed(KeyEvent e){
  98. if(e.getKeyCode() == KeyEvent.VK_UP){
  99. model.moveUp = true;
  100. model.moveDown = false;
  101. model.moveLeft = false;
  102. model.moveRight = false;
  103. }
  104. if(e.getKeyCode() == KeyEvent.VK_DOWN ){
  105. model.moveDown = true;
  106. model.moveUp = false;
  107. model.moveLeft = false;
  108. model.moveRight = false;
  109. }
  110. if(e.getKeyCode() == KeyEvent.VK_LEFT ){
  111. model.moveLeft = true;
  112. model.moveUp = false;
  113. model.moveDown = false;
  114. model.moveRight = false;
  115. }
  116. if(e.getKeyCode() == KeyEvent.VK_RIGHT ){
  117. model.moveLeft = false;
  118. model.moveUp = false;
  119. model.moveDown = false;
  120. model.moveRight = true;
  121. }
  122. if(e.getKeyChar() == 's')
  123. model.fire = true;
  124. if(e.getKeyCode()==e.VK_ENTER){
  125. if(!view.messageField.getText().equals("")){
  126. model.addMessage("用户端玩家说:" + view.messageField.getText());
  127. model.playerTypedMessage += "e" + view.messageField.getText() + ";";
  128. view.messageField.setText("");
  129. }
  130. }
  131. if(e.getKeyChar() == 'y' && model.gameOver && !model.clientVoteYes){
  132. model.clientVoteYes = true;
  133. model.addMessage("等待主机端玩家的回应...");
  134. }
  135. if(e.getKeyChar() == 'n'  && model.gameOver)
  136. model.clientVoteNo = true;
  137. }
  138. public void keyReleased(KeyEvent e){
  139. if(e.getKeyCode() == KeyEvent.VK_UP)
  140. model.moveUp = false;
  141. if(e.getKeyCode() == KeyEvent.VK_DOWN )
  142. model.moveDown = false;
  143. if(e.getKeyCode() == KeyEvent.VK_LEFT )
  144. model.moveLeft = false;
  145. if(e.getKeyCode() == KeyEvent.VK_RIGHT )
  146. model.moveRight = false;
  147. if(e.getKeyChar() == 's')
  148. model.fire = false;
  149. }
  150. }
  151. );
  152. }
  153. }