Book Image

Creative Greenfoot: RAW

By : Michael Haungs
Book Image

Creative Greenfoot: RAW

By: Michael Haungs

Overview of this book

Table of Contents (17 chapters)
Creative Greenfoot
About the Author
About the Reviewers


This book is designed to help you learn how to program games and other interactive applications quickly using a learn-by-doing approach. Unlike other texts, which start with a detailed description of all aspects of a language or development platform, we will only cover exactly what is needed for the task at hand. As you progress through the book, your programming skill and ability will grow as you learn topics such as animation, collision detection, artificial intelligence, and game design. Project-based learning is a proven approach and becoming prominent in primary, secondary, and higher education. It enhances the learning process and improves knowledge retention.

The topics presented in this book closely follow the ones I cover in my game design class. Through years of teaching this material, I have found that a project-based learning approach can quickly get students successfully programming and creating interesting games and applications. I hope that you too will be amazed with how much you can accomplish in a short amount of time.

We will code our games in Java. Java is one of the most popular and powerful programming languages in the world and is widely used in the finance industry, gaming companies, and research institutions. We will be doing our programming in Greenfoot (—an interactive Java development environment. This environment allows both novice and experienced programmers to quickly create visually appealing applications. It provides a safe environment for experimentation and allows you to share your work on a variety of platforms.

To get the most out of this book, you should:

  • Open Greenfoot and code as you are reading the book

  • Experiment with the code you have after completing a chapter

  • Know that some details not covered in a chapter will be addressed in an upcoming chapter

  • Be proud of your accomplishments and share them with friends, family, and the Greenfoot community

Learning is not a passive activity. Dig into each chapter and experiment, add your own unique twists, and then code something uniquely your own. I can't wait to see what you can do.

What this book covers

Chapter 1, Let's Dive Right in…, takes you through a complete tutorial for creating a simple game complete with an introduction screen, game over screen, a score, mouse input, and sound. This tutorial serves the purpose of introducing you to Greenfoot basics, Java basics, and good programming practices.

Chapter 2, Animation, discusses how to perform animation in Greenfoot. Animation requires appropriate and well-timed image swapping as well as realistic movement around the screen. After reading the given topic and seeing an example, you will apply learned animation techniques to the game you created in Chapter 1, Let's Dive Right in….

Chapter 3, Collision Detection, discusses why collision detection is necessary for most simulations and games. You will learn how to use Greenfoot's built-in collision detection mechanisms and then learn more accurate methods to do collision detection. You will use both border-based and hidden-sprite methods of collision detection to create a zombie invasion simulation.

Chapter 4, Projectiles, talks about how actors in creative Greenfoot often have movement that can best be described as being launched. A soccer ball, bullet, laser, light ray, baseball, and firework are examples of this type of object. You will learn how to implement this type of propelled movement. You will also learn how gravity, if present, affects it by working through the implementation of a comprehensive platform game.

Chapter 5, Interactive Application Design and Theory, discusses creating engaging and immersive experiences in Greenfoot, which is far more involved than compiling a collection of programming effects into one application. In this chapter, you will learn how to engage your user by understanding the relationship between user choice and outcome, conditioning the user, and including the right level of complexity into your work. You will be shown a proven iterative development process that helps you put the theory into practice.

Chapter 6, Scrolling and Mapped Worlds, discusses how to create worlds that are much more extensive than the ones that can fit into the confines of a single screen. At the beginning of the chapter, you will code a scrolling exploration game and by the end of the chapter you will expand it into a large mapped game.

Chapter 7, Artificial Intelligence, talks about how AI, despite being a deep and complex topic, has some simple techniques you can learn to give the illusion of having intelligent, autonomous actors in your worlds. First, you will learn how to effectively use random behaviors. Next, you will implement simple heuristics to simulate intelligent behavior. Last, you will learn the A* search algorithm to allow game actors to intelligently bypass obstacles when moving between two locations on the screen.

Chapter 8, User Interfaces, discusses adding an interface to your Greenfoot scenarios. In this chapter, you will learn how to communicate with your user through buttons, labels, menus, and a heads-up display.

Chapter 9, Gamepads in Greenfoot, discusses the capabilities of a gamepad device and then teaches you how to set up Greenfoot to work with it. You will then add gamepad support to the game we created in Chapter 1, Let's Dive Right in…, and Chapter 2, Animation.

Chapter 10, What to Dive into Next…, gives you an opportunity to reflect on the skills you learned during the course of this book. I then go on to suggest projects you should attempt in order to continue your journey as a programmer and interactive application author.

What you need for this book

For this book, you will need to download Greenfoot from and install it on your computer. Greenfoot is free and works on Windows, Mac, and Linux. The Greenfoot website provides easy-to-follow installation instructions. After installation, you should work through the six simple tutorials found on These tutorials can be completed in less than two hours and will give you all you need to know to get the most from this book.

Who this book is for

If you are ready to explore the world of creative programming, then you will appreciate the methods, tips, and processes described in this book. Appropriate for Java programmers of all levels (novice to expert), it methodically guides you through topics crucial to building engaging interactive applications. You will learn how to build games, simulations, and animations through guided programming exercises.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "As you can see, we made some really simple changes to the Enemy class."

A block of code is set as follows:

private void increaseLevel() {
  int score = scoreBoard.getValue();

  if( score > nextLevel ) {
    enemySpawnRate += 2;
    nextLevel += 100;

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

  public void act() {
    if( Greenfoot.mouseClicked(this) ) {
      AvoiderWorld world = new AvoiderWorld(pad);

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Hit the Ok button in the New class pop-up window, and then, in the main scenario window, hit the Compile button."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from:


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from


Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.