Advertisement
Guest User

Untitled

a guest
Apr 4th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.18 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Image;
  3. import java.awt.event.MouseEvent;
  4. import java.util.Random;
  5.  
  6. import acm.graphics.GImage;
  7. import acm.graphics.GLabel;
  8. import acm.graphics.GOval;
  9. import acm.graphics.GRect;
  10. import acm.program.GraphicsProgram;
  11.  
  12. /*      TO DO LIST
  13.  *  ------------------
  14.  *  Corner Bounce
  15.  *  Difficulty Level
  16.  *  
  17.  *  
  18.  *  
  19.  */
  20.  
  21. @SuppressWarnings("serial")
  22. public class Pong extends GraphicsProgram {
  23.     private static final double PAUSE = 1000 / 96.0;
  24.     public Random rand = new Random();
  25.     public double mouseY;
  26.  
  27.     // ball
  28.     private static final double BALL_SIZE = 20;
  29.     private static final double SPEED = 5;
  30.     public double dx = SPEED * 1.5;
  31.     public double dy = SPEED;
  32.     public double startX;
  33.     public double startY;
  34.  
  35.     // paddle
  36.     private static double HEIGHT = 150;
  37.     private static double WIDTH = 15;
  38.     private static int COUNTER = 0;
  39.     public static double AI_SPEED = 9.0; // difficulty 0-20
  40.  
  41.     // label
  42.     private float TRANSPARENCY = 0.0f;
  43.     public int AI_SCORE = 0;
  44.     public int PLAYER_SCORE = 0;
  45.  
  46.     public static void main(String[] args) {
  47.         Pong p = new Pong();
  48.         p.run();
  49.     }
  50.  
  51.     public void run() {
  52.         addMouseListeners();
  53.         GLabel counter = new GLabel(String.valueOf(COUNTER));
  54.         GLabel AiScore = new GLabel(String.valueOf(AI_SCORE));
  55.         GLabel PlayerScore = new GLabel(String.valueOf(COUNTER));
  56.         counter.setFont("Impact-600");
  57.         AiScore.setFont("Impact-100");
  58.         PlayerScore.setFont("Impact-100");
  59.         Color labelC = new Color(0, 0.0f, 0.0f, TRANSPARENCY);
  60.         Color scoreC = new Color(0, 0.0f, 0.0f, 0.1f);
  61.         counter.setColor(labelC);
  62.         AiScore.setColor(scoreC);
  63.         PlayerScore.setColor(scoreC);
  64.         counter.setLocation(getWidth() / 2 - counter.getWidth() / 2,
  65.                 getHeight() / 2 + counter.getHeight() / 3.2);
  66.         counter.sendToFront();
  67.         Image texture = getImage(getCodeBase(), "texture.png");
  68.         Image texture2 = getImage(getCodeBase(), "texture2.png");
  69.         Image ballTexture = getImage(getCodeBase(), "ballTexture.png");
  70.         Image greenFlash = getImage(getCodeBase(), "greenFlash.png");
  71.         Image blueFlash = getImage(getCodeBase(), "blueFlash.png");
  72.         GImage image = new GImage(texture);
  73.         GImage image2 = new GImage(texture2);
  74.         GImage image3 = new GImage(ballTexture);
  75.         GImage image4 = new GImage(greenFlash, -250, 0);
  76.         GImage image5 = new GImage(blueFlash, -250, 0);
  77.         add(image);
  78.         add(image2);
  79.         add(image3);
  80.         add(image4);
  81.         add(image5);
  82.         add(counter);
  83.         image.setSize(WIDTH + 1, HEIGHT + 1);
  84.         image2.setSize(WIDTH + 1, HEIGHT + 1);
  85.         image3.setSize(BALL_SIZE, BALL_SIZE);
  86.         image4.setSize(100, 300);
  87.         image5.setSize(100, 300);
  88.         GOval ball = makeBall();
  89.         GRect paddleLeft = makePaddle();
  90.         GRect paddleRight = makePaddle();
  91.         add(paddleLeft);
  92.         add(paddleRight);
  93.         bounce(labelC, AiScore, PlayerScore, counter, ball, paddleLeft,
  94.                 paddleRight, image, image2, image3, image4, image5);
  95.  
  96.     }
  97.  
  98.     public static GRect makePaddle() {
  99.         GRect result = new GRect(0, 0, WIDTH, HEIGHT);
  100.         result.setFilled(true);
  101.         result.setColor(Color.BLACK);
  102.         return result;
  103.     }
  104.  
  105.     public static GOval makeBall() {
  106.         GOval result = new GOval(150, 100, BALL_SIZE, BALL_SIZE);
  107.         result.setFilled(true);
  108.         result.setColor(Color.WHITE);
  109.         return result;
  110.  
  111.     }
  112.  
  113.     public void mouseMoved(MouseEvent e) {
  114.         mouseY = e.getY();
  115.     }
  116.  
  117.     public void bounce(Color labelC, GLabel AiScore, GLabel PlayerScore,
  118.             GLabel counter, GOval ball, GRect paddleLeft, GRect paddleRight,
  119.             GImage image, GImage image2, GImage ballTexture, GImage greenFlash,
  120.             GImage blueFlash) {
  121.         add(ball);
  122.         add(ballTexture);
  123.         add(AiScore);
  124.         add(PlayerScore);
  125.         PlayerScore.setLabel(String.valueOf(PLAYER_SCORE));
  126.         PlayerScore.setLocation(3*WIDTH+10,getHeight()-10);
  127.         AiScore.setLabel(String.valueOf(AI_SCORE));
  128.         AiScore.setLocation(getWidth()-AiScore.getWidth()-3*WIDTH-10,   getHeight()-10);
  129.         startX = rand.nextInt((int) (getWidth() * 0.8))
  130.                 + (int) (0.1 * getWidth()); // zapobiega pojawieniu się piłki po
  131.                                             // lewej stronie lewej paletki
  132.         startY = rand.nextInt(getHeight());
  133.         ball.setLocation(startX, startY);
  134.         image2.setLocation(getWidth() - 3 * WIDTH, startY - HEIGHT / 2);
  135.         paddleRight.setLocation(getWidth() - 3 * WIDTH, startY - HEIGHT / 2);
  136.         image2.sendToFront();
  137.         counter.setLabel(String.valueOf(COUNTER));
  138.         counter.setLocation(getWidth() / 2 - counter.getWidth() / 2,
  139.                 getHeight() / 2 + counter.getHeight() / 3.2);
  140.         dx = SPEED * 1.5;
  141.         dy = SPEED;
  142.         while (true) {
  143.  
  144.             ball.move(dx, dy);
  145.             ballTexture.setLocation(ball.getX(), ball.getY());
  146.             ballTexture.sendToFront();
  147.             if (TRANSPARENCY >= 0.0f)
  148.                 TRANSPARENCY -= TRANSPARENCY / 100f;
  149.  
  150.             labelC = new Color(0, 0.0f, 0.0f, TRANSPARENCY);
  151.             counter.setColor(labelC);
  152.             if (mouseY < getHeight() - HEIGHT) { // Player
  153.                 paddleLeft.setLocation(2 * WIDTH, mouseY);
  154.                 image.setLocation(2 * WIDTH, mouseY);
  155.                 image.sendToFront();
  156.             } else {
  157.                 paddleLeft.setLocation(2 * WIDTH, getHeight() - HEIGHT);
  158.                 image.setLocation(2 * WIDTH, getHeight() - HEIGHT);
  159.                 image.sendToFront();
  160.  
  161.             }
  162.  
  163.             // AI, z którym da się wygrać
  164. /*
  165.             double targetY = ball.getY() + BALL_SIZE / 2;
  166.             if (targetY < getHeight() - HEIGHT / 2 && targetY > HEIGHT / 2) {
  167.                 if (targetY < paddleRight.getY() + HEIGHT / 2) {
  168.                     paddleRight.move(0, -AI_SPEED);
  169.                     image2.move(0, -AI_SPEED);
  170.  
  171.                 } else if (targetY > paddleRight.getY() + HEIGHT / 2) {
  172.                     paddleRight.move(0, AI_SPEED);
  173.                     image2.move(0, AI_SPEED);
  174.  
  175.                 }
  176.             }*/
  177.  
  178.             // AI, z którym nie da się wygrać
  179.             // Zamiennie z kodem powyżej
  180.             // Jeden z algorytmów musi być w komentarzu: /* .: code :. */
  181.  
  182.            
  183.              
  184.             if (ball.getY() < getHeight() - HEIGHT / 2
  185.                     && ball.getY() > HEIGHT / 2) { // AI
  186.                 paddleRight.setLocation(getWidth() - 3 * WIDTH, ball.getY()
  187.                         - HEIGHT / 2);
  188.                 image2.setLocation(getWidth() - 3 * WIDTH, ball.getY() - HEIGHT
  189.                         / 2);
  190.                 image2.sendToFront();
  191.             } else if (ball.getY() < HEIGHT / 2) {
  192.                 paddleRight.setLocation(getWidth() - 3 * WIDTH, 0);
  193.                 image2.setLocation(getWidth() - 3 * WIDTH, 0);
  194.                 image2.sendToFront();
  195.             } else {
  196.                 paddleRight.setLocation(getWidth() - 3 * WIDTH, getHeight()
  197.                         - HEIGHT);
  198.                 image2.setLocation(getWidth() - 3 * WIDTH, getHeight() - HEIGHT);
  199.                 image2.sendToFront();
  200.             }
  201.  
  202.             if (ballHitBottom(ball) && dy >= 0) {
  203.                 dy *= -1;
  204.             }
  205.  
  206.             if (ballHitTop(ball) && dy <= 0) {
  207.  
  208.                 dy *= -1;
  209.             }
  210.  
  211.             if (ballHitPaddleRight(ball, paddleRight)) {
  212.                 dx *= -1;
  213.             }
  214.  
  215.             if (ballHitPaddleLeft(ball, paddleLeft)) {
  216.                 dx *= -1;
  217.                 COUNTER++;
  218.                 counter.setLabel(String.valueOf(COUNTER));
  219.                 counter.setLocation(getWidth() / 2 - counter.getWidth() / 2,
  220.                         getHeight() / 2 + counter.getHeight() / 3.2);
  221.                 TRANSPARENCY = 0.1f;
  222.                 labelC = new Color(0, 0.0f, 0.0f, TRANSPARENCY);
  223.                 counter.setColor(labelC);
  224.  
  225.                 boolean bool = rand.nextBoolean();
  226.                 if (bool)
  227.                     if (dx > 0)
  228.                         dx += 1;
  229.                     else
  230.                         dx -= 1;
  231.                 else if (dy > 0)
  232.                     dy += 0.5;
  233.                 else
  234.                     dy -= 0.5;
  235.  
  236.             }
  237.             pause(PAUSE);
  238.            
  239.  
  240.             if (ballOffScreen(ball)) {
  241.                 if (ball.getX() + SPEED * 2 < 0) { // left
  242.                     double pos = ball.getY() - greenFlash.getHeight() / 2;
  243.                     remove(ball);
  244.                     remove(ballTexture);
  245.                     AI_SCORE+=COUNTER;
  246.                     AiScore.setLabel(String.valueOf(AI_SCORE));
  247.                     AiScore.setLocation(getWidth()-AiScore.getWidth()-3*WIDTH-10,   getHeight()-10);
  248.                     for (int i = 20; i < 100; i += 5) {
  249.                         greenFlash.setLocation(-i, pos);
  250.                         pause(25);
  251.                     }
  252.                 } else { // right
  253.                     double pos = ball.getY() - blueFlash.getHeight() / 2;
  254.                     remove(ball);
  255.                     remove(ballTexture);
  256.                     PLAYER_SCORE+=COUNTER;
  257.                     PlayerScore.setLabel(String.valueOf(PLAYER_SCORE));
  258.                     PlayerScore.setLocation(10+3*WIDTH,getHeight()-10);
  259.                     for (int i = 20; i < 100; i += 5) {
  260.                         blueFlash.setLocation(getWidth() - blueFlash.getWidth()
  261.                                 + i, pos);
  262.                         pause(25);
  263.                     }
  264.  
  265.                 }
  266.  
  267.                 COUNTER = 0;
  268.                 bounce(labelC, AiScore, PlayerScore, counter, ball, paddleLeft,
  269.                         paddleRight, image, image2, ballTexture, greenFlash,
  270.                         blueFlash);
  271.             }
  272.         }
  273.     }
  274.  
  275.     private boolean ballHitBottom(GOval ball) {
  276.         double bottomY = ball.getY() + ball.getHeight();
  277.         return bottomY >= getHeight();
  278.     }
  279.  
  280.     private boolean ballHitTop(GOval ball) {
  281.         double topY = ball.getY();
  282.         return topY <= 0;
  283.     }
  284.  
  285.     private boolean ballHitPaddleRight(GOval ball, GRect paddle) {
  286.         double rightX = ball.getX() + ball.getWidth();
  287.         double rightY = ball.getY() + ball.getHeight() / 2;
  288.         double paddlePosX = paddle.getX();
  289.         double paddlePosY = paddle.getY();
  290.         if (rightX > paddlePosX && rightY > paddlePosY
  291.                 && rightY < paddlePosY + paddle.getHeight())
  292.             return true;
  293.         else
  294.             return false;
  295.     }
  296.  
  297.     private boolean ballOffScreen(GOval ball) {
  298.         if (ball.getX() + SPEED * 2 < 0
  299.                 || ball.getX() + ball.getWidth() - SPEED * 2 > getWidth())
  300.             return true;
  301.         else
  302.             return false;
  303.     }
  304.  
  305.     private boolean ballHitPaddleLeft(GOval ball, GRect paddle) {
  306.         double leftX = ball.getX();
  307.         double leftY = ball.getY();
  308.         double paddlePosX = paddle.getX() + WIDTH;
  309.         double paddlePosY = paddle.getY();
  310.         if (leftX < paddlePosX && leftY > paddlePosY
  311.                 && leftY < paddlePosY + paddle.getHeight())
  312.             return true;
  313.         else
  314.             return false;
  315.     }
  316.  
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement