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

射击游戏

开发平台:

Java

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