Johanneszockt1

java pong v1

Jan 30th, 2019
111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float spieler_x = 20; //balken startpos y
  2. float spieler_y = 60; //balken startpos y
  3. float ball_x = 450;    //position (x) kugel
  4. float ball_y = 350;    //position (y) kugel
  5. float ball_speed_x = -3;  //erhöhende ball geschwindigkeit
  6. float ball_speed_y = 0;  //erhöhende ball geschwindigkeit
  7. float player_speed_y = 12;  //max balken geschwindigkeit
  8. int runde;  //runden counter
  9. int up = 'w';// standard = UP
  10. boolean upstate;
  11. int down = 's';// standard = DOWN
  12. boolean downstate;
  13. boolean autoplay = true;
  14. void setup () {
  15.   size(900, 700);
  16.   rectMode(CENTER);
  17. }
  18.  
  19. void draw() {
  20.  
  21.   background(0);
  22.   rect(spieler_x, spieler_y, 20, 100);
  23.   ellipse(ball_x, ball_y, 10, 10);
  24.  
  25.   ball_x = ball_x + ball_speed_x;
  26.   ball_y = ball_y + ball_speed_y;
  27.   if (autoplay) spieler_y = ball_y;
  28.   if (upstate && spieler_y > 50) spieler_y = spieler_y - player_speed_y;
  29.   if (downstate && spieler_y < 650) spieler_y = spieler_y + player_speed_y;
  30.   if (ball_x < 30) {
  31.     if (ball_y < (spieler_y + 55) && ball_y > (spieler_y - 55)) {
  32.       ball_speed_x = (-ball_speed_x) + 1;
  33.       ball_speed_y = ball_speed_y - (spieler_y - ball_y) * 0.1;
  34.       runde = runde + 1;
  35.       println(runde);
  36.     } else {
  37.       ball_x = 450;
  38.       ball_y = 350;
  39.       ball_speed_x = -3;
  40.       ball_speed_y = 0;
  41.       runde = 0;
  42.       print("Neue Runde");
  43.       println(runde);
  44.     }
  45.   }
  46.  
  47.   if (ball_y > 695 || ball_y < 5) {
  48.     ball_speed_y = -ball_speed_y;
  49.   }
  50.   if (ball_x > 895) {
  51.     ball_speed_x = -ball_speed_x;
  52.   }
  53.   text("Runde: " + runde, 810, 20);
  54. }
  55. void keyPressed()
  56. {
  57.   int pressedKey = key;
  58.   print("down ");
  59.   println(pressedKey);
  60.   if (pressedKey == up) upstate = true;
  61.   else if (pressedKey == down) downstate = true;
  62.   else ;
  63. }
  64. void keyReleased() {
  65.   int releasedKey = key;
  66.   print("up ");
  67.   println(releasedKey);
  68.   if (releasedKey == up) upstate = false;
  69.   else if (releasedKey == down) downstate = false;
  70.   else ;
  71. }
RAW Paste Data