Skip to content

Commit 38bf445

Browse files
committed
add example donut io
1 parent 8276832 commit 38bf445

File tree

18 files changed

+589
-42
lines changed

18 files changed

+589
-42
lines changed

docs/de/book/examples/donut-io.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: Donut IO
3+
lang: de
4+
---
5+
6+
# Donut IO
7+
8+
Dieses Beispiel zeigt die Wechsel der Bühne und die Interaktion mehrerer Figuren miteinander.
9+
10+
![donut io example](/assets/donut-io.gif)
11+
12+
## Quelltext
13+
14+
- Java: https://github.com/openpatch/scratch-for-java/tree/main/examples/java/DonutIO

docs/de/public/assets/donut-io.gif

Lines changed: 3 additions & 0 deletions
Loading

docs/en/book/examples/donut-io.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: Donut IO
3+
lang: en
4+
---
5+
6+
# Donut IO
7+
8+
This example show the switch of stages and the interaction of multiple sprites.
9+
10+
![donut io example](/assets/donut-io.gif)
11+
12+
## Source Code
13+
14+
- Java: https://github.com/openpatch/scratch-for-java/tree/main/examples/java/DonutIO

docs/en/public/assets/donut-io.gif

Lines changed: 3 additions & 0 deletions
Loading

examples/java/DonutIO/Background.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import org.openpatch.scratch.Sprite;
2+
3+
public class Background extends Sprite {
4+
5+
public Background() {
6+
this.addCostume("grid", "assets/grid.png");
7+
}
8+
9+
public void run() {
10+
this.setX(-WorldStage.CAM.getX() % 20);
11+
this.setY(-WorldStage.CAM.getY() % 20);
12+
}
13+
14+
}

examples/java/DonutIO/Donut.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.awt.geom.Ellipse2D;
2+
import org.openpatch.scratch.Sprite;
3+
import org.openpatch.scratch.extensions.math.Random;
4+
import org.openpatch.scratch.extensions.math.Vector2;
5+
6+
public class Donut extends Sprite {
7+
8+
private int strength;
9+
protected double speed = 1;
10+
protected Vector2 mapPosition;
11+
12+
public Donut() {
13+
this(0, 0, 2);
14+
}
15+
16+
public Donut(double mapX, double mapY, int strength) {
17+
this.addCostume("donut", "assets/donut.png");
18+
this.setHitbox(new Ellipse2D.Double(0, 0, 512, 480));
19+
20+
this.setMapPosition(new Vector2(mapX, mapY));
21+
22+
this.setStrength(strength);
23+
24+
this.setTint(Random.randomInt(240), Random.randomInt(240), Random.randomInt(240));
25+
}
26+
27+
public void setStrength(int strength) {
28+
this.strength = strength;
29+
this.setSize(strength);
30+
}
31+
32+
public void setMapPosition(Vector2 position) {
33+
this.mapPosition = position;
34+
this.setPosition(this.mapPosition.sub(WorldStage.CAM));
35+
}
36+
37+
public void run() {
38+
this.setPosition(this.mapPosition.sub(WorldStage.CAM));
39+
40+
var touchingDonut = this.getTouchingSprite(Donut.class);
41+
if (touchingDonut != null && this.strength >= touchingDonut.strength) {
42+
this.setStrength(this.strength + touchingDonut.strength);
43+
touchingDonut.remove();
44+
}
45+
}
46+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class FollowDonut extends Donut {
2+
3+
private PlayerDonut player;
4+
5+
public FollowDonut(PlayerDonut player) {
6+
this.player = player;
7+
}
8+
9+
public void run() {
10+
var v = player.mapPosition.sub(this.mapPosition).unitVector();
11+
12+
this.setMapPosition(this.mapPosition.add(v).multiply(this.speed));
13+
14+
super.run();
15+
}
16+
}

examples/java/DonutIO/Game.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import org.openpatch.scratch.Window;
2+
3+
public class Game extends Window {
4+
5+
public static int LEVEL = 0;
6+
7+
public Game() {
8+
super(800, 600, "assets");
9+
10+
this.setStage(new StartStage());
11+
}
12+
13+
public static void main(String[] args) {
14+
new Game();
15+
}
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import org.openpatch.scratch.KeyCode;
2+
import org.openpatch.scratch.Stage;
3+
import org.openpatch.scratch.Window;
4+
import org.openpatch.scratch.extensions.text.Text;
5+
6+
public class GameOverStage extends Stage {
7+
public GameOverStage() {
8+
9+
this.add(new Background());
10+
11+
var text = new Text();
12+
text.setTextSize(48);
13+
text.setTextColor(200, 100, 100);
14+
text.showText("Level " + Game.LEVEL + " is too hard for you!");
15+
text.setPosition(0, 0);
16+
this.add(text);
17+
18+
text = new Text();
19+
text.setTextSize(32);
20+
text.setTextColor(200, 100, 100);
21+
text.showText("Press Space to start again!");
22+
text.setPosition(0, -40);
23+
this.add(text);
24+
}
25+
26+
public void whenKeyPressed(int keyCode) {
27+
System.out.println(keyCode);
28+
if (keyCode == KeyCode.VK_SPACE) {
29+
Game.LEVEL = 0;
30+
Window.getInstance().setStage(new WorldStage());
31+
}
32+
}
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class PlayerDonut extends Donut {
2+
3+
public PlayerDonut() {
4+
super(0, 0, 10);
5+
}
6+
7+
public void run() {
8+
var v = this.getMouse().unitVector();
9+
10+
this.setMapPosition(this.mapPosition.add(v).multiply(this.speed));
11+
12+
super.run();
13+
14+
}
15+
}

0 commit comments

Comments
 (0)