Kilobolt
  • Home
  • Tutorials
    • Game Development Tutorial >
      • Unit 1: Beginning Java >
        • Before you begin...
        • Day 1: Setting Up
        • Day 2: Java Basics
        • Day 3: More Basics
        • Day 4: Java Math
        • Day 5: More Math
        • Day 6: If... else...
        • Day 7: More Control Flow
        • Day 8: Looping
        • Day 9: More on Looping
        • Day 10: Inheritance, Interface
        • Day 11: Threads and Graphics
      • Unit 2: Creating a Game I >
        • Day 1: Foundations
        • Day 2: Basic Framework
        • Day 3: Taking User Input
        • Day 4: Enter the Robot
        • Day 5: Background and Sprites
        • Day 6: Adding Enemies
        • Day 7: Shooting Bullets
        • Day 8: Animations
        • Day 9: 2D-Arrays
        • Day 10: Painting the Tilemap
      • Unit 3: Creating a Game II >
        • Day 1: Level Creation - Part 1
        • Day 2: Level Creation - Part 2
        • Day 3: Level Creation - Part 3
        • Collision Detection Basics
        • Day 4: Collision Detection Part 1
        • Day 5: Collision Detection Part 2
        • Day 6: Collision Detection Part 3
        • Day 7: Health System & Death
        • Day 8: Basic AI & Final Touches
      • Unit 4: Android Game Development >
        • Day 1: Introduction to Android
        • Day 2: Setting up for Development
        • Day 3: Creating our First Android Application
        • Day 4: Parts of an Android Application
        • Day 5: The Android Game Framework: Part I
        • Day 6: The Android Game Framework: Part II
        • Create an Android Game From Scratch (or port your existing game)
        • Day 7: Creating an Android Game (From Start to Finish)
      • Reference Sheet
    • Zombie Bird Tutorial (Flappy Bird Remake) >
      • Unit 1: Building the Game >
        • Introduction
        • Day 1: Flappy Bird - An In-depth Analysis
        • Day 2: Setting up libGDX
        • Day 3: Understanding the libGDX Framework
        • Day 4: GameWorld and GameRenderer and the Orthographic Camera
        • Day 5: The Flight of the Dead - Adding the Bird
        • Day 6: Adding Graphics - Welcome to the Necropolis
        • Day 7: The Grass, the Bird and the Skull Pipe
        • Day 8: Collision Detection and Sound Effects
        • Day 9: Finishing Gameplay and Basic UI
        • Day 10: GameStates and High Score
        • Day 11: Supporting iOS/Android + SplashScreen, Menus and Tweening
        • Day 12: Completed UI & Source Code
    • Android Application Development Tutorial >
      • Unit 1: Writing Basic Android Apps >
        • Before you begin...
        • Day 1: Android 101
        • Day 2: Getting to Know the Android Project
        • Day 3: The Development Machine
        • Day 4: Building a Music App - Part 1: Building Blocks
        • Day 5: Building a Music App - Part 2: Intents
        • Day 6: Building a Music App - Part 3: Activity Lifecycles
  • New Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Facebook
  • Twitter

GAME DEVELOPMENT TUTORIAL: DAY 2-1: Foundations

10/4/2012

74 Comments

 
Picture

This is where the fun begins.

All the hard work. The hours of dedication. The years of dreaming. It culminates here.
Welcome to Unit 2. By the time you walk away from this Unit, you will have made a fully playable game.

Before we get into the actual coding, let's talk about logistics.

1. You will be making a Java based applet. The purpose of this is to make it easy to embed into HTML, so you will be seeing live examples on this page.

2. If you have not followed Unit 1, we will be using Eclipse throughout this series. So I suggest that you go read the first few lessons in the first Unit to get yourself ready to develop.

In the past, many of you (nearly a hundred) participated in the survey that was available on this page, and I read each response. 

Thank you so much for your feedback.

Game Idea:
The most popular game idea was a platforming shooter. So that is the direction we will be headed.

Lesson #2-1: Setting up a Project

You're back for more! This Unit will be fun.

Open up Eclipse and create a Java project. You can do this by right clicking on the Package Explorer >> New >> Java Project:
Picture
Click to Enlarge
Call it KiloboltGame (consistency will ensure that we won't have errors due to naming). Also the JRE version can be either 1.6 or 1.7. It does not matter!
Picture
Inside this project's src folder (remember this is where we store all our code):

1. Create a Package. Name it kiloboltgame. By convention its the package name begins lowercase. 

Packages are ways of organizing classes into folder-like structures.
Right click on your newly created package and create a class: StartingClass.
Picture
KiloboltGame Project containing a kiloboltgame Package containing a StartingClass class.
Now we can start coding.

Lesson #2-2: Adding Methods and extending Applet


By this time, I trust that you are all experienced Java programmers capable of discerning which brace corresponds with which.

As such I will no longer be color coding unless I believe it is absolutely necessary.

1. Add extends Applet after "StartingClass". Recall that we can borrow methods from a superclass by inheriting it.

2. You will see a red line under Applet. Import Applet by pressing Ctrl+Shift+O or by manually selecting the import by mouseover.

3. You can now add the following methods: init(), start(), stop(), destroy(). These are part of the Applet superclass, and by importing Applet, you gain the ability to use them.

4. An easy way to add a pre-defined method (as in one that is already written for you), is to use the auto-complete function. Begin typing "init" and then press Ctrl + Space. It will open an auto-complete suggestions box, from which you can choose the first init() function. Proceed to do this with all four methods and you will see this:
Picture
5. Press Ctrl + Shift + F to auto-organize your code. 

Shortcuts such as these will help you save a lot of time.

Figure 2-1

 package kiloboltgame;

import java.applet.Applet;

public class StartingClass extends Applet {

@Override
public void init() {
// TODO Auto-generated method stub
super.init();
}

@Override
public void start() {
// TODO Auto-generated method stub
super.start();
}

@Override
public void stop() {
// TODO Auto-generated method stub
super.stop();
}

@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
}

Now, as always, we will talk about each of these methods.

The four methods: init(), start(), stop(), and destroy() are frameworks for execution provided by the Applet class. In this Applet's life cycle, major events will call one of these methods and execute it.

The @Override tests for errors upon compilation. In this case, we are using it to annotate that we are overriding methods from a parent class. It informs you when you make mistakes.

More information on this can be found on Google. But for now, don't worry about it. :)
Within each method, you will see a "super." This "super" is referring to the superclass (in this case Applet). You might also see a "this" in the future, which will refer to the current class. Don't worry about this for now. We can safely delete each "super" line (or leave it, if you choose):


Before we move on!
Mini Quiz:
1. How can you make a program keep running? How do you repeat statements?
2. What tool do we use for simultaneous processes in Java?
3. What is a game loop?

Answer these questions and check below!

Answers:
1. We utilize a loop.
2. A thread.
3. A game loop is the central component of a game. People refer to it as the heartbeat of the game. To put simply, it is a loop that will continuously check for changes in the game and make necessary updates. Graphics, controls, movement, physics all rely on the game loop in some way.

Here's a fake game loop that will help you understand:

Figure 2-2

 while (playerIsAlive) {

updatePosition();
applyGravity();
drawCharacterAtCurrentLocation();

}
Figure 2-3 incorporates three pseudo-methods (they are simply there for illustration).

We have a condition that while the playerIsAlive is true, we update his position, redraw his character at that new position, and apply gravity.

This is a simplified example of a game loop.

As we get into more advanced gaming concepts, we will talk about creating a proper game loop. But for now, a simple one will have to suffice (And on modern computers, a little bit of slacking here won't hurt us). But wait until we start developing on Android. Technical considerations can be PAINFUL. 

Day 1 will stop here. Day 2 will be up in an hour or two.

In Day 2, we will talk about applying threads and loops to create the heartbeat of our upcoming game.

Be excited.

Thank you for being an awesome audience, and please share our page on Facebook. 
Go to Day 2: Basic Framework
Picture
74 Comments

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge.


© 2014 Kilobolt, LLC. All rights reserved.