Talar97

[GK] Processing - Pong

May 18th, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. Ball circle;
  2. Paddle left;
  3. Paddle right;
  4.  
  5. //Sterowanie
  6. boolean upL, downL;
  7. boolean upR, downR;
  8.  
  9. //Wyniki
  10. int scoreL = 0;
  11. int scoreR = 0;
  12. int maxScore = 5;
  13.  
  14.  
  15. void setup(){
  16.   size(1100, 600);
  17.  
  18.   //Piłka
  19.   circle = new Ball(width/2, width/2, 40, 40, 4, 4);
  20.  
  21.   //Paletki
  22.   textSize(30);
  23.   textAlign(CENTER, CENTER);
  24.   rectMode(CENTER);
  25.   left = new Paddle(40, height/2, 20, 125, 6, true);
  26.   right = new Paddle(width-40, height/2, 20, 125, 6, false);
  27. }
  28.  
  29. void draw(){
  30.   background(0);
  31.  
  32.   circle.drawBall();
  33.   circle.moveBall();
  34.   circle.bounceOff();
  35.  
  36.   left.drawPaddle();
  37.   right.drawPaddle();
  38.   left.movePaddle();
  39.   right.movePaddle();
  40.   left.paddleLimit();
  41.   right.paddleLimit();
  42.   left.detectBall(circle);
  43.   right.detectBall(circle);
  44.  
  45.   scores();
  46.   gameOver();
  47. }
  48.  
  49. void scores() {
  50.   fill(255);
  51.   text(scoreL + "   : ", 520, 50);
  52.   text(scoreR, width-520, 50);
  53. }
  54.  
  55. void gameOver(){
  56.   if(scoreL == maxScore) endGameScreen("Zwycięzca: gracz 1");
  57.   else if(scoreR == maxScore) endGameScreen("Zwycięzca: gracz 2");
  58. }
  59.  
  60. void endGameScreen(String winMsg){
  61.   circle.setSX(0);
  62.   circle.setSY(0);
  63.  
  64.   fill(255);
  65.   text("Zagraj ponownie", width/2, height/3 + 40);
  66.   text(winMsg, width/2, height/3);
  67.  
  68.   if(mousePressed){
  69.     scoreR = 0;
  70.     scoreL = 0;
  71.     circle.setSX(4);
  72.     circle.setSY(4);
  73.   }
  74. }
  75.  
  76. class Paddle{
  77.   //PosX, PosY, width, height, speed
  78.   private int x, y, w, h, s;
  79.   private boolean controlType;
  80.  
  81.   Paddle(int x, int y, int w, int h, int s, boolean controlType){
  82.     this.x = x;
  83.     this.y = y;
  84.     this.w = w;
  85.     this.h = h;
  86.     this.s = s;
  87.     this.controlType = controlType;
  88.   }
  89.  
  90.   void movePaddle(){
  91.     if(controlType){
  92.       if (upL)   this.y -= this.s;
  93.       if (downL) this.y += this.s;
  94.     }
  95.     else{
  96.       if (upR)   this.y -= this.s;
  97.       if (downR) this.y += this.s;
  98.     }
  99.   }
  100.  
  101.   void paddleLimit() {
  102.     if(controlType){
  103.       if (this.y - this.h/2 < 0)       this.y += this.s;
  104.       if (this.y + this.h/2 > height)  this.y -= this.s;
  105.     }
  106.     else{
  107.       if (this.y - this.h/2 < 0)       this.y += this.s;
  108.       if (this.y + this.h/2 > height)  this.y -= this.s;
  109.     }
  110.   }
  111.  
  112.   void drawPaddle(){
  113.     fill(255);
  114.     rect(this.x, this.y, this.w, this.h);
  115.   }
  116.  
  117.   void detectBall(Ball circle){
  118.     int x = circle.getX();
  119.     int y = circle.getY();
  120.     int w = circle.getW();
  121.     int h = circle.getH();
  122.    
  123.     if(controlType){
  124.       if(x - w/2 < this.x + this.w/2 && y - h/2 < this.y + this.h/2 && y + h/2 > this.y - this.h/2){
  125.         if(circle.getSX() < 0) circle.setSX(-circle.getSX());
  126.       }
  127.     }
  128.     else{
  129.       if(x + w/2 > this.x - this.w/2 && y - h/2 < this.y + this.h/2 && y + h/2 > this.y - this.h/2){
  130.         if(circle.getSX() > 0) circle.setSX(-circle.getSX());
  131.       }
  132.     }
  133.    
  134.   }
  135. }
  136.  
  137. class Ball{
  138.   //PosX, PosY, width, height, SpeedX, SpeedY
  139.   private int x, y, w, h, sX, sY;
  140.  
  141.   Ball(int x, int y, int w, int h, int sX, int sY){
  142.     this.x = x;
  143.     this.y = y;
  144.     this.w = w;
  145.     this.h = h;
  146.     this.sX = sX;
  147.     this.sY = sY;
  148.   }
  149.  
  150.   int getX(){ return this.x; }
  151.   int getY(){ return this.y; }
  152.   int getW(){ return this.w; }
  153.   int getH(){ return this.h; }
  154.   int getSX() { return this.sX; }
  155.   void setSX(int sX) { this.sX = sX; }
  156.   void setSY(int sY) { this.sY = sY; }
  157.  
  158.   void drawBall(){
  159.     fill(255);
  160.     ellipse(this.x, this.y, this.w, this.h);
  161.   }
  162.  
  163.   void moveBall(){
  164.     this.x += this.sX*2;
  165.     this.y += this.sY*2;
  166.   }
  167.  
  168.   void bounceOff() {
  169.    if ( this.x > width - this.w/2) {
  170.       setup();
  171.       this.sX *= -1;
  172.       scoreL++;
  173.     } else if ( this.x < 0 + this.w/2) {
  174.       setup();
  175.       scoreR++;
  176.     }
  177.     if ( this.y > height - this.h/2) {
  178.       this.sY *= -1;
  179.     } else if ( this.y < 0 + this.h/2) {
  180.       this.sY *= -1;
  181.     }
  182.   }
  183. }
  184.  
  185. void keyPressed() {
  186.   if (key == 'w' || key == 'W') upL = true;
  187.   if (key == 's' || key == 'S') downL = true;
  188.   if (keyCode == UP)            upR = true;
  189.   if (keyCode == DOWN)          downR = true;
  190. }
  191.  
  192. void keyReleased() {
  193.   if (key == 'w' || key == 'W') upL = false;
  194.   if (key == 's' || key == 'S') downL = false;
  195.   if (keyCode == UP)            upR = false;
  196.   if (keyCode == DOWN)          downR = false;
  197. }
Add Comment
Please, Sign In to add comment