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.
