Pinball Game in Java

Pinball is a classic arcade game that has been around since the 1930s. It involves the player using flippers to hit a ball around a table aiming to score points by hitting various targets and obstacles. A sample GIF is given below to get an idea about what we are going to do in this article.

 

Implementation

Java




package abc;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
public class a extends JPanel implements ActionListener {
  
    private Timer timer;
    private int ballX, ballY;
    private int paddleX;
    private int dx, dy;
  
    public a() {
        
        setBackground(Color.BLACK);
        setPreferredSize(new Dimension(600, 400));
        setFocusable(true);
  
        ballX = 200;
        ballY = 200;
        paddleX = 250;
        dx = 3;
        dy = 3;
  
        timer = new Timer(10, this);
        timer.start();
  
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    paddleX -= 10;
                    if (paddleX < 0)
                        paddleX = 0;
                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    paddleX += 10;
                    if (paddleX > getWidth() - 100)
                        paddleX = getWidth() - 100;
                }
            }
        });
    }
  
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.fillOval(ballX, ballY, 20, 20);
        g.fillRect(paddleX, getHeight() - 20, 100, 10);
    }
  
    public void actionPerformed(ActionEvent e) {
        
        ballX += dx;
        ballY += dy;
  
        if (ballX < 0 || ballX > getWidth() - 20)
            dx = -dx;
  
        if (ballY < 0 || ballY > getHeight() - 20)
            dy = -dy;
  
        if (ballY > getHeight() - 30 && ballX >= paddleX && ballX <= paddleX + 100)
            dy = -dy;
  
        repaint();
    }
  
    public static void main(String[] args) {
        JFrame frame = new JFrame("Pinball");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new a());
        frame.pack();
        frame.setVisible(true);
    }
}


Output

 

Code Explanation

  • Pinball class extends the JFrame class and implements the ActionListener interface which allows it to handle events.
  • The timer object is created to update the game every 10 milliseconds.
  • The ballX, ballY, and ballSize variables are used to keep track of the ball’s position and size.
  • The ballXdir and ballYdir variables are used to control the ball’s direction and speed.
  • The paddleX, paddleY, paddleWidth, and paddleHeight variables are used to keep track of the paddle’s position and size.
  • The Pinball constructor sets the size of the window sets the title of the game starts the timer
  •  adds a key listener to handle input, and sets the window to be visible.
  • The paint method is called whenever the window needs to be redrawn. It draws the ball and paddle on the screen.