Crenox

Pong Game Code

May 6th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. // By: Sammy Samkough
  2. // This is a pong game. Still a work in progress.
  3.  
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7.  
  8. public class Moving extends JApplet implements KeyListener
  9. {
  10. // Declaring all of the variables.
  11. Point AppletD = new Point(1000, 600),
  12. SquareP = new Point(980, 250),
  13. SquareD = new Point(20, 150),
  14. SquareP2 = new Point(00, 250),
  15. SquareD2 = new Point(20, 150),
  16. BallP = new Point(450, 300),
  17. BallD = new Point(50, 50);
  18.  
  19. int SquareSpeed = 25;
  20.  
  21. // Setting the size of the applet and adding the keys.
  22. public void init()
  23. {
  24. setSize((int)AppletD.getX(), (int)AppletD.getY());
  25.  
  26. setFocusable(true);
  27. addKeyListener(this);
  28. }
  29.  
  30. // Painting the squares and the balls.
  31. public void paint(Graphics g)
  32. {
  33. g.clearRect(0, 0, getWidth(), getHeight());
  34. // First square.
  35. g.setColor(Color.black);
  36. g.fillRect((int)SquareP.getX(), (int)SquareP.getY(), (int)SquareD.getX(), (int)SquareD.getY());
  37. // 2nd square.
  38. g.setColor(Color.black);
  39. g.fillRect((int)SquareP2.getX(), (int)SquareP2.getY(), (int)SquareD2.getX(), (int)SquareD2.getY());
  40. // The ball.
  41. g.setColor(Color.black);
  42. g.fillOval((int)BallP.getX(), (int)BallP.getY(), (int)BallD.getX(), (int)BallD.getY());
  43. }
  44.  
  45. public void keyTyped(KeyEvent e){}
  46.  
  47. // Setting the keys for the squares.
  48. public void keyPressed(KeyEvent e)
  49. {
  50. if(e.getKeyCode() == KeyEvent.VK_UP)
  51. {
  52. SquareP = new Point ((int)SquareP.getX(), (int)SquareP.getY() - SquareSpeed);
  53.  
  54. if(SquareP.getY() <= 0)
  55. {
  56. SquareP = new Point(SquareP.x, 0);
  57. }
  58. }
  59.  
  60. if(e.getKeyCode() == KeyEvent.VK_DOWN)
  61. {
  62. SquareP = new Point ((int)SquareP.getX(), (int)SquareP.getY() + SquareSpeed);
  63.  
  64. if(SquareP.y + SquareD.y >= AppletD.y)
  65. {
  66. SquareP = new Point(SquareP.x, AppletD.y - SquareD.y);
  67. }
  68. }
  69.  
  70. if(e.getKeyCode() == KeyEvent.VK_W)
  71. {
  72. SquareP2 = new Point ((int)SquareP2.getX(), (int)SquareP2.getY() - SquareSpeed);
  73. if(SquareP2.getY() <= 0)
  74. {
  75. SquareP2 = new Point(SquareP2.x, 0);
  76. }
  77. }
  78.  
  79. if(e.getKeyCode() == KeyEvent.VK_S)
  80. {
  81. SquareP2 = new Point ((int)SquareP2.getX(), (int)SquareP2.getY() + SquareSpeed);
  82. if(SquareP2.y + SquareD2.y >= AppletD.y)
  83. {
  84. SquareP2 = new Point(SquareP2.x, AppletD.y - SquareD2.y);
  85. }
  86. }
  87.  
  88. repaint();
  89. }
  90.  
  91. public void keyReleased(KeyEvent e){}
  92. }
Advertisement
Add Comment
Please, Sign In to add comment