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

Dynamic classes in Greenfoot

Greenfoot now has many classes which provide live information through web services. Your scenarios could be more interesting and rich by using dynamic images from GoogleMaps as background, for example, or by showing/controlling actors depending on the Weather status.

The next video is an introduction which summarises how to make use of these classes in simulation/games scenarios. You can find links to each scenario’s source-code and tutorial video in the description below. However, the whole video tutorials can be found in our youTube channel as well.

Using Maps in the World class (background)

From the Edit menu in greenfoot 2.4.0 (and after), you can open the Import Class dialog and then select the Map class to be included into your scenario. After doing this, you can simply create a object of Map into the constructor of your World class and pass the map image, obtained by getImage() on this map object, into the setBackground() method of the world class. The Map class has two constructors: the first takes only the location, and the other takes location, width and height of the image, and the zooming level of the needed map (more details can be found in this class documentation).

To illustrate this, two scenarios are provided:

  1. Ants with GoogleMaps: a simulation of Ants behaviour searching for food (run in the browser, source code opened by greenfoot).

  2. Boids with GoogleMaps: a simulation of Birds behaviour flying in groups (run in the browser, source code opened by greenfoot).

Using Maps as an Actor class

After importing the Map class, as mentioned in the previous paragraph, you can assign set an Actor’s image as a map by creating a object of Map into its constructor and pass the map image, obtained by getImage() on this map object. In the run-time, the image properties can be changed (e.g. location, size, zooming level and the map type). this can be done through the act() method of the Actor. You can give the user this capability as well by adding public methods, so he/she can right click and change the map properties.

"Maps as Actors" is a scenario provided with video tutorials to demonstrate these capabilities. You can run it in the browser and download the source code to be opened by greenfoot.

Detect Land vs. Water automatically in Maps for more interesting games

If you select a map of a specific location as a background for your game, you probably like the game to recognise different regions of the map for more advance features. In other words, you may want two players to have borders, or you like that players (actors) have different behaviours in sea than on land. This can be achieved somehow easily by a two-lines function which classify the areas based on the pixels' colours.

"Ketchup Cannon" is a simple fun game which been updated to have a map in the background. When the cannon shots the ketchup blobs they will splat only if they hit a land, but sink when they get into the ocean. You can run it in the browser and download the source code to be opened by greenfoot.

Weather Information

There is another dynamic helper class has been added to greenfoot since version (2.4.0). This class contains methods that use web services to fetch weather status locally or for any city in the world. These information could be printed easily on the terminal, showed graphically on a map, or even used to control other actors in games or simulation.

Greenfoot Dynamic classes #4 Weather is a video tutorial demonstrates the functionalities available of the weather class.

Other helper classes

There are many other helper classes which are not depending on web services. To illustrate their usability, an interactive Boat racing game that leverages some of them is presented in the next video tutorial. The scenario could be tested in the browser, and downloaded with the source code to be opened by greenfoot. The speed of the boats depend on real-time wind speed in selected cities. In addition to classes mentioned previously, this scenario uses:

  1. "SmoothMover:" a variation of an actor that maintains a precise location by using doubles for the co-ordinates instead of integers. This allows small precise movements (e.g. movements of 1 pixel or less) that do not lose precision.

  2. "Label:" a class that enable a textual display of a value on the world. This class is an Actor, so you will need to create it, and then add it to the world in Greenfoot. If you keep a reference to the Label then you can change the text it displays and/or its properties. An easier way that replaces this class, and more suitable for beginners, is showText() method in the world object. However, as this method doesn't return a reference for an object, so it is not possible to change the properties of the text once written.