Guest User

Untitled

a guest
Mar 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. //BALLONWELT
  2.  
  3. package ballonwelteclipse;
  4.  
  5. import processing.core.PApplet;
  6. import processing.core.PImage;
  7.  
  8. public class BallonWelt extends PApplet {
  9. // verkettete Ballons steigen langsam schwingend in die Höhe
  10.  
  11. // lade aus Moodle/Medien/Greenfoot/Objects die Bilder
  12. // balloon1,2,3 in den data-Ordner
  13.  
  14. // Datenfelder:
  15. // PImage-Array ballons
  16. PImage[] ballons;
  17.  
  18. // b vom Typ Ballon
  19. Ballon b;
  20.  
  21. public void setup() {
  22.  
  23. // setze Fenstergröße 400 x 600
  24. size(400, 600);
  25.  
  26. // initialisiere Array ballons für 3 Bilder
  27. ballons = new PImage[3];
  28.  
  29. // den ballons-Elementen werden die Bilder zugewiesen
  30. ballons[0] = loadImage("balloon1.png");
  31. ballons[1] = loadImage("balloon2.png");
  32. ballons[2] = loadImage("balloon3.png");
  33.  
  34. // Ballon b wird am unteren Fensterrand erzeugt
  35. b = new Ballon(this, ballons[0], height);
  36. }
  37.  
  38. // Methode draw
  39. public void draw() {
  40. // setze Hintergrundfarbe
  41. background(255, 255, 119);
  42. // b steigt
  43. b.steige();
  44. }
  45.  
  46. // Methode mousePressed
  47. public void mousePressed() {
  48. // an b wird ein Ballon mit zufälliger Farbe angehängt
  49. b.haengeAn(new Ballon(ballons[(int) (random(0, 3))]));
  50. }
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. //BALLON
  64. package ballonwelteclipse;
  65.  
  66. import processing.core.PApplet;
  67. import processing.core.PImage;
  68.  
  69. public class Ballon {
  70. // Datenfelder:
  71. // x, y vom Typ float für die y-Koordinate
  72. float x, y;
  73. // img vom Typ PImage
  74. PImage img;
  75. // next vom Typ Ballon
  76. Ballon next;
  77. // p vom Typ PApplet
  78. static PApplet p;
  79.  
  80. // der erste Konstruktor erhält Werte für y und img
  81. // x wird auf die Bildschirmmitte gesetzt.
  82. // Zunächst hat der Ballon noch keinen Nachfolger
  83. public Ballon(PApplet p, PImage img, float y) {
  84. this.y = y;
  85. this.img = img;
  86. x = p.width / 2;
  87. next = null;
  88. this.p = p;
  89. }
  90.  
  91. // der zweiter Konstruktor erhält nur Werte für img.
  92. // Er ruft den ersten Konstruktor auf.
  93. public Ballon(PImage img) {
  94. this(p, img, 0);
  95. }
  96.  
  97. // Methode steige
  98. void steige() {
  99. // die x-Koordinate schwingt um die Mittelsenkrechte.
  100. // Nutze dazu:
  101. // x = width/2 + 40*sin(radians(frameCount));
  102. x = p.width / 2 + 40 * p.sin(p.radians(p.frameCount));
  103.  
  104. // die y-Koordinate verringert sich (z.B. um 0.8);
  105. y = y - 0.8f;
  106. // der Ballon wird gezeigt
  107. display();
  108. // die Methode ziehe wird aufgerufen
  109. ziehe();
  110. // wenn der letzte Ballon oben herausgeflogen ist,
  111. // kommt der erste wieder unten hoch.
  112. if (letzter().y < -letzter().img.height)
  113. y = p.height;
  114. }
  115.  
  116. // Methode display
  117. void display() {
  118. // das Bild wird gezeigt.
  119. p.image(img, x, y);
  120. }
  121.  
  122. // Methode ziehe
  123. void ziehe() {
  124. // falls ein Nachfolger vorhanden
  125. if (next != null) {
  126. // die y-Koordinate des Nachfolgers wird mit 10 Pixel
  127. // Zwischenraum unter den aktuellen Ballon gesetzt.
  128. next.y = y + img.height + 10;
  129. // die x-Koordinate des Nachfolgers schwankt um die x-Koordinate
  130. // des aktuellen Ballons, z.B:
  131. // next.x = x + 20*sin(radians(frameCount));
  132. next.x = x + 20 * p.sin(p.radians(p.frameCount));
  133. // der Nachfolger wird gezeigt
  134. next.display();
  135. // der Nachfolger wird aufgefordert, ebenfalls zu ziehen.
  136. next.ziehe();
  137. }
  138. }
  139.  
  140. // Methode letzter gibt den letzten Ballon in der
  141. // Kette zurück
  142. Ballon letzter() {
  143. // wenn kein Nachfolger vorhanden, dann ist der
  144. // aktuelle Ballon der letzte
  145. if (next == null)
  146. return this;
  147. // ansonsten wird der Nachfolger gebeten, den
  148. // letzten zu liefern
  149. else
  150. return next.letzter();
  151. }
  152.  
  153. // Methode haengeAn hängt einen Ballon unter den letzten.
  154. void haengeAn(Ballon b) {
  155. letzter().next = b;
  156. }
  157. }
Add Comment
Please, Sign In to add comment