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

2018/6/5

Book Scenarios from First Edition - updated?

stevex stevex

2018/6/5

#
My version of these scenarios referenced in the first edition of Introduction to Programming with Greenfoot no longer work in version 3.1.0 on the Macintosh. Have these been updated for downloading (or instructions posted on how to change the source code to run in this version)?
davmac davmac

2018/6/5

#
I can't remember offhand which version of Greenfoot corresponds to which edition of the book, nor if the scenarios changed much between editions other than being updated for the newer Greenfoot version; however, the book web page (https://www.greenfoot.org/book/ ) has the 2nd edition scenarios available for download. Otherwise, feel free to post any errors that you get when opening older scenarios with the current version of Greenfoot here. We should be able to help you resolve them.
danpost danpost

2018/6/5

#
stevex wrote...
My version of these scenarios referenced in the first edition of Introduction to Programming with Greenfoot no longer work in version 3.1.0 on the Macintosh. Have these been updated for downloading (or instructions posted on how to change the source code to run in this version)?
In your piano-complete scenario, you just need to remove the second line in every class -- this one:
import java.awt.Color;
It most likely is the same with your other scenario.
stevex stevex

2018/6/6

#
No version of Greenfoot ran any book scenario that I have tried. Version 3.1.0 updates the code so it compiles, but nothing happens hitting the run button for the 2 scenarios of chapter 1 for instance, Wombats and Asteroids. Maybe the removed awt libraries that were removed have to be replaced by some other code. If so, is this documented anywhere?
danpost danpost

2018/6/6

#
stevex wrote...
No version of Greenfoot ran any book scenario that I have tried. Version 3.1.0 updates the code so it compiles, but nothing happens hitting the run button for the 2 scenarios of chapter 1 for instance, Wombats and Asteroids. Maybe the removed awt libraries that were removed have to be replaced by some other code. If so, is this documented anywhere?
The greenfoot.Color class, which is imported by the top line of your classes, replaces the java.awt.Color class. So, nothing more needs done with it. Maybe you can post the class codes for review -- say for the Wombat scenario you have.
stevex stevex

2018/6/7

#
WombatWorld Class code: import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage /** * A world where wombats live. * * @author Michael Kölling * @version 2.0 */ public class WombatWorld extends World { /** * Create a new world with 10x10 cells and * with a cell size of 60x60 pixels. */ public WombatWorld() { super(10, 10, 60); setBackground("cell.jpg"); setPaintOrder(Wombat.class, Leaf.class); // draw wombat on top of leaf } /** * Populate the world with a fixed scenario of wombats and leaves. */ public void populate() { addObject(new Wombat(), 7, 1); addObject(new Wombat(), 6, 6); addObject(new Wombat(), 1, 7); randomLeaves(20); } /** * Place a number of leaves into the world at random places. * The number of leaves can be specified. */ public void randomLeaves(int howMany) { for (int i=0; i<howMany; i++) { Leaf leaf = new Leaf(); int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject(leaf, x, y); } } } Wombat Class code: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * Wombat. A Wombat moves forward until it hits the edge of the world, at * which point it turns left. If a wombat finds a leaf, it eats it. * * @author Michael Kölling * @version 2.0 */ public class Wombat extends Actor { private int leavesEaten; public Wombat() { leavesEaten = 0; } /** * Do whatever the wombat likes to to just now. */ public void act() { if (foundLeaf()) { eatLeaf(); } else if (canMove()) { move(); } else { turnLeft(); } } /** * Move one step forward. */ public void move() { move(1); } /** * Turn left by 90 degrees. */ public void turnLeft() { turn(-90); } /** * Check whether there is a leaf in the same cell as we are. * Return true, if there is, false otherwise. */ public boolean foundLeaf() { Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class); return leaf != null; } /** * Eat a leaf (if there is one in our cell). */ public void eatLeaf() { Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class); if (leaf != null) { // eat the leaf... getWorld().removeObject(leaf); leavesEaten = leavesEaten + 1; } } /** * Set the direction we're facing. The 'direction' parameter must * be in the range . */ public void setDirection(int direction) { if ((direction >= 0) && (direction <= 3)) { setRotation(direction * 90); } } /** * Test if we can move forward. Return true if we can, false otherwise. */ public boolean canMove() { int rotation = getRotation(); int x = getX(); int y = getY(); boolean facingEdge = false; switch (rotation) { case 0: facingEdge = (x == getWorld().getWidth() - 1); break; case 90: facingEdge = (y == getWorld().getHeight() - 1); break; case 180: facingEdge = (x == 0); break; case 270: facingEdge = (y == 0); break; } return !facingEdge; } /** * Tell how many leaves we have eaten. */ public int getLeavesEaten() { return leavesEaten; } } Leaf Class: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * Leaf - a class for representing leaves. * * @author Michael Kölling * @version 2.0 */ public class Leaf extends Actor { public Leaf() { } } This code was produced by running Greenfoot 3.1.0 on the book scenarios downloaded from the Volume 2 book site listed above, and yes to the prompt update automatically to the message "Update automatically or manually".
danpost danpost

2018/6/7

#
stevex wrote...
No version of Greenfoot ran any book scenario that I have tried. Version 3.1.0 updates the code so it compiles, but nothing happens hitting the run button for the 2 scenarios of chapter 1 for instance, Wombats and Asteroids.
The only problem I see for the Wombat scenario is that the WombatWorld constructor does not call the populate method.
You need to login to post a reply.