Skip to content

Commit d5b0d49

Browse files
committed
add
1 parent 8ba378b commit d5b0d49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+875
-0
lines changed

java/greenfoot/bump-babies/Baby.class

1.28 KB
Binary file not shown.

java/greenfoot/bump-babies/Baby.ctxt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#BlueJ class context
2+
comment0.target=Baby
3+
comment0.text=\n\ Write\ a\ description\ of\ class\ Baby\ here.\n\ \n\ @author\ (your\ name)\ \n\ @version\ (a\ version\ number\ or\ a\ date)\n
4+
comment1.params=id\ x\ y
5+
comment1.target=Baby(int,\ int,\ int)
6+
comment2.params=
7+
comment2.target=int\ getId()
8+
comment3.params=
9+
comment3.target=int\ getX()
10+
comment4.params=
11+
comment4.target=int\ getY()
12+
comment5.params=
13+
comment5.target=void\ act()
14+
comment5.text=\n\ Act\ -\ do\ whatever\ the\ Baby\ wants\ to\ do.\ This\ method\ is\ called\ whenever\n\ the\ 'Act'\ or\ 'Run'\ button\ gets\ pressed\ in\ the\ environment.\n
15+
numComments=6

java/greenfoot/bump-babies/Baby.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
2+
import java.util.Random;
3+
4+
/**
5+
* Write a description of class Baby here.
6+
*
7+
* @author (your name)
8+
* @version (a version number or a date)
9+
*/
10+
public class Baby extends Actor
11+
{
12+
private static final Random _rand = new Random(37L);
13+
private int id;
14+
private int x;
15+
private int y;
16+
private int xDelta;
17+
private int yDelta;
18+
19+
public Baby(int id, int x, int y) {
20+
this.id = id;
21+
this.x = x;
22+
this.y = y;
23+
this.xDelta = _rand.nextDouble() < 0.5d ? -1 : 1;
24+
this.yDelta = _rand.nextDouble() < 0.5d ? -1 : 1;
25+
}
26+
27+
public int getId() { return id; }
28+
public int getX() { return x; }
29+
public int getY() { return y; }
30+
/**
31+
* Act - do whatever the Baby wants to do. This method is called whenever
32+
* the 'Act' or 'Run' button gets pressed in the environment.
33+
*/
34+
public void act() {
35+
x = x + xDelta;
36+
y = y + yDelta;
37+
38+
setLocation(x, y);
39+
40+
if (x == 0 || x == getWorld().getWidth()) {
41+
xDelta = xDelta * -1;
42+
}
43+
if (y == 0 || y == getWorld().getHeight()) {
44+
yDelta = yDelta * -1;
45+
}
46+
}
47+
}

java/greenfoot/bump-babies/Fish.class

1 KB
Binary file not shown.

java/greenfoot/bump-babies/Fish.ctxt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#BlueJ class context
2+
comment0.target=Fish
3+
comment0.text=\n\ Write\ a\ description\ of\ class\ Fish\ here.\n\ \n\ @author\ (your\ name)\ \n\ @version\ (a\ version\ number\ or\ a\ date)\n
4+
comment1.params=
5+
comment1.target=void\ act()
6+
comment1.text=\n\ Act\ -\ do\ whatever\ the\ Fish\ wants\ to\ do.\ This\ method\ is\ called\ whenever\n\ the\ 'Act'\ or\ 'Run'\ button\ gets\ pressed\ in\ the\ environment.\n
7+
comment2.params=
8+
comment2.target=void\ babyHitDetection()
9+
numComments=3

java/greenfoot/bump-babies/Fish.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
2+
3+
/**
4+
* Write a description of class Fish here.
5+
*
6+
* @author (your name)
7+
* @version (a version number or a date)
8+
*/
9+
public class Fish extends Actor
10+
{
11+
/**
12+
* Act - do whatever the Fish wants to do. This method is called whenever
13+
* the 'Act' or 'Run' button gets pressed in the environment.
14+
*/
15+
public void act()
16+
{
17+
try {
18+
MouseInfo mouseInfo = Greenfoot.getMouseInfo();
19+
if (null != mouseInfo) {
20+
setLocation(mouseInfo.getX(), mouseInfo.getY());
21+
}
22+
} catch (Exception e) {
23+
// swallow exception
24+
}
25+
26+
babyHitDetection();
27+
}
28+
29+
public void babyHitDetection() {
30+
Actor actor = getOneIntersectingObject(Baby.class);
31+
if (actor != null) {
32+
Baby baby = (Baby) actor;
33+
((MyWorld)getWorld()).remove(actor);
34+
}
35+
}
36+
}
1.95 KB
Binary file not shown.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#BlueJ class context
2+
comment0.target=MyWorld
3+
comment0.text=\n\ Write\ a\ description\ of\ class\ MyWorld\ here.\n\ \n\ @author\ (your\ name)\ \n\ @version\ (a\ version\ number\ or\ a\ date)\n
4+
comment1.params=
5+
comment1.target=MyWorld()
6+
comment1.text=\n\ Constructor\ for\ objects\ of\ class\ MyWorld.\n\ \n
7+
comment2.params=
8+
comment2.target=Baby\ nextBaby()
9+
comment3.params=actor
10+
comment3.target=void\ remove(greenfoot.Actor)
11+
numComments=4
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
2+
import java.util.List;
3+
import java.util.ArrayList;
4+
import java.util.Random;
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
7+
/**
8+
* Write a description of class MyWorld here.
9+
*
10+
* @author (your name)
11+
* @version (a version number or a date)
12+
*/
13+
public class MyWorld extends World
14+
{
15+
private static final Random _rand = new Random(37L);
16+
private static final AtomicInteger _babyCounter = new AtomicInteger(0);
17+
18+
Fish fish;
19+
List<Baby> babies;
20+
final int numBabies = 20;
21+
/**
22+
* Constructor for objects of class MyWorld.
23+
*
24+
*/
25+
public MyWorld()
26+
{
27+
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
28+
super(600, 400, 1);
29+
30+
fish = new Fish();
31+
addObject(fish, getWidth() / 2, getHeight() / 2);
32+
33+
babies = new ArrayList();
34+
for (int i = 0; i < numBabies; i++) {
35+
Baby baby = nextBaby();
36+
babies.add(baby);
37+
addObject(baby, baby.getX(), baby.getY());
38+
}
39+
40+
}
41+
42+
private Baby nextBaby() {
43+
int x = _rand.nextInt(getWidth());
44+
int y = _rand.nextInt(getHeight());
45+
Baby baby = new Baby(_babyCounter.getAndIncrement(), x, y);
46+
System.out.println(baby.getId());
47+
return baby;
48+
}
49+
50+
public void remove(Actor actor) {
51+
babies.remove(actor);
52+
removeObject(actor);
53+
54+
int threshold = _rand.nextInt(100) + 1;
55+
if (babies.size() <= threshold) {
56+
int numToAdd = _rand.nextInt(5) + 1;
57+
for (int i = 0; i < numToAdd; i++) {
58+
Baby baby = nextBaby();
59+
babies.add(baby);
60+
addObject(baby, baby.getX(), baby.getY());
61+
}
62+
}
63+
}
64+
}

java/greenfoot/bump-babies/README.TXT

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
------------------------------------------------------------------------
2+
This is the project README file. Here, you should describe your project.
3+
Tell the reader (someone who does not know anything about this project)
4+
all he/she needs to know. The comments should usually include at least:
5+
------------------------------------------------------------------------
6+
7+
PROJECT TITLE:
8+
PURPOSE OF PROJECT:
9+
VERSION or DATE:
10+
HOW TO START THIS PROJECT:
11+
AUTHORS:
12+
USER INSTRUCTIONS:
936 Bytes
Loading
3.57 KB
Loading
1.7 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#Greenfoot project file
2+
class.Baby.image=baby2.png
3+
class.Fish.image=fish3.png
4+
class.MyWorld.image=bluerock.jpg
5+
dependency1.from=MyWorld
6+
dependency1.to=Fish
7+
dependency1.type=UsesDependency
8+
dependency2.from=MyWorld
9+
dependency2.to=Baby
10+
dependency2.type=UsesDependency
11+
dependency3.from=Fish
12+
dependency3.to=Baby
13+
dependency3.type=UsesDependency
14+
dependency4.from=Fish
15+
dependency4.to=MyWorld
16+
dependency4.type=UsesDependency
17+
editor.fx.0.height=0
18+
editor.fx.0.width=0
19+
editor.fx.0.x=0
20+
editor.fx.0.y=0
21+
height=585
22+
package.numDependencies=4
23+
package.numTargets=3
24+
project.charset=UTF-8
25+
publish.hasSource=false
26+
publish.locked=true
27+
publish.longDesc=
28+
publish.shortDesc=
29+
publish.tags=
30+
publish.title=
31+
publish.url=
32+
readme.height=58
33+
readme.name=@README
34+
readme.width=47
35+
readme.x=10
36+
readme.y=10
37+
simulation.speed=58
38+
target1.height=50
39+
target1.name=Baby
40+
target1.showInterface=false
41+
target1.type=ClassTarget
42+
target1.width=80
43+
target1.x=0
44+
target1.y=0
45+
target2.height=50
46+
target2.name=Fish
47+
target2.showInterface=false
48+
target2.type=ClassTarget
49+
target2.width=80
50+
target2.x=0
51+
target2.y=0
52+
target3.height=50
53+
target3.name=MyWorld
54+
target3.showInterface=false
55+
target3.type=ClassTarget
56+
target3.width=80
57+
target3.x=0
58+
target3.y=0
59+
version=3.0.0
60+
width=825
61+
world.lastInstantiated=MyWorld
62+
xPosition=1385
63+
yPosition=610
1.41 KB
Binary file not shown.

java/greenfoot/race-car/GreenCar.ctxt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#BlueJ class context
2+
comment0.target=GreenCar
3+
comment0.text=\n\ Write\ a\ description\ of\ class\ GreenCar\ here.\n\ \n\ @author\ (your\ name)\ \n\ @version\ (a\ version\ number\ or\ a\ date)\n
4+
comment1.params=world
5+
comment1.target=GreenCar(greenfoot.World)
6+
comment2.params=
7+
comment2.target=void\ act()
8+
comment2.text=\n\ Act\ -\ do\ whatever\ the\ GreenCar\ wants\ to\ do.\ This\ method\ is\ called\ whenever\n\ the\ 'Act'\ or\ 'Run'\ button\ gets\ pressed\ in\ the\ environment.\n
9+
comment3.params=
10+
comment3.target=void\ checkCollision()
11+
comment4.params=
12+
comment4.target=int\ getX()
13+
comment5.params=
14+
comment5.target=int\ getY()
15+
numComments=6

java/greenfoot/race-car/GreenCar.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
2+
3+
/**
4+
* Write a description of class GreenCar here.
5+
*
6+
* @author (your name)
7+
* @version (a version number or a date)
8+
*/
9+
public class GreenCar extends Actor
10+
{
11+
private int x;
12+
private int y;
13+
14+
public GreenCar(World world) {
15+
int wWidth = world.getWidth();
16+
int wHeight = world.getHeight();
17+
int iWidth = getImage().getWidth();
18+
int iHeight = getImage().getHeight();
19+
20+
x = (wWidth / 2) - (iWidth / 2);
21+
y = wHeight - (iHeight / 2);
22+
}
23+
/**
24+
* Act - do whatever the GreenCar wants to do. This method is called whenever
25+
* the 'Act' or 'Run' button gets pressed in the environment.
26+
*/
27+
public void act()
28+
{
29+
if (Greenfoot.isKeyDown("left")) {
30+
x = x - 1;
31+
} else if (Greenfoot.isKeyDown("right")) {
32+
x = x + 1;
33+
} else if (Greenfoot.isKeyDown("up")) {
34+
y = y - 1;
35+
} else if (Greenfoot.isKeyDown("down")) {
36+
y = y + 1;
37+
}
38+
setLocation(x, y);
39+
checkCollision();
40+
}
41+
42+
public void checkCollision() {
43+
RedCar redCar = (RedCar) getOneIntersectingObject(RedCar.class);
44+
if (null != redCar) {
45+
Greenfoot.stop();
46+
}
47+
}
48+
49+
public int getX() { return x; }
50+
public int getY() { return y; }
51+
}

java/greenfoot/race-car/MyWorld.class

2.9 KB
Binary file not shown.

java/greenfoot/race-car/MyWorld.ctxt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#BlueJ class context
2+
comment0.target=MyWorld
3+
comment0.text=\n\ Write\ a\ description\ of\ class\ MyWorld\ here.\n\ \n\ @author\ (your\ name)\ \n\ @version\ (a\ version\ number\ or\ a\ date)\n
4+
comment1.params=
5+
comment1.target=MyWorld()
6+
comment1.text=\n\ Constructor\ for\ objects\ of\ class\ MyWorld.\n\ \n
7+
comment2.params=
8+
comment2.target=void\ act()
9+
comment3.params=
10+
comment3.target=void\ addRedCar()
11+
comment4.params=redCar
12+
comment4.target=void\ removeRedCar(RedCar)
13+
comment5.params=
14+
comment5.target=long\ getScore()
15+
comment6.params=
16+
comment6.target=int\ getLevel()
17+
comment7.params=
18+
comment7.target=int\ getRandomXPosition()
19+
comment8.params=
20+
comment8.target=java.util.List\ getXPositions()
21+
numComments=9

0 commit comments

Comments
 (0)