cover.eps

Title page image

Write Code Like a Pro – Create Working Applications

INTRODUCTION

So you want to make your very own videogame? And you want to learn how to code in Java? That is an awesome idea!

Writing Java code can be really hard work, especially writing Java code to make a videogame. For example, the original Legend of Zelda took more than 100 people three years to make! But don’t worry, this book will guide your through all the steps to learn the basics of Java programming and to make two videogames of your own. The first game you’ll make is kind of like Mario, where you have a character that runs across the screen and tries to avoid lava pits and enemies. The second, larger game that you’ll code is kind of like Flappy Bird. It’s called Ninja Bird. Keeping track of scores, making pipes move on their own randomly, and even helping the bird fly — you’ll have your friends playing games you make for hours!

ABOUT THIS BOOK

This book uses a computer application called Processing that was made to make coding Java video games much easier. As you’ll see, you can easily make drawings with different shapes and colors, you can add images that you find online (with adult permission, of course!) into your drawing and games, and it comes with an entire library of code that you get to just use. It’s free, and available online. In the first chapter, you’ll learn how to download it and begin using it in just a couple of simple steps.

Write Code Like a Pro has three different types of programs that you’ll write. First, you’ll make a simple drawing using a lot of what Processing provides to you in its library. With shapes and colors, the first couple chapters of this book guide you through an art program. The next part of this book teaches you how to turn that drawing into an animation — making shapes and images move around the canvas. Finally, you’ll get a chance to build two different videogames.

Though this book will guide you step-by-step in making these games, you’re highly encouraged to keep coding after you finish this book and make variations of the games you learn here! This is just the beginning of your coding and videogame development career. With this book as a starting point, you never know what you can achieve!

FOOLISH ASSUMPTIONS

Everybody has to start somewhere, right? I had to start writing this book by assuming that you’re comfortable doing this stuff:

ICONS USED IN THIS BOOK

As you read through the projects in this book, you’ll see icons. The icons point out different things:

remember The Remember icon highlights ideas that you should keep in mind.

tip The Tip icon marks advice and shortcuts that can make coding easier.

WHERE TO GO FROM HERE

In this book, it’s best if you start from Chapter 1 and move through to Chapter 12. There are some things you learn in early chapters that really help you complete the larger problems in later chapters. The chapters in this book do fall under three categories: Chapters 1 through 6 guide you through the basics of coding in Java with a drawing canvas; you’ll get to learn how to make shapes, add color, make objects move, and even have user interaction — all very important for coding a videogame. Chapters 7 through 8 guide you through making your very first videogame! This game is similar to Mario, but just a preview to videogame making. Finally, Chapters 9 through 12 guide you through making Ninja Bird, which is a game like Flappy Bird.

image

 

So, you want to learn how to code in Java. That’s a great goal! In this book, you’ll learn the basics of Java while learning how to make a videogame! Coding can be tricky. Sometimes it’s frustrating. Remember to keep trying, test your code, and double-check that you’ve typed everything correctly. With dedication, you’ll learn a lot and have fun doing it!

In this chapter, you’ll install the Processing application, which will allow you to write Java code. Then you’ll get a chance to test out some real Java code and make sure you understand how the application will work throughout the rest of the book.

GETTING STARTED WITH PROCESSING

To get started with Java, you’ll be using a programming environment called Processing. Processing comes with a library of code that you can use to make objects move around the screen and create a fun gaming experience for your players! That makes writing your first videogame faster and easier.

DOWNLOADING AND INSTALLING PROCESSING

To get started with Processing, you need to download and install the application on your computer.

Follow these steps to download and install Processing:

  1. Go to www.processing.org (shown in Figure 1-1) and click the Download Processing button.
  2. Find the operating system that you’re using in the list of downloads (see Figure 1-2), and choose the correct version.

    If you aren’t sure which operating system you’re using, ask an adult.

    A file begins downloading to your computer.

  3. After the file downloads, unzip the file by double-clicking it.

    The Processing application should be created.

image

FIGURE 1-1: The Processing website.

image

FIGURE 1-2: Options for downloading Processing.

image

FIGURE 1-3: Optional contribution to the Processing application development.

After you have unzipped the Processing application, you can move it to your Desktop if you want by dragging the file from the Downloads folder to the Desktop folder.

STARTING TO USE PROCESSING

Now that you have Processing installed on your computer, you can start using it! Follow these steps:

  1. Double-click the Processing file that you extracted.
  2. The first time you open Processing, you might get an alert that asks if you’re sure you want to open the file (see Figure 1-4). Click Open.

    You see a welcome screen (see Figure 1-5).

  3. Uncheck the Show This Welcome Message Each Time box and then click the Get Started button.

    A new Sketch opens. A Sketch is what Processing calls a new program file. You’ll notice that the Sketch has Java as the programming language set in the upper right of the screen (see Figure 1-6).

image

FIGURE 1-4: A warning you might see when opening Processing the first time.

image

FIGURE 1-5: A welcome popup you see when opening Processing.

image

FIGURE 1-6: The Processing window with a new Sketch opened.

Now that you have Processing open, you can test out some code to make sure everything is working!

TRYING SOME CODE

In this book, you’re going to be learning how to write your own code from scratch, but when you first set up a programming environment, it’s a good idea to test some code that you know should work to make sure your programming environment is working. Follow these steps:

  1. Go to www.processing.org/examples/star.html (see Figure 1-7).
  2. Scroll down to the code that creates these stars, shown in Figure 1-8.
  3. Highlight all the code. Then right-click the code and choose Copy from the pop-up menu that appears (see Figure 1-9).

    You can also highlight the code and then press Command+C if you’re on a Mac or Ctrl+C if you’re on a Windows computer.

  4. Click the Processing application again, right-click in the Sketch window, and choose Paste from the pop-up menu that appears (see Figure 1-10).

    You can also click in the Sketch window and press Command+V if you’re on a Mac or Ctrl+V if you’re on a Windows computer.

  5. Click the Run button in the upper left of the Processing window (see Figure 1-11).
  6. Watch the three different stars spin (see Figure 1-12)!
image

FIGURE 1-7: The Star program example on the Processing website.

image

FIGURE 1-8: The Star program code that you’ll copy.

image

FIGURE 1-9: Copy the Star program code by highlighting and right-clicking.

image

FIGURE 1-10: Paste the Star program code by right-clicking in the Sketch.

image

FIGURE 1-11: Test the Star program by clicking the Run button.

image

FIGURE 1-12: The canvas with stars spinning.

Congratulations! You’ve tested your first Java program! Don’t worry if the code doesn’t make sense yet. Throughout this book, you’ll learn a lot of Java and have a chance to understand how everything works.