Pong Game

Kategori: Java , 02 Ağustos 2019 , JanFranco


Pratik olması açısından yaptığım bir Pong uygulamasını paylaşmak istedim. Uygulamayı basit tuttum. Ok tuşları ile yönettiğimiz bir çubuk ve rakip çubuk var. Rakip çubuğun y koordinatı, topun y koordinatı ile aynı. Bu nedenle rakip asla kaybetmiyor. Amaç burada kullanıcının ne kadar dayandığıdır. Uygulama geliştirilebilir tabi ki, biraz fazla basit tuttum:
Pong Game

Kodu da paylaşayım. Ball.java:


import java.awt.Color;

public class Ball {

	private int x, y, size, dirX, dirY;
	private Color color;
	
	public Ball(int x, int y, int size, int dirX, int dirY, Color color) {
		this.x = x;
		this.y = y;
		this.size = size;
		this.dirX = dirX;
		this.dirY = dirY;
		this.color = color;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
	public int getSize() {
		return size;
	}
	
	public void setSize(int size) {
		this.size = size;
	}
	
	public int getDirX() {
		return dirX;
	}
	
	public void setDirX(int dirX) {
		this.dirX = dirX;
	}
	
	public int getDirY() {
		return dirY;
	}
	
	public void setDirY(int dirY) {
		this.dirY = dirY;
	}
	
	public Color getColor() {
		return color;
	}
	
	public void setColor(Color color) {
		this.color = color;
	}
	
}
Rod.java:


import java.awt.Color;

public class Rod {

	private Color color;
	private int x, y, width, height, dir;
	
	public Rod(int x, int y, int width, int height, int dir, Color color) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.dir = dir;
		this.color = color;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}
	
	public int getDir() {
		return dir;
	}
	
	public void setDir(int dir) {
		this.dir = dir;
	}
	
}
Ana pencere, Board.java:


import java.awt.EventQueue;
import javax.swing.JFrame;

public class Board extends JFrame {

	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Board frame = new Board();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Board() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 780, 650);
		Panel contentPane = new Panel();
		setContentPane(contentPane);
	}

}
Panel, oyunun kodları, Panel.java:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.JPanel;

public class Panel extends JPanel implements KeyListener, ActionListener{

	private Timer timer = new Timer(5, this);
	private static final long serialVersionUID = 1L;
	private Ball ball = new Ball(390, 300, 20, 2, 5, Color.blue);
	private Rod userRod = new Rod(20, 300, 10, 100, 10, Color.white);
	private Rod enemyRod = new Rod(750, 300, 5, 100, 6, Color.white);
	
	public Panel() {
		
		timer.start();
		
		setBackground(Color.BLACK);
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(null);
		
		requestFocus();
		addKeyListener(this);
		setFocusable(true);
		setFocusTraversalKeysEnabled(false);
	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(ball.getColor());
		g.fillOval(ball.getX(), ball.getY(), ball.getSize(), ball.getSize());
		g.setColor(userRod.getColor());
		g.fillRect(userRod.getX(), userRod.getY(), userRod.getWidth(), userRod.getHeight());
		g.setColor(enemyRod.getColor());
		g.fillRect(enemyRod.getX(), enemyRod.getY(), enemyRod.getWidth(), enemyRod.getHeight());
	    
	    if(control()) {
	    	timer.stop();
	    	System.exit(0);
	    }
	}
	
	public boolean control() {
		
		if(ball.getX() < 0 || ball.getX() > 750) {
			return true;
		}
		return false;
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		ball.setX(ball.getX() + ball.getDirX());
		ball.setY(ball.getY() + ball.getDirY());
		enemyRod.setY(ball.getY());
		
		if(ball.getY() < 0 || ball.getY() > 600) {
			ball.setDirY(ball.getDirY() * -1);
			enemyRod.setDir(enemyRod.getDir() * -1);
		}
		if(new Rectangle(userRod.getX(), userRod.getY(), userRod.getWidth(), userRod.getHeight())
				.intersects(new Rectangle(ball.getX(), ball.getY(), ball.getSize(), ball.getSize()))) {
			ball.setDirX(ball.getDirX() * -1);
		}
		if(new Rectangle(enemyRod.getX(), enemyRod.getY(), enemyRod.getWidth(), enemyRod.getHeight())
				.intersects(new Rectangle(ball.getX(), ball.getY(), ball.getSize(), ball.getSize()))){
			ball.setDirX(ball.getDirX() * -1);
		}
		
		repaint();
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
		int code = e.getKeyCode();
		int userDir = userRod.getDir();
		int userY = userRod.getY();
		
		if (code == KeyEvent.VK_UP) {
			if(userY > 0) {
				userRod.setY(userY - userDir);
			}
		}
		
		if (code == KeyEvent.VK_DOWN) {
			if(userY < 520) {
				userRod.setY(userY + userDir);
			}
		}
		
		repaint();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
Github link: https://github.com/janFrancoo/Java-Projects/tree/master/Pong%20Game


Sonraki Yazı: Snake Game in Java
Yorumlar

Henüz bir yorum bulunmuyor.
Yorum bırakın