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

70 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
70 Comments
tony
10/4/2012 06:11:02 pm

KiloGun?

Reply
friedfish
10/4/2012 11:30:14 pm

Thank you for this tutorial. I have been looking for a good tutorial for android development and this one is great. If I ever sell some of my games on Android I will definitely donate some here. :)

Check out this game titles:

- Advanced Android Annihilation
- Samurai Carnage
- Dynamite Shootout
- Chocolate Crocodile
- Fluffy Princess
- Chase Munchers
- Hyper Sushi Man
- Prince Kilo vs Super Bolt

Reply
WDQw
12/2/2013 07:52:37 am

dsssmwjlsw KQ/LDAMSkl

Reply
Scott link
10/5/2012 04:10:19 am

Thanks for writing this tutorial! I've always been interested in developing games, but never really know how to go about it, and I hope by following your tutorial I can.

It would be cool if you could post all of this stuff on Github, Unit has its own public repo, and each day has its own subfolder.

Reply
doseela
10/5/2012 08:09:01 am

KiloWar

Reply
John Paul
10/5/2012 08:33:14 am

Hello everyone!!!
I am super exited to the next tutorial, I love free education, I am going to create a website for free education and when I do I will definetly talk about you. Thank you so much.
The best name that I can think of is: "Cortilo"
"Poltic"
or
"Folitri"

Reply
Roshan Karki link
10/6/2012 06:57:42 pm

Thanks, can't wait for more.

Reply
s!d
10/7/2012 03:24:28 am

Just cant wait for complete tutorials to be out.......i Guess u mae a FAN!

Reply
Vishal
10/7/2012 03:37:34 am

Thanks a lot for this. I am getting very excited and learning whole this in bits is actually making me understand more than what I learned from other books and tutorials.

Reply
RRC
10/17/2012 01:05:20 pm

Are you saying that your TUMBL game was written in Applet? Or you are giving us an example of writing an Applet game and then teach us about how to create a game for Android? I am really NOT that interested in learning Applet, but if it is a way to create a game for Android, I can continue reading it.

BTW, I really appreciate your efforts and willingness to help others. Great work!

Reply
James C.
10/17/2012 01:28:35 pm

TUMBL was originally written as an Applet (as a proof of concept) and then ported to Android.

The curriculum for this tutorial series is as follows (simplified):
Unit 1: Basic Java
Unit 2: Creating a Java Game
Unit 3: Porting this game to Android
Unit 4: Advanced Game Dev.

Since this tutorial was created for beginners, we assume that you have not created a game before. Since it is a huge leap to go from not knowing how to make a game using Basic Java to making a full-fledged Android application that incorporates SurfaceViews and XMLs while worrying about resource management, activity life cycles and touch/accelerometer input, we are covering Applets first (not because we are going to be making Applets in the future, but because it is a way to apply basic game development concepts).

Most of the concepts that we discuss here will scale up perfectly -- in fact, you can even copy and paste some of the code when we begin Unit 3.

The other alternative is to utilize Swing, but I just chose the path that I've taken when I developed TUMBL.

I hope that clears things up and thank you for following the guide!

Reply
Amit
11/3/2012 05:17:57 am

Thank you so much for these tutorials James. I have learned java for 2 years in university but only for boring stuff like recursive algorithms and efficient search and stuff like that. I always wanted to learn about android game development and these tutorials are absolutely perfect as i have been learning something new every tutorial. Amazing job man, keep up the good work. Will be donating soon :)

Reply
Deniz
11/7/2012 11:34:10 pm

THANK YOU
THANK YOU
THANK YOU

Reply
Reece
11/17/2012 03:29:34 pm

Awesome stuff. I'm really looking forward to doing this unit.

Thank you!

Reply
Alihan Yılmaz
11/19/2012 04:25:09 pm

Thank You !! I really learn a alot from you. Cant wait to see new lessons.

Reply
Mackshun
11/21/2012 11:12:41 am

Great stuff. This is very helpful. Thank you!!! Excited to the transition into Android Development! :P

Reply
Roberto Rios link
12/2/2012 12:32:30 pm

megagipon!

Reply
The Sinister V
12/4/2012 05:38:38 pm

You could be making some money off these tutorials yet here you are sharing the knowledge for free.
I seriously cannot thank you enough.
Thank you.
Thank you.
Thank you.

Reply
Driff
12/5/2012 02:47:31 pm

excellent tutorial man im loving them if i end making the game i want ill totally give you the credits you deserve+donations if i sell it on the playstore :D
and the game name related to you is
Revenge of Kilobolt

Reply
Zach
12/12/2012 03:56:14 am

Great job!

FWIW, I more used to having an "auto" autocomplete, especially being out of the language for a while and forgetting so much. I followed a stackoverflow tip to go in to Preferences -> Java -> Editor -> Content Assist and add a-z and A-Z to the activation triggers. I also set the activation delay to 50 ms. The autocomplete options now pop up as I type.

Reply
syed huzaifa
12/12/2012 07:20:04 pm

Awesome tutorial,great job and thoroughly explained.

Reply
palas
12/19/2012 02:51:06 pm

Thanks for this nice tutorial,
I am new, and little bit confused in placing all class and code together. Then i download your code and your final source code showing error when i import.
plz provide compiling source code.

Reply
James C.
12/19/2012 03:09:33 pm

I copied and pasted the "final code" and it works perfectly for me.

Check that you have named your .java file exactly as the class declaration says: StartingClass.java.

Reply
James C.
12/19/2012 03:07:30 pm

Could you email me a screenshot of the error?

support@kilobolt.com

Thank you!

Reply
Connor link
1/1/2013 02:07:33 pm

Thanks for taking the time to make this tutorial :) i have been looking for one and i found this very high quality tutorial for free :) thanks.

Reply
SomeDudesOnTheInternet
1/7/2013 03:29:47 pm

On my version control + space does not work, but i found out Alt + / works. Might help somebody.

Reply
Aemon
1/15/2013 04:36:43 am

Man u rock!!! Thanks to ppl like u this world goes around. My only complaint is that TUMBL+ is soooo addictive ;P

I m a student but I ll try to support u as much as I can. Thx again a million times :D

Reply
Griff
1/21/2013 01:31:35 pm

The best! You actually teach stuff, and explain well! You actually care about your readers! I find these tutorials 100x better than YouTube videos!

Reply
rohanpro(xda developer)
2/3/2013 07:50:16 pm

its a great learning site....keep up the good work

Reply
Alex
2/5/2013 02:10:36 am

Go ahead lads ! Thanks for your work.

Reply
Newbie
2/9/2013 11:34:54 pm

This is seriously a Godsend:D

Reply
Reid
2/14/2013 05:39:47 am

You're a cool guy man. Thanks for putting up all this stuff. I've already learned so much and I'm a noob. :)

Reply
maham
2/15/2013 07:32:39 pm

hangman

Reply
Aqeel
3/27/2013 05:01:16 am

Please add moneybookers for donations.

Reply
Abel
3/31/2013 02:36:44 pm

Runmintz

Reply
MR NL
4/4/2013 04:28:35 am

Thanks for this wonderful tutoriual!

- Bolts from the blue
- Kane Kilobolt

Reply
ssk
4/8/2013 09:31:27 pm

James Bolt

Reply
nelzkie
4/27/2013 04:59:22 pm

Tutorial is great. however can u make the code more like the one in stakoverflow? To make it more readable?

Reply
kishore
6/13/2013 10:26:32 pm

Thank you Bolt... Your tutorial is my foundation step for some great game development.

Reply
venkatesh
6/16/2013 11:58:51 pm

Good work ...i enjoyed it lets move on.....

Reply
Dharmik
6/18/2013 04:30:13 pm

Sir my previous questione's solution has found. I want a suggestion from you that wheather I use netbeans because graphic creation Is simple in that software.

Reply
Baker
6/20/2013 06:39:37 am

Just in case anyone doesn't feel like typing all of the inherited methods

right click where you would like to insert code > Source > Override/Implement Methods... then you can choose which methods you would like to Override instead of typing them out.

Reply
Zeak
7/3/2013 12:56:06 am

Thanks for the heart work you've put into these guides. I've started from the beginning and hope to develop games in my spare time. I appreciate everything you've done. Thank you!

Reply
sid link
7/27/2013 10:03:40 pm

for(i=0;i<=-1;i++)
{
System.out.println("Thank you for great tutorials");
}

Reply
Arslan
8/2/2013 11:14:07 am

I love the shortcuts of Eclipse you told that I don't know already :)

Reply
Yaniv
8/4/2013 04:26:12 am

Kilo Invader

Reply
saydie
8/13/2013 05:31:04 am

thanks a lot for this tutorial.. this helps me a lot.. this tutorial is way better that school :)

Reply
Ghavinj
8/23/2013 07:41:11 am

Thanks for these tuts.. what about Trench like the combat trench in war.

Reply
Carlos Navas
9/4/2013 05:51:47 am

This is really really well explained. Thank you!!

Reply
GOPI
9/23/2013 09:59:24 pm

very very useful.

Reply
Andrew
9/29/2013 03:49:51 pm

At Step 1 I came across a problem that might be a version of eclipse issue. There is no folder under the src and no way of adding a package. What am I missing?

Reply
yogesh patel
1/31/2014 09:43:17 pm

You Should Use This-
eclipse-SDK-4.2.2-win32

Reply
michael roni
9/29/2013 10:41:41 pm

shooting dino

Reply
Zack
10/3/2013 01:55:43 pm

Thank you very much for these tutorials, they are really helpful. I am not able donate money, but all I can do is share your site with my friends and I can help you make some money off Google Adsense ads that you have put on this site (Obviously by clicking the ads once a while :P)
.
Btw, thanks again :)

Reply
devlop
10/12/2013 12:17:50 am

great work,thanks

Reply
Arpan Kumar Nandi
12/24/2013 04:33:22 pm

Awesome@@2 simply awesome...

Reply
nivya
1/14/2014 02:50:18 pm

horse knock,bingo,behead names

Reply
yogesh
1/31/2014 09:44:27 pm

Thanks.....

Reply
Navjot Singh
2/2/2014 04:22:12 am

Very Very Helpful! I took a Java class but barley learned game programming. this tutorial is VERY nice, and answers all my questions along the way. Also, i like the mini quizzes!

Reply
vishal dey
6/30/2014 04:45:01 am

whats the difference between init() and start() methods

Reply
alessio
8/2/2014 05:45:56 pm

I use android studio but when I build a project it can't find "java.applet"

Reply
Fusion4991
8/12/2014 06:31:17 am

Hey Kilobolt!

Just finished Unit 1 of your game development tutorials! Flying into Unit 2 as we speak. You say that we need to use JRE 1.6 or 1.7.
Would it hurt if I use 1.8? Because that is the latest I have. I recon it will not because it will contain everything from 1.6 /1.7 but I just want to ask to be sure.

Thanks in advance

Reply
Rohit
4/17/2015 02:36:53 pm

Can I do the same stuff in Android Studio ? Please reply

Reply
Fan
5/15/2015 01:32:38 pm

The best tuto i ever see, thank you

Reply
Arjun
1/9/2016 11:31:30 am

THANK YOU

Reply
indra
4/4/2016 01:04:50 am

What a good tutorial,step by step,it is useful for a beginner like me
I've been looking for this kind of tutorial for long time before,finally found this..thx very much..

Reply
leo
7/29/2016 11:08:14 pm

hi james, i'm working your game tutorial in android studio,
i've got an error when run java app it's "Exception in thread "main" java.lang.ClassNotFoundException".
I think my errors in main method StartingClass, where is it?

Reply
Kumar link
8/26/2016 09:48:41 am

I read these tutorials few month back and then went on to experiment with game development tools (Unity, Constuct2, etc). I will say that this site provides one the best foundation material for anyone going for game development.

Thanks for creating such a nice tutorial.

Reply
Florent
11/2/2016 12:36:49 pm

Hi, I've been following your tutorial, I've been to the keyEvent part, and I wanted to test it, but the problem is : why don't you have a main class ? If you have one where is it ?

Reply
crytek
5/8/2018 08:48:05 am

hey a complete java noob here. i want to make a java game and this site looks great. but i dont know how to run this code, it gives an error saying there isnt any main class. how do i run this game?

Reply



Leave a Reply.

    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.