Skip to content

Commit 9cddaa7

Browse files
committed
add tutorial
1 parent bdee311 commit 9cddaa7

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
name: Bouncing Hedgehog
3+
index: 2
4+
lang: en
5+
---
6+
7+
# Bouncing Hedgehog
8+
9+
Spike, the bouncing hedgehog, loves to jump on his trampoline, but he's a bit clumsy. Can you move the trampoline so that he doesn't fall to the ground anymore?
10+
11+
In this chapter, you will program your first game. In this game, you will be able to use the arrow keys to move a trampoline from left to right. Your goal is to catch a bouncing target with the trampoline. This project will show you how to add new sprites and backgrounds, and how to control behavior in your project using conditional statements. These skills will also help you with the following projects.
12+
13+
## Step 1: Download Project Template
14+
15+
You can download the images along with a basic framework for your BlueJ project here.
16+
17+
::archive[Project: Bouncy Hedgehog]{name="bouncy-hedgehog"}
18+
19+
## Step 2: Run the Project
20+
21+
Let's take a look at what the project looks like in its initial state. Right-click on the `BouncyHedgehogStage` class and create a new object by clicking on `new BouncyHedgehogStage()`.
22+
23+
A window should open where you can see the playground.
24+
25+
## Step 3: Add the Trampoline to the Stage
26+
27+
Open the TrampolineSprite class in BlueJ by double-clicking. The class is already prepared for you. We will now go through it line by line so you understand what each line means. Don't worry, you don't have to understand everything right away – understanding grows over time.
28+
29+
```java
30+
import org.openpatch.scratch.Sprite;
31+
32+
public class TrampolineSprite extends Sprite {
33+
public TrampolineSprite() {
34+
this.addCostume("trampoline", "trampoline.png");
35+
}
36+
}
37+
```
38+
39+
- Line 1: Here a class is imported. This means we are using source code from someone else to make our work easier. We use the Sprite class from Scratch4j so we don't have to worry about how, for example, images can be displayed on the screen.
40+
41+
- Line 3: Here we define our own class named `TrampolineSprite` and this class should extend the `Sprite` class. In other words, use the functions of the already implemented `Sprite` class.
42+
43+
- Lines 4-6: Here we define a constructor. This is executed as soon as we create a trampoline - more on that later. Here we define that we want to add a costume, which we call `trampoline` and should use the file `trampoline.png`.
44+
45+
Now let's add the trampoline to our stage. Double-click on the `BouncyHedgehogStage` class. Here you see similar lines of source code.
46+
47+
```java
48+
import org.openpatch.scratch.Stage;
49+
50+
public class BouncyHedgehogStage extends Stage {
51+
52+
public BouncyHedgehogStage() {
53+
this.addBackdrop("playground", "playground.jpg");
54+
}
55+
}
56+
```
57+
58+
This time, however, the class inherits from Stage (line 3), since this should be a stage. Therefore, no costume is added in the constructor, but a background (see line 6).
59+
60+
So that we can now also see the trampoline, we add a line of source code to the constructor.
61+
62+
```java
63+
import org.openpatch.scratch.Stage;
64+
65+
public class BouncyHedgehogStage extends Stage {
66+
67+
public BouncyHedgehogStage() {
68+
this.addBackdrop("playground", "playground.jpg");
69+
this.add(new TrampolineSprite());
70+
}
71+
}
72+
```
73+
74+
The new line first calls the constructor of the `TrampolineSprite` class and then adds the newly created object to the stage with the method call `this.add(new TrampolineSprite())`.
75+
76+
Run the project again now. The trampoline should now be visible in the center of the stage.
77+
78+
## Step 4: Add the Hedgehog
79+
80+
Can you manage to add the hedgehog? Follow the previous steps and this time use the `HedgehogSprite` class.
81+
82+
## Step 5: Move the Trampoline
83+
84+
So far, both sprites are placed in the center of the stage. Now we will first move the trampoline to a good starting position. Then we will program it so that we can control it with the arrow keys.
85+
86+
To position the trampoline, we add another line to the constructor of the `TrampolineSprite` class.
87+
88+
```java
89+
import org.openpatch.scratch.Sprite;
90+
91+
public class TrampolineSprite extends Sprite {
92+
public TrampolineSprite() {
93+
this.addCostume("trampoline", "trampoline.png");
94+
this.setPosition(0, -120);
95+
}
96+
}
97+
```
98+
99+
With this line, we position the trampoline in the x-direction (left-right) at 0, which means in the center, and in the y-direction (up-down) at -120, which means 120 pixels down from the center.
100+
101+
Look at the result by running the project. To do this, right-click on the `BouncyHedgehogStage` class and select `new BouncyHedgehogStage()`.
102+
103+
## Step 6: Position the Hedgehog
104+
105+
Try to position the hedgehog at position (x: -180, y: 140).
106+
107+
## Step 7: Move the Trampoline
108+
109+
We want to move the trampoline with the arrow keys. To do this, we extend the `TrampolineSprite` class as follows:
110+
111+
```java
112+
import org.openpatch.scratch.KeyCode;
113+
import org.openpatch.scratch.Sprite;
114+
115+
public class TrampolineSprite extends Sprite {
116+
public TrampolineSprite() {
117+
this.addCostume("trampoline", "trampoline.png");
118+
this.setPosition(0, -120);
119+
}
120+
121+
public void whenKeyPressed(int keyCode) {
122+
if (keyCode == KeyCode.VK_LEFT) {
123+
this.changeX(-10);
124+
} else if (keyCode == KeyCode.VK_RIGHT) {
125+
this.changeX(10);
126+
}
127+
}
128+
}
129+
```
130+
131+
We have added a new method `whenKeyPressed` to the class. Methods allow us to implement certain behaviors for the objects of a class. In this case, `whenKeyPressed` is a special method - predefined by Scratch4j. It is always called when a key on the keyboard is pressed. The variable `keyCode` then contains the numeric code of that key.
132+
133+
For example, if you press the key `A`, then the variable `keyCode` contains the value `65`. So that we don't have to remember all the values of individual keys, there is the `KeyCode` class. For example, behind `KeyCode.VK_A` would be the value `65`. This also makes the source code more comprehensible. Since this is again a class from Scratch4j, we must also import it (see first line).
134+
135+
Now we can define what should happen when a key is pressed.
136+
137+
If the pressed key is the left arrow key, then we change the x-position by -10 pixels. So we move the trampoline to the left.
138+
Otherwise, if the pressed key is the right arrow key, then we change the x-position by 10 pixels. So we move the trampoline to the right.
139+
140+
So, now it's time to try out the project again. You already know the steps for that.
141+
142+
## Step 8: Make the Hedgehog Bounce
143+
144+
The hedgehog should automatically fall down and move back up when it touches the trampoline. We achieve this with the following code:
145+
146+
```java
147+
import org.openpatch.scratch.Sprite;
148+
import org.openpatch.scratch.extensions.math.Random;
149+
150+
public class HedgehogSprite extends Sprite {
151+
152+
public HedgehogSprite() {
153+
this.addCostume("hedgehog", "hedgehog.png");
154+
155+
this.pointInDirection(15);
156+
this.setPosition(-180, 140);
157+
}
158+
159+
public void run() {
160+
if (this.getY() > -120) {
161+
this.move(1);
162+
this.ifOnEdgeBounce();
163+
164+
if (this.isTouchingSprite(TrampolineSprite.class)) {
165+
this.pointInDirection(Random.random(-45, 45));
166+
}
167+
} else {
168+
this.say("Ouch!", 2000);
169+
}
170+
}
171+
}
172+
```
173+
174+
Based on the method names (isTouchingSprite, pointInDirection), think about what happens when the project is executed. Run the project and check your assumptions.
175+
176+
## Conclusion
177+
178+
Congratulations, you have programmed your first game with Scratch4j! Even if everything isn't clear to you yet, stick with it – practice makes perfect.
179+
180+
Feel free to continue experimenting: Make the hedgehog faster, reset the trampoline to the center with the spacebar, or swap out the graphics. When you become more confident, try out the [setTint](/reference/sprite/looks/setTint) method.
181+
182+
Keep having fun :smiley:!
183+
184+
::archive[Project: Bouncy Hedgehog 100%]{name="bouncy-hedgehog-100"}

0 commit comments

Comments
 (0)