This site requires JavaScript, please enable it in your browser!
Greenfoot back
VanishingDog
VanishingDog wrote ...

2019/11/15

Newtons Project - Greenfoot Textbook

VanishingDog VanishingDog

2019/11/15

#
I need help spawning in planets with the numpad. Here is the Space code and Body code could anyone tell me what I am doing wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
******Body*******
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
 
/**
 * A 'Body' is any kind of object in space that has a mass. It could be a star, or a planet,
 * or anything else that floats around in space.
 *
 * @author Michael Kolling
 * @version 0.1
 */
public class Body extends SmoothMover
{
    // constants
    private static final double GRAVITY = 5.8;
    private static final Color defaultColor = new Color(255, 216, 0);
     
    // fields
    private double mass;
     
 
    private boolean keydown;
     
    /**
     * Construct a Body with default size, mass, movement and color.
     */
    public Body()
    {
        this (20, 300, new Vector(0, 1.0), defaultColor);
    }
     
    /**
     * Construct a Body with a specified size, mass, movement and color.
     */
    public Body(int size, double mass, Vector movement, Color color)
    {
        this.mass = mass;
        addForce(movement);
        GreenfootImage image = new GreenfootImage (size, size);
        image.setColor (color);
        image.fillOval (0, 0, size-1, size-1);
        setImage (image);
    }
     
    /**
     * Act. That is: apply  the gravitation forces from
     * all other bodies around, and then move.
     */
    public void act()
    {
        move();
        applyForces();
        bounceAtEdge();
        numPad();
    }
     
    /**
     * Return the mass of this body.
     */
    public double getMass()
    {
        return mass;
    }
     
    private void applyForces()
    {
        List<Body> bodies = getWorld().getObjects(Body.class);
        for(Body body : bodies)
        {
            if (body != this)
            {
                applyGravity (body);
            }
        }
    }
    private void applyGravity(Body other)
    {
        double dx = other.getExactX() - this.getExactX();
        double dy = other.getExactY() - this.getExactY();
        Vector force = new Vector (dx, dy);
        double distance = (dx*dx + dy*dy);
        double strength = (GRAVITY * this.mass * other.mass / (distance * distance));
        double acceleration = strength / this.mass;
        force.setLength (acceleration);
        addForce (force);
         
    }
    private void bounceAtEdge()
    {
        if(getX() == getWorld().getWidth()-1)
        {
            setLocation((double)getX(), (double)getY());
            getMovement().revertHorizontal();
            accelerate(0.9);
        }
        if(getX() == 0)
        {
            setLocation((double)getX(), (double)getY());
            getMovement().revertHorizontal();
            accelerate(0.9);
        }
        if(getY() == getWorld().getHeight()-1)
        {
            setLocation((double)getX(), (double)getY());
            getMovement().revertVertical();
            accelerate(0.9);
        }
        if(getY() == 0)
        {
            setLocation((double)getX(), (double)getY());
            getMovement().revertVertical();
            accelerate(0.9);
        }
    }
     
    public void numPad()
    {
        int num = 0;
        if (Greenfoot.isKeyDown("0"))
        {
            getWorld().removeObjects(getWorld().getObjects(Body.class));
        }
         
        if (Greenfoot.isKeyDown("1")){num = 1;}
        if (Greenfoot.isKeyDown("2")){num = 2;}
        if (Greenfoot.isKeyDown("3")){num = 3;}
        if (Greenfoot.isKeyDown("4")){num = 4;}
        if (Greenfoot.isKeyDown("5")){num = 5;}
        if (Greenfoot.isKeyDown("6")){num = 6;}
        if (Greenfoot.isKeyDown("7")){num = 7;}
        if (Greenfoot.isKeyDown("8")){num = 8;}
        if (Greenfoot.isKeyDown("9")){num = 9;}
                
        for (int loop = 0; loop < 0; loop--)
        {
            int size = 20 + Greenfoot.getRandomNumber(40);
            double mass = size * Greenfoot.getRandomNumber(10);
            int degree = Greenfoot.getRandomNumber(360);
            double speed = Greenfoot.getRandomNumber(5);
            int r = Greenfoot.getRandomNumber(255);
            int g = Greenfoot.getRandomNumber(255);
            int b = Greenfoot.getRandomNumber(255);
            int x = Greenfoot.getRandomNumber(getWorld().getWidth());
            int y = Greenfoot.getRandomNumber(getWorld().getHeight());
            getWorld().addObject (new Body(size, mass, new Vector(degree, speed), new Color(r,g,b)), x, y);
            num--;
        }
         
        }
        
}
 
 
 
*******Space********
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
/**
 * Space. The final frontier.
 *
 * @author Michael Kolling
 * @version 1.0
 */
public class Space extends World
{
 
    /**
     * Create space.
     */
    public Space()
    {   
        super(960, 620, 1);
        randomPlanets();
        // Uncomment one of the following method calls if you want the objects created automatically:
         
        //sunAndPlanet();
        //sunAndTwoPlanets();
        //sunPlanetMoon();
    }
     
    /**
     * Set up the universe with a sun and a planet.
     */
    public void sunAndPlanet()
    {
        removeAllObjects();
        addObject (new Body (50, 240.0, new Vector(270, 0.03), new Color(255, 216, 0)), 460, 270);
        addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 695, 260);
    }
 
    /**
     * Set up the universe with a sun and two planets.
     */
    public void sunAndTwoPlanets()
    {
        removeAllObjects();
        addObject (new Body (50, 240.0, new Vector(270, 0.0), new Color(255, 216, 0)), 460, 310);
        addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 695, 300);
        addObject (new Body (24, 4.6, new Vector(270, 1.8), new Color(248, 160, 86)), 180, 290);
    }
 
    /**
     * Set up the universe with a sun, a planet, and a moon.
     */
    public void sunPlanetMoon()
     {
        removeAllObjects();
        addObject (new Body (50, 240.0, new Vector(270, 0.0), new Color(255, 216, 0)), 460, 270);
        addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 720, 260);
        addObject (new Body (5, 0.8, new Vector(90, 3.25), new Color(240, 220, 96)), 748, 260);
    }
 
    /**
     * Remove all objects currently in the world.
     */
    private void removeAllObjects()
    {
        removeObjects (getObjects(Actor.class));
    }
     
    //Randomly Generate Planets
    public void randomPlanets()
    {
        for(int i = 10 + Greenfoot.getRandomNumber(10); i > 0; i--)
        {
            int size = 20 + Greenfoot.getRandomNumber(40);
            double mass = size * Greenfoot.getRandomNumber(10);
            int degree = Greenfoot.getRandomNumber(360);
            double speed = Greenfoot.getRandomNumber(5);
            int r = Greenfoot.getRandomNumber(255);
            int g = Greenfoot.getRandomNumber(255);
            int b = Greenfoot.getRandomNumber(255);
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(new Body(size, mass, new Vector(degree, speed), new Color(r,g,b)), x, y);
        }
    }
     
}
danpost danpost

2019/11/15

#
Use getKey instead of isKeyDown. It could start as follows:
1
2
3
4
5
6
7
8
9
String key = Greenfoot.getKey();
if (key == null) return; // no key detected
int num = "0123456789".indexOf(key);
if (num < 0) return; // invalid key detected
if (num == 0) getWorld().removeObjects(getWorld().getObjects(Body.class)); // "0" detected
else // "1" thru "9" detected
for (int loop=0; loop<num; loop++)
{
    ...
Do not put your line 146 in the loop.
You need to login to post a reply.