Skip to content

Commit b2fcc7f

Browse files
author
Marcel Kaufmann
committed
Level 5 A 2
1 parent ac64e3d commit b2fcc7f

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Catcher {
2+
PShape c;
3+
float yPos;
4+
float xPos;
5+
float catcherWidth;
6+
float catcherHeight;
7+
8+
public Catcher( ) {
9+
catcherWidth = gameWidth/8;
10+
catcherHeight = gameHeight/100;
11+
rectMode(CENTER);
12+
fill(255);
13+
c = createShape(RECT, 0, 0, catcherWidth, catcherHeight);
14+
yPos = gameHeight-(gameHeight/8);
15+
xPos = gameWidth/2;
16+
}
17+
18+
void draw() {
19+
xPos = playerPosX;
20+
shape(c, xPos, yPos);
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
float playerPosX;
2+
float gameHeight;
3+
float gameWidth;
4+
Game game;
5+
6+
void setup() {
7+
size(400, 640);
8+
//fullScreen(P2D);
9+
gameWidth = width;
10+
gameHeight = height;
11+
game = new Game();
12+
}
13+
14+
void draw() {
15+
background(50);
16+
if (mousePressed) {
17+
playerPosX = mouseX;
18+
}
19+
game.execute();
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Coin {
2+
PShape c;
3+
float xPos;
4+
float yPos;
5+
float coinWidth;
6+
float coinHeigth;
7+
8+
public Coin() {
9+
coinWidth = gameHeight/50;
10+
coinHeigth = gameHeight/50;
11+
xPos = random(coinWidth, (gameWidth-1)-coinWidth);
12+
yPos = -coinHeigth;
13+
ellipseMode(CENTER);
14+
c = createShape(ELLIPSE, 0, 0, coinWidth, coinHeigth);
15+
c.setFill(color(255, 255, 0));
16+
}
17+
18+
void display( ) {
19+
shape(c, xPos, -coinWidth);
20+
c.translate(0, 2);
21+
yPos += 2;
22+
}
23+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.Stack;
2+
3+
class Game {
4+
int lives;
5+
ArrayList<Coin> coinListInGame = new ArrayList<Coin>();
6+
Stack<Coin> waitingStack = new Stack();
7+
8+
Catcher catcher;
9+
10+
int interval = 1000;
11+
int savedTime = millis();
12+
13+
public Game () {
14+
lives = 5;
15+
println("Lives: " + lives);
16+
catcher = new Catcher();
17+
}
18+
19+
20+
void execute() {
21+
catcher.draw();
22+
if (isTimeForNextOne()) {
23+
if (waitingStack.empty()) {
24+
waitingStack.push(new Coin());
25+
}
26+
Coin newCoin = waitingStack.pop();
27+
coinListInGame.add(newCoin);
28+
}
29+
30+
for (int i = 0; i < coinListInGame.size(); i++) {
31+
Coin currentCoin = coinListInGame.get(i);
32+
boolean isOut = isInOutZone(currentCoin);
33+
if (isOut) {
34+
coinListInGame.remove(currentCoin);
35+
lives --;
36+
println("Lives: " + lives);
37+
} else {
38+
currentCoin.display();
39+
}
40+
}
41+
}
42+
43+
boolean isInOutZone(Coin coin) {
44+
return coin.yPos + coin.coinHeigth/2 >= gameHeight-1;
45+
}
46+
47+
boolean isTimeForNextOne( ) {
48+
int currentTime = millis();
49+
if (savedTime + interval < currentTime) {
50+
savedTime = currentTime;
51+
return true;
52+
}
53+
return false;
54+
}
55+
}

tutorial/Level5/Processing_Tutorial_Level_#5-CatcherGameTutorial.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ println("Lives: "+ lives); // Innerhalb der zyklischen Ausführung
2222
</div>
2323
<br>
2424

25-
3. Implementiere die Kollisionserkennung. Tipp: Du kannst die Y-Position des herunterfallenden Objektes aus dem PShape-Objekt extrahieren, oder die einfachere Variante: Jedesmal beim Aufruf von *translate()* die eine Variable zur yPosition um denselben Wert wie translate erhöhen (achte aber darauf, dass du beim Zeichnen des Objektes nicht die nun erstellte yPos nimmst!).
25+
3. Implementiere die Kollisionserkennung. Tipp: Du kannst die Y-Position des herunterfallenden Objektes aus dem PShape-Objekt extrahieren, oder die einfachere Variante: Jedesmal beim Aufruf von *translate()* kannst du eine Variable zur yPosition um denselben Wert wie translate erhöhen (achte aber darauf, dass du beim Zeichnen des Objektes nicht die nun erstellte yPos nimmst!).
2626
4. Zähle ein Leben runter, sobald ein Objekt das Ende des Bildschirms erreicht. Entferne das entsprechenden Objekt aus der *ingame*-Liste
2727

2828
[Mögliche Lösung](https://github.com/Flocksserver/CatcherTutorial/blob/master/tutorial/Level5/CatcherTutorialLevel5A1)
2929

30+
### Aufgabe 2
31+
1. Nun möchten wir es nicht bei den 6 Objekten belassen. Als nächstes sollst du einen Dauerregen an Objekten implementieren. Lass den Stack (waitingStack) initial leer. Passe deine Implementierung so an, dass immer dann, wenn der Stack leer ist, ein neues Objekt erstellt wird, das in diesen Stack gepusht wird.
32+
33+
[Mögliche Lösung](https://github.com/Flocksserver/CatcherTutorial/blob/master/tutorial/Level5/CatcherTutorialLevel5A2)
34+
3035
# Nächstes Level
3136
Hier geht es weiter zum [nächsten Level :arrow_right:](https://github.com/Flocksserver/CatcherTutorial/blob/master/tutorial/Level6/Processing_Tutorial_Level_%236-CatcherGameTutorial.md)

0 commit comments

Comments
 (0)