knugi

Untitled

Apr 9th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. package gra;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyAdapter;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseMotionListener;
  12.  
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15. import javax.swing.Timer;
  16.  
  17. @SuppressWarnings("serial")
  18. public class PongGame extends JPanel implements ActionListener, MouseMotionListener, KeyListener{
  19. public PongGame() {
  20. }
  21.  
  22. private static Color kolorPilka = Color.ORANGE;
  23. private static Color kolorTlo = Color.DARK_GRAY;
  24. private static Color kolorPaleta = Color.RED;
  25.  
  26. private boolean start = false;
  27.  
  28. private int paletaX = 100;
  29. private int paletaY = 350;
  30.  
  31. private int pilkaX = 0;
  32. private int pilkaY = 0;
  33.  
  34. private int ballYSpeed = 5;
  35. private int ballXSpeed = 10;
  36.  
  37. static PongGame game = new PongGame();
  38. static Timer timer = new Timer(100, game);
  39.  
  40. protected void paintComponent(Graphics g){
  41. super.paintComponent(g);
  42. //tlo
  43. g.setColor(kolorTlo);
  44. g.fillRect(0, 0, 600, 400);
  45.  
  46. //pilka
  47. g.setColor(kolorPilka);
  48. g.fillOval(pilkaX, pilkaY, 15, 15);
  49.  
  50. //paletka
  51. g.setColor(kolorPaleta);
  52. g.fillRect(paletaX, paletaY, 150, 15);
  53.  
  54. }
  55. public static void main(String args[]){
  56.  
  57. JFrame jf = new JFrame();
  58. jf.setTitle("PongGame");
  59. jf.setSize(600, 400);
  60. jf.setVisible(true);
  61. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62. jf.setResizable(false);
  63. jf.getContentPane().add(game);
  64.  
  65. jf.addMouseMotionListener(game);
  66. jf.addKeyListener(game);
  67. jf.getContentPane().addKeyListener(new KeyAdapter() {
  68. @Override
  69. public void keyTyped(KeyEvent e) {
  70.  
  71. }
  72. });
  73. }
  74. //NIE UZYWANE
  75. public void mouseDragged(MouseEvent e) {}
  76.  
  77.  
  78. public void actionPerformed(ActionEvent e) {
  79. pilkaX += ballXSpeed;
  80. pilkaY += ballYSpeed;
  81. if(pilkaX >= paletaX && pilkaX <= paletaX + 150 && pilkaY >= 400-60){
  82. ballYSpeed = -7;
  83. }
  84. if(pilkaY <= 0){
  85. ballYSpeed = 7;
  86. }
  87. if(pilkaX >= 600-20){
  88. ballXSpeed = -5;
  89. }
  90. if(pilkaX <= 0){
  91. ballXSpeed = 5;
  92. }
  93. if(pilkaY > 400-60){
  94. System.out.println("FAILED");
  95. timer.stop();
  96. //okno
  97. JFrame error = new JFrame("Przegrales!");
  98. error.setTitle("PRZEGRALES!");
  99. error.setVisible(true);
  100. error.setSize(300, 200);
  101. error.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  102. }
  103. repaint();
  104. }
  105.  
  106. public void mouseMoved(MouseEvent e) {
  107. paletaX = e.getX()-75;
  108. repaint();
  109.  
  110. }
  111. public void keyPressed(KeyEvent e) {
  112. System.out.println(e.getKeyChar());
  113. if(e.getKeyChar()==KeyEvent.VK_ENTER){
  114. if(!start){
  115. start = true;
  116. timer.start();
  117. }
  118.  
  119. }
  120. }
  121.  
  122. public void keyReleased(KeyEvent arg0) {}
  123.  
  124. public void keyTyped(KeyEvent arg0) {}
  125.  
  126.  
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment