Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // By: Sammy Samkough
- // This is a pong game. Still a work in progress.
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Moving extends JApplet implements KeyListener
- {
- // Declaring all of the variables.
- Point AppletD = new Point(1000, 600),
- SquareP = new Point(980, 250),
- SquareD = new Point(20, 150),
- SquareP2 = new Point(00, 250),
- SquareD2 = new Point(20, 150),
- BallP = new Point(450, 300),
- BallD = new Point(50, 50);
- int SquareSpeed = 25;
- // Setting the size of the applet and adding the keys.
- public void init()
- {
- setSize((int)AppletD.getX(), (int)AppletD.getY());
- setFocusable(true);
- addKeyListener(this);
- }
- // Painting the squares and the balls.
- public void paint(Graphics g)
- {
- g.clearRect(0, 0, getWidth(), getHeight());
- // First square.
- g.setColor(Color.black);
- g.fillRect((int)SquareP.getX(), (int)SquareP.getY(), (int)SquareD.getX(), (int)SquareD.getY());
- // 2nd square.
- g.setColor(Color.black);
- g.fillRect((int)SquareP2.getX(), (int)SquareP2.getY(), (int)SquareD2.getX(), (int)SquareD2.getY());
- // The ball.
- g.setColor(Color.black);
- g.fillOval((int)BallP.getX(), (int)BallP.getY(), (int)BallD.getX(), (int)BallD.getY());
- }
- public void keyTyped(KeyEvent e){}
- // Setting the keys for the squares.
- public void keyPressed(KeyEvent e)
- {
- if(e.getKeyCode() == KeyEvent.VK_UP)
- {
- SquareP = new Point ((int)SquareP.getX(), (int)SquareP.getY() - SquareSpeed);
- if(SquareP.getY() <= 0)
- {
- SquareP = new Point(SquareP.x, 0);
- }
- }
- if(e.getKeyCode() == KeyEvent.VK_DOWN)
- {
- SquareP = new Point ((int)SquareP.getX(), (int)SquareP.getY() + SquareSpeed);
- if(SquareP.y + SquareD.y >= AppletD.y)
- {
- SquareP = new Point(SquareP.x, AppletD.y - SquareD.y);
- }
- }
- if(e.getKeyCode() == KeyEvent.VK_W)
- {
- SquareP2 = new Point ((int)SquareP2.getX(), (int)SquareP2.getY() - SquareSpeed);
- if(SquareP2.getY() <= 0)
- {
- SquareP2 = new Point(SquareP2.x, 0);
- }
- }
- if(e.getKeyCode() == KeyEvent.VK_S)
- {
- SquareP2 = new Point ((int)SquareP2.getX(), (int)SquareP2.getY() + SquareSpeed);
- if(SquareP2.y + SquareD2.y >= AppletD.y)
- {
- SquareP2 = new Point(SquareP2.x, AppletD.y - SquareD2.y);
- }
- }
- repaint();
- }
- public void keyReleased(KeyEvent e){}
- }
Advertisement
Add Comment
Please, Sign In to add comment