Guest User

Untitled

a guest
Feb 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. public class Pong extends JFrame {
  2.  
  3. JPanel Buttons= new JPanel();
  4. JPanel Test = new JPanel();
  5. GameThread gamethread;
  6. JPanel GamePanel = new JPanel();
  7.  
  8. int x = 30; // Anfangskoordinaten Schläger 1
  9. int y = 260;
  10. int Px = 100, Py = 100; // Anfangskoordinaten Ball
  11. int x2 = 570, y2 = 260; // Anfangskoordinaten Schläger 2
  12. int dPx = 10, dPy = 10; // Geschwindigkeit Ball
  13. boolean Spieler1o = false; // Pfeiltaste oben am Anfang Taste nicht gedrückt
  14. boolean Spieler1u = false; // Pfeiltast unten
  15. boolean Spieler2o = false;
  16. boolean Spieler2u = false;
  17.  
  18. static boolean start=false;
  19.  
  20. public Pong() {
  21. JFrame frame = new JFrame();
  22. frame.setSize(800, 800);
  23. frame.setFocusable(true);
  24. frame.setLayout(new FlowLayout());
  25. frame.add(GamePanel,new GamePanel());
  26. frame.add( Buttons,new Buttons());
  27. frame.setVisible(true);
  28.  
  29. }
  30.  
  31. class Buttons extends JPanel implements ActionListener {
  32. JButton button1 = new JButton("Pause");
  33. JButton button2 = new JButton("Start Computer");
  34. JButton button3 = new JButton("Start Multiplayer");
  35. JButton button4 = new JButton("Beenden");
  36.  
  37. Buttons() {
  38. setPreferredSize(new Dimension(100, 100));
  39. Buttons.setBackground(Color.YELLOW);
  40. Buttons.setLayout(new FlowLayout());
  41. button1.addActionListener(this);
  42. button2.addActionListener(this);
  43. button3.addActionListener(this);
  44. button4.addActionListener(this);
  45.  
  46. Buttons.add(button1);
  47. Buttons.add(button2);
  48. Buttons.add(button3);
  49. Buttons.add(button4);
  50. }
  51.  
  52. @Override
  53. public void actionPerformed(ActionEvent e) {
  54. }
  55. }
  56.  
  57. class GamePanel extends JPanel { // draw ball and rackets
  58. GamePanel(){
  59. GamePanel.setPreferredSize(new Dimension(600,600));
  60. GamePanel.setBackground(Color.YELLOW);
  61. gamethread = new GameThread();
  62. gamethread.start();
  63. }
  64.  
  65. public void paint(Graphics gr) { // beiden Schläger und Ball
  66. super.paint(gr);
  67. Graphics2D g = (Graphics2D) gr; // werden gezeichnet
  68. g.setColor(Color.YELLOW);
  69. g.fill(g.getClipBounds());
  70. g.setColor(Color.BLACK);
  71. g.fillRect(x, y, 10, 80); // Ball und Schläger werden gezeichnet
  72. g.fillOval(Px, Py, 30, 30);
  73. g.fillRect(x2, y2, 10, 80);
  74. }
  75. }
  76.  
  77. class GameThread extends Thread implements KeyListener {
  78. GameThread() {
  79. addKeyListener(this);
  80. }
  81.  
  82. public void run() {
  83. while(true) {
  84.  
  85. TastaturEingabe();
  86. bewegeBall();
  87. repaint();
  88. try {
  89. Thread.sleep(50);
  90. System.out.println("Test");
  91. } catch (InterruptedException e) {
  92. return;
  93. }
  94. }
  95. }
  96.  
  97. public void bewegeBall() {
  98. Px = Px + dPx; // die Bewegung
  99. Py = Py + dPy;
  100.  
  101. if (Px < 0) { // Ball prallt von der Wand ab
  102. Px = 0;
  103. dPx = -dPx;
  104. }
  105. if (Py < 0) { // Ball prallt von der Wand ab
  106. Py = 0;
  107. dPy = -dPy;
  108. }
  109. if (Py > 570) { // Ball prallt von der Wand ab
  110. Py = 570;
  111. dPy = -dPy;
  112. }
  113. if (Px > 570) { // Ball prallt von der Wand ab
  114. Px = 570;
  115. dPx = -dPx;
  116. }
  117.  
  118. if (Px <= 40 && Py >= y && Py <= y + 80) { // Hier soll er vom Schläger1 abprallen
  119. Px = 40;
  120. dPx = -dPx;
  121. }
  122.  
  123. if (Px >= 540 && Py >= y2 && Py <= y2 + 80) {
  124. Px = 540;
  125. dPx = -dPx;
  126. dPy = -dPy;
  127. }
  128. }
  129.  
  130. public void TastaturEingabe() {
  131. if (Spieler1o == true) { // Bewegung Schläger1
  132. y -= 12;
  133. }
  134. if (Spieler1u == true) {
  135. y += 12;
  136. }
  137. if (Spieler2o == true) { // Bewegung Schläger2
  138. y2 -= 12;
  139. }
  140. if (Spieler2u == true) {
  141. y2 += 12;
  142. }
  143.  
  144. if (y2 > 520) { // 600-80 wegen Schlägerlänge
  145. y2 = 520; // Damit die Schläger nicht aus dem Bild verschwinden
  146. }
  147. if (y2 < 0) {
  148. y2 = 0;
  149. }
  150. if (y < 0) {
  151. y = 0;
  152. }
  153. if (y > 520) {
  154. y = 520;
  155. }
  156. }
  157.  
  158. @Override
  159. public void keyPressed(KeyEvent e) {
  160. if (e.getKeyCode() == KeyEvent.VK_W) {
  161. Spieler1o = true;
  162. }
  163. if (e.getKeyCode() == KeyEvent.VK_S) {
  164. Spieler1u = true;
  165. }
  166.  
  167. if (e.getKeyCode() == KeyEvent.VK_UP) { // Spieler2 hier wird geguckt ob Taste gedrückt
  168. Spieler2o = true;
  169. }
  170. if (e.getKeyCode() == KeyEvent.VK_DOWN) {
  171. Spieler2u = true;
  172. }
  173. }
  174.  
  175. @Override
  176. public void keyReleased(KeyEvent e) {
  177. if (e.getKeyCode() == KeyEvent.VK_W) {
  178. Spieler1o = false;
  179. }
  180. if (e.getKeyCode() == KeyEvent.VK_S) {
  181. Spieler1u = false;
  182. }
  183. if (e.getKeyCode() == KeyEvent.VK_UP) {
  184. Spieler2o = false;
  185. }
  186. if (e.getKeyCode() == KeyEvent.VK_DOWN) {
  187. Spieler2u = false;
  188. }
  189.  
  190. }
  191.  
  192. @Override
  193. public void keyTyped(KeyEvent e) {
  194. // TODO Auto-generated method stub
  195. }
  196. }
  197.  
  198. public static void main(String[] args) {
  199. new Pong();
  200. }
  201. }
Add Comment
Please, Sign In to add comment