Skip to content

Commit afc975a

Browse files
author
Marcel Kaufmann
committed
Level 5 A 5 und 6
1 parent 874e226 commit afc975a

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed
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+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Coin {
2+
PShape c;
3+
float xPos;
4+
float yPos;
5+
float coinWidth;
6+
float coinHeigth;
7+
8+
public Coin() {
9+
ellipseMode(CENTER);
10+
init();
11+
}
12+
13+
void init( ) {
14+
coinWidth = gameHeight/50;
15+
coinHeigth = gameHeight/50;
16+
xPos = random(coinWidth, (gameWidth-1)-coinWidth);
17+
yPos = -coinHeigth;
18+
c = createShape(ELLIPSE, 0, 0, coinWidth, coinHeigth);
19+
c.setFill(color(255, 255, 0));
20+
}
21+
22+
void display( ) {
23+
shape(c, xPos, -coinWidth);
24+
c.translate(0, 2);
25+
yPos += 2;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.Stack;
2+
3+
class CoinShower {
4+
5+
Stack<Coin> waitingStack = new Stack();
6+
int interval = 1000;
7+
int savedTime = millis();
8+
9+
void start(ArrayList<Coin> ingameCoins) {
10+
if (isTimeForNextOne()) {
11+
if (waitingStack.empty()) {
12+
waitingStack.push(new Coin());
13+
}
14+
Coin coin = waitingStack.pop();
15+
ingameCoins.add(coin);
16+
}
17+
}
18+
19+
boolean isTimeForNextOne( ) {
20+
int currentTime = millis();
21+
if (savedTime + interval < currentTime) {
22+
savedTime = currentTime;
23+
return true;
24+
}
25+
return false;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
class Game {
3+
int lives;
4+
int score;
5+
ArrayList<Coin> coinListInGame = new ArrayList<Coin>();
6+
7+
Catcher catcher;
8+
CoinShower shower;
9+
10+
int interval = 1000;
11+
int savedTime = millis();
12+
13+
public Game () {
14+
lives = 5;
15+
score = 0;
16+
catcher = new Catcher();
17+
shower = new CoinShower();
18+
}
19+
20+
21+
void execute() {
22+
catcher.draw();
23+
shower.start(coinListInGame);
24+
checkIngameCoins();
25+
println("Score: " + score);
26+
}
27+
28+
void checkIngameCoins( ) {
29+
for (int i = 0; i < coinListInGame.size(); i++) {
30+
Coin coin = coinListInGame.get(i);
31+
boolean isCatched = isInCatcherZone(coin);
32+
boolean isOut = isInOutZone(coin);
33+
if (isCatched) score++;
34+
if (isOut) lives--;
35+
36+
if (isCatched || isOut) {
37+
coin.init();
38+
shower.waitingStack.push(coin);
39+
coinListInGame.remove(coin);
40+
} else {
41+
coin.display();
42+
}
43+
}
44+
}
45+
46+
boolean isInOutZone(Coin coin) {
47+
return coin.yPos + coin.coinHeigth/2 >= gameHeight-1;
48+
}
49+
50+
boolean isInCatcherZone(Coin coin) {
51+
return isInYZone(coin) && isInXZone(coin);
52+
}
53+
54+
boolean isInYZone(Coin coin) {
55+
boolean coinLowerBound =
56+
coin.yPos + coin.coinHeigth/2 >= catcher.yPos - catcher.catcherHeight/2 &&
57+
coin.yPos + coin.coinHeigth/2 <= catcher.yPos + catcher.catcherHeight/2;
58+
boolean coinUpperBound =
59+
coin.yPos - coin.coinHeigth/2 >= catcher.yPos - catcher.catcherHeight/2 &&
60+
coin.yPos - coin.coinHeigth/2 <= catcher.yPos + catcher.catcherHeight/2;
61+
return coinLowerBound || coinUpperBound;
62+
}
63+
64+
boolean isInXZone(Coin coin) {
65+
boolean coinRightBound =
66+
coin.xPos + coin.coinWidth/2 >= catcher.xPos - catcher.catcherWidth/2 &&
67+
coin.xPos + coin.coinWidth/2 <= catcher.xPos + catcher.catcherWidth/2;
68+
boolean coinLeftBound =
69+
coin.xPos - coin.coinWidth/2 >= catcher.xPos - catcher.catcherWidth/2 &&
70+
coin.xPos - coin.coinWidth/2 <= catcher.xPos + catcher.catcherWidth/2;
71+
return coinRightBound || coinLeftBound;
72+
}
73+
}

tutorial/Level5/Processing_Tutorial_Level_#5-CatcherGameTutorial.md

+6
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ println("Lives: "+ lives); // Innerhalb der zyklischen Ausführung
4949

5050
> Exkurs: Ist dir aufgefallen, dass die Kollisionserkennung nicht ganz korrekt ist wenn du wie ich für die herunterfallenden Objekte Kreise verwendest? Der Algorithmus dafür geht von einem Rechteck aus. Dies wird klar, wenn du die Berechnung der Kollision auf Papier mit den Objekten malst und den Kreis links oder rechts an die Ecke des *Catchers* treffen lässt. Haben wir nun quatsch implementiert? Wieder: Theoretisch ja, praktisch nein! In Spielen (vorrangig bei 3D - aber auch teilweise bei 2D) ist es aus Effizienzgründen gängig, dass eine [*Hitbox*](https://de.wikipedia.org/wiki/Hitbox) definiert wird. Diese Beschreibt den Bereich, in dem Kollision als gegeben angesehen wird. Der Kreis besitzt also eine virtuelle Hitbox um sich herum.
5151
52+
### Aufgabe 5 (optional)
53+
1. Die Game-Klasse wird immer voller. Lagere doch Funktionalität aus! Ein Beispiel ist das Organisieren der herunterfallenden Objekte. Falls du magst kannst du auch andere spezifische Logik aus der Klasse in eine neue ziehen.
5254

55+
[Mögliche Lösung](https://github.com/Flocksserver/CatcherTutorial/blob/master/tutorial/Level5/CatcherTutorialLevel5A5)
56+
57+
### Aufgabe 6 (optional)
58+
1. Teste die Anwendung auch auf Android
5359

5460
# Nächstes Level
5561
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)