Additional scenario ideas

Additional scenario ideas

topics:

ideas for more scenarios

concepts: (no new concepts introduced)

This is the last chapter of this book. It is different from the other chapters in that it does not try to teach you any new concepts or techniques of programming. Instead it briefly presents a number of additional scenarios to give you some ideas for other things you might like to investigate and work on.

All scenarios presented here are also available as Greenfoot projects with source code in the book scenarios. However, most of them are not complete implementations of the idea they represent.

Some scenarios are almost complete, and you may like to study them to learn further techniques and see how certain effects were achieved. Others are beginnings, partial implementations of an idea which you could take as a starting point for your own project. Yet other ones are illustrations of a single concept or idea that might provide inspiration for something you could incorporate into one of your own scenarios.

In short, view these as a collection of ideas for future projects, and study them for a small glimpse into what else is possible for a competent programmer to achieve.

10.1 Marbles

The marbles scenario (Figure 10.1) implements a game in which you roll a golden ball over a board with the aim of clearing the board of all silver balls within a limited number of moves. The game is reasonably complete.

Several things are worth observing about this scenario. The first thing that stands out is that it looks quite nice. This has very little to do with Java or Greenfoot programming and is mostly due to the use of nice graphics. Using nicely designed graphics and sounds can make a big difference in the attractiveness of a game.

Marbles uses a nice looking background image (the game board and scroll for the text display) and actors with semi-transparent drop shadows (the marbles and the obstacles).

10.2 Lifts

Figure 10.1 The Marbles game

The other interesting aspect to examine is the collision detection. The marbles do not use any of the built-in Greenfoot collision detection methods, since these all work on the rectangular actor images. The marbles, on the other hand, are round, and we need precise collision for this.

Luckily, when the actors are round, this is not very difficult. Two marbles collide if their distance (measured from their center points) is less than their diameter. We know the diameter, and the distance is fairly easy to compute (using the Pythagoras theorem).

The next interesting thing is the way the new movement direction of a colliding marble is computed. There is a little trigonometry involved here, but if you are familiar with that, then it is not too hard.

Collisions with the fixed obstacles are easier, since they are always horizontally or vertically oriented rectangles. Therefore, a marble hitting one of these obstacles simply reverses its direction along one of the axes (x or y).

You could reuse the marble collision logic for all sorts of other games that involve collision of round objects.

10.2 Lifts

The lifts scenario (Figure 10.2) shows a simple lift (or elevator) simulation. It shows several floors of a multistory building and three lifts moving up and down. People appear on the floors and press the call buttons and enter the lifts when they come.

| Chapter 10 ■ Additional scenario ideas

Figure 10.2 A (partial) lift simulation

This is actually a very rudimentary, unfinished implementation. Much of what we see on the screen is fake: It does not properly simulate what is going on, and is just written for show effect.

For example, the people do not properly enter the lifts (they are just erased when a lift reaches a floor). The number of people shown in a lift is just a random number. Lifts also do not react to call buttons—they just move up and down randomly. There is no control algorithm implemented for the lifts.

So this is just a quick demo that presents the idea and the graphics. To finish the project, the movement of people would have to be properly modeled (in and out of the elevators). And then we could experiment with implementing and testing different lift control algorithms.

10.3 Boids

The boids example (Figure 10.3) shows a simulation of flocking behavior of birds. The term “boids” comes from a program developed in 1986 by Craig Reynolds that

first implemented this flocking algorithm. In it, each bird flies according to three rules:

10.4 Circles

Figure 10.3 Boids: A simulation of flocking behavior

Separation: steer away from other birds if getting too close.

Alignment: steer toward the average heading of other birds in the vicinity.

Cohesion: steer to move toward the average position of other birds in the vicinity. With this algorithm, the birds develop movement that is quite nice to look at. Included in this

scenario is also obstacle avoidance: trying not to fly into trees.

A version of this algorithm was used, for example, in Tim Burton’s 1992 film “Batman Returns” to create animation for computer-generated swarms of bats and penguin flocks, and in the “Lord of the Rings” films to create the movement of the Orc armies.

The version for this book was written by Poul Henriksen. You can find out much more about this by searching the web for “boids”. And while this sce-

nario currently does nothing other than show the movement, one feels that there has to be a game in it somewhere . . .

10.4 Circles

Circles (Figure 10.4) is a project that does not seem to have much of a purpose but is interesting to play with and nice to look at.

| Chapter 10 ■ Additional scenario ideas

Figure 10.4 “Circles” is a mixture of physics and art

It uses some physical simulation, such as gravity and bouncing off edges, and some randomness to create beautiful moving images. Is it physics? Is it art? Or maybe a bit of both?

Whatever it is, I am sure there are many other ways to produce interesting or beautiful images, patterns, or color animations. (One idea might be to combine the circles with the collision detec- tion algorithm from the Marbles game.)

Circles was written by Joe Lenton.

10.5 Explosion

The explosion scenario (Figure 10.5) demonstrates how we can implement a more spectacular looking explosion effect. The object that explodes is, in this case, a simple rock that we have encountered in other scenarios before. (It played, for example, the role of the asteroid in the asteroids scenario.) But we could really explode anything we like.

To achieve this effect, we have a Debris class that represents a part of the rock. When the rock explodes, we remove it from the world and place 40 pieces of debris in its place.

Each piece of debris is randomly stretched and rotated to make it look somewhat unique, and initially has a movement vector in a random direction. At every step, we add a bit of

10.6 Breakout

Figure 10.5 An explosion effect

downward movement to simulate gravity, and the result is the explosion you see when you run this scenario.

A tutorial video explaining this scenario in more detail is available on the Greenfoot web site at http://www.greenfoot.org/doc/videos.html .

10.6 Breakout

“Breakout” (Figure 10.6) is a classic computer game in which the player controls a paddle at the bottom of the screen to bounce a ball upwards to remove some blocks. If you do not know the game, do a web search and you will quickly find out.

The breakout scenario is a partial implementation of this game. It uses the ball with the smoke effect that we discussed in Chapter 8 and adds a paddle for the player to control the ball. It has, however, no blocks to aim for, so in its current form it is not very interesting.

Many variations of breakout have been created over time. Many use different patterns of layout for the blocks at different levels. Most also have some “power-ups”—goodies hidden behind some blocks that float down when the block is removed. Catching them typically makes some- thing interesting happen in the game (extra balls, increased speed, larger or smaller paddles, etc.)

Completing this game in an interesting way can make a good project. It could also be modified to have two paddles, one on either side, essentially turning it into the classic “Pong” game.

| Chapter 10 ■ Additional scenario ideas

Figure 10.6 The beginning of a “breakout” game

10.7 Platform jumper

A very common style of game is a “platform” game. The player typically controls a game char- acter that has to move from one area on the screen to another, while overcoming various obsta- cles. One such obstacle may be a gap in the ground the character is walking on, with some means of getting across it.

The pengu scenario (Figure 10.7) implements a small segment of such a game. There are two pieces of ground on either side of the screen, and the penguin can get across by jumping onto a moving cloud.

This scenario is included here to demonstrate how an actor can move along the top of another (the penguin on top of the ground), and how jumping and falling might be implemented.

10.8 Wave

Figure 10.7 A start of a simple platform jumper game

A tutorial video discussing this in more detail is available on the Greenfoot web site at http://www.greenfoot.org/doc/videos.html , under the name “Running, jumping and falling”.

10.8 Wave

The last scenario presented here is called wave (Figure 10.8). It is a simple simulation of the propagation of a wave on a piece of string. Play around with it for a little while, and you will discover what it does.

One of the fascinating aspects of this example is how a fairly simple implementation—in each act round, each bead simply moves toward the middle of its two neighbors—achieves a quite sophisticated simulation of various aspects of wave propagation.

This example is included here to illustrate that, with a bit of thought and preparation, various behaviors from other disciplines could be simulated. In this case, it is a simple physical effect. Equally, one could simulate chemical reactions, biological interactions, interactions of sub- atomic particles, and much more. With some careful planning, we can learn something about other application areas, as well as learning about programming.

This scenario also implements slider and switch controls, which may be useful in other projects.

Chapter 10 | ■ Additional scenario ideas

Figure 10.8 Simulation of wave propagation on a string of beads

10.9 Summary

In this concluding chapter of our book, we have tried to show that there are many more direc- tions you can follow, beyond the few examples that we have discussed in more detail throughout this book.

As you become more experienced, you will become more confident and more able to turn your ideas into reality as you develop your programs. As a programmer, an infinite world of creative endeavor lies in front of you, both within Greenfoot and without, using other development envi- ronments.

When you program in other environments, outside of Greenfoot, you will have to learn new skills and techniques, but everything you have learned using Greenfoot will be useful and applicable.

If you have followed this book all the way through to this point, you have learned a great deal about programming in Java, and indeed programming in an object-oriented language in general. In learning to program, the beginning is always the hardest part, and you have that behind you.

If you would like support and ideas for further Greenfoot programming, make use of the Greenfoot web site. 1 Use the Greenfoot Gallery to publish your scenarios, look at other people’s work, and get some ideas. Look at the video tutorials for tips and tricks. And join the discussion group to chat to other Greenfoot programmers, get and give help, and discuss new ideas.

We hope that you have come to enjoy programming as much as we do. If you have, a whole new world lies before you. Program, enjoy, and be creative!

1 www.greenfoot.org