Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Font;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.util.Random;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- public class PongPanel extends JPanel implements ActionListener, KeyListener {
- private Game game;
- private boolean running = true;
- private boolean upPressed = false, downPressed = false, wPressed = false, sPressed = false;
- private int ballX = 400, ballY = 200;
- private int diameter = 20;
- private int ballDeltaX = -5, ballDeltaY = 5;
- private int playerOneX = 25, playerOneY = 220;
- private int playerOneWidth = 20, playerOneHeight = 60;
- private int playerTwoX = 750, playerTwoY = 220;
- private int playerTwoWidth = 20, playerTwoHeight = 60;
- private int paddleSpeed = 10;
- Random random = new Random();
- private final float hue = random.nextFloat();
- private final float saturation = 0.9f;
- private float luminance = 1.0f;
- private int deaths;
- private int rallyScore = 0;
- private int rallyInterval = 5;
- private long currentTime = System.currentTimeMillis();
- public static Thread thread;
- private int[] Colors = new int[getWidth() * getHeight()];
- public PongPanel() {
- Random rand = new Random();
- setBackground(new Color(rand.nextInt(0xFF4AF6)));
- setFocusable(true);
- addKeyListener(this);
- Timer timer = new Timer(1000 / 60, this);
- timer.start();
- }
- public void actionPerformed(ActionEvent e) {
- step();
- }
- public void step() {
- if (upPressed) {
- if (playerTwoY - paddleSpeed > 0) {
- playerTwoY -= paddleSpeed;
- }
- }
- if (downPressed) {
- if (playerTwoY + paddleSpeed + playerTwoHeight < getHeight()) {
- playerTwoY += paddleSpeed;
- }
- }
- if (wPressed) {
- if (playerOneY - paddleSpeed > 0) {
- playerOneY -= paddleSpeed;
- }
- }
- if (sPressed) {
- if (playerOneY + paddleSpeed + playerOneHeight < getHeight()) {
- playerOneY += paddleSpeed;
- }
- }
- int nextBallLeft = ballX + ballDeltaX;
- int nextBallRight = ballX + diameter + ballDeltaX;
- int nextBallTop = ballY + ballDeltaY;
- int nextBallBottom = ballY + diameter + ballDeltaY;
- int playerOneRight = playerOneX + playerOneWidth;
- int playerOneTop = playerOneY;
- int playerOneBottom = playerOneY + playerOneHeight;
- float playerTwoLeft = playerTwoX;
- float playerTwoTop = playerTwoY;
- float playerTwoBottom = playerTwoY + playerTwoHeight;
- if (nextBallTop < 0 || nextBallBottom > getHeight()) {
- ballDeltaY *= -1;
- }
- if (nextBallLeft < playerOneRight) {
- if (nextBallTop > playerOneBottom || nextBallBottom < playerOneTop) {
- System.out.println("You died. Your rally was " + rallyScore + ".");
- deaths++;
- ballX = 400;
- ballY = 200;
- } else {
- rallyScore++;
- ballDeltaX *= -1;
- }
- }
- if (nextBallRight > playerTwoLeft) {
- if (nextBallTop > playerTwoBottom || nextBallBottom < playerTwoTop) {
- System.out.println("You died. Your rally was " + rallyScore + ".");
- deaths++;
- ballX = 400;
- ballY = 200;
- } else {
- rallyScore++;
- ballDeltaX *= -1;
- }
- }
- ballX += ballDeltaX;
- ballY += ballDeltaY;
- if (rallyScore % rallyInterval == 1) {
- ballDeltaX = ballDeltaX - 2;
- ballDeltaY = ballDeltaY + 2;
- paddleSpeed = paddleSpeed + 2;
- }
- repaint();
- }
- public synchronized void Score() {
- }
- public void RandomColor() {
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Random rand = new Random();
- for (int lineY = 0; lineY < getHeight(); lineY += 50) {
- g.setColor(new Color(rand.nextInt(0xFF4AF6)));
- g.fillRoundRect(400, lineY, 20, 800, 0, 0);
- }
- g.setColor(Color.WHITE);
- g.fillRoundRect(ballX, ballY, 25, 25, 5, 25);
- for (int tick = 0; tick > getHeight(); tick--) {
- g.setColor(new Color(rand.nextInt(0xFF4AF6)));
- }
- g.setColor(new Color(rand.nextInt(0xFF4AF6)));
- g.fillRoundRect(playerOneX, playerOneY, playerOneWidth,
- playerOneHeight, 50, 5);
- g.fillRoundRect(playerTwoX, playerTwoY, playerTwoWidth,
- playerTwoHeight, 50, 5);
- g.setColor(Color.WHITE);
- g.fillRoundRect(335, 180, 150, 150, 50, 50);
- g.setFont(new Font(Font.DIALOG, Font.BOLD, 150));
- String rally = " " + rallyScore;
- g.setColor(new Color(0xFF2172));
- g.drawString(rally, 328, 310);
- g.dispose();
- }
- public void keyTyped(KeyEvent e) {
- }
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_UP) {
- upPressed = true;
- } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- downPressed = true;
- } else if (e.getKeyCode() == KeyEvent.VK_W) {
- wPressed = true;
- } else if (e.getKeyCode() == KeyEvent.VK_S) {
- sPressed = true;
- }
- }
- public void keyReleased(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_UP) {
- upPressed = false;
- } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- downPressed = false;
- } else if (e.getKeyCode() == KeyEvent.VK_W) {
- wPressed = false;
- } else if (e.getKeyCode() == KeyEvent.VK_S) {
- sPressed = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement