Moortiii

Pong - Singleplayer

Feb 10th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. Ball b;
  2. Paddle p;
  3. int score = 0;
  4.  
  5. void setup() {
  6.   frameRate(144);
  7.   size(640, 380);
  8.   b = new Ball(50, height/2, 50);
  9.   p = new Paddle(width-100, height/2, 30, 120);
  10. }
  11.  
  12. void draw() {
  13.   background(100);
  14.   b.display();
  15.   b.move();
  16.   b.bounce();
  17.   p.display();
  18.   //p.move();
  19.   if(b.overlaps(p)){
  20.     b.turn();
  21.     score += 1;
  22.   }
  23.  
  24.  
  25.   String myText = "Score: " + score;
  26.   textSize(32);
  27.   textAlign(CENTER);
  28.   text(myText, width/2, 50);
  29. }
  30.  
  31. class Ball {
  32.   float x, y, d;
  33.   int xdirection = 1;
  34.   int ydirection = 1;
  35.   float xspeed = 4;
  36.   float yspeed = 4;
  37.  
  38.   Ball(float tempX, float tempY, float tempD) {
  39.     x = tempX;
  40.     y = tempY;
  41.     d = tempD;
  42.   }
  43.  
  44.   void display() {
  45.     ellipse(x, y, d, d);
  46.   }
  47.  
  48.   void move() {
  49.     x = x + (xspeed * xdirection);
  50.     //y = y + (yspeed * ydirection);
  51.   }
  52.  
  53.   void bounce() {
  54.     if (x < 0+d/2 || x > width-d/2) {
  55.       xdirection *= -1;
  56.     }
  57.     if (y < 0+d/2 || y > height-d/2) {
  58.       ydirection *= -1;
  59.     }
  60.   }
  61.  
  62.   boolean overlaps(Paddle paddle){
  63.     float dist = dist(x, y, paddle.x, paddle.y);
  64.     if(dist < d/2 + paddle.h/2){
  65.       return true;
  66.     } else {
  67.       return false;
  68.     }
  69.   }
  70.  
  71.   void turn() {
  72.     xdirection *= -1;
  73.   }
  74. }
  75.  
  76. class Paddle {
  77.   float x, y, w, h;
  78.   Paddle(float tempX, float tempY, float tempH, float tempW) {
  79.     x = tempX;
  80.     y = tempY;
  81.     h = tempH;
  82.     w = tempW;
  83.   }
  84.  
  85.   void display(){
  86.     rectMode(CENTER);
  87.     rect(x, y, h, w);
  88.   }
  89.  
  90.   void move(){
  91.     y = mouseY;
  92.   }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment