cs498gd:java_and_game_programming
Table of Contents
Java and Game Programming
We will use...
- Java Standard Edition (J2SE) Standard Development Kit (SDK) (or JDK).
- JDK version >= 6.
- Eclipse or other IDE such as IntellJ or NetBeans (or sometimes just a plain text editor and the command line).
Packages
- Java manages its namespace using packages.
- A package is a group of related classes.
- Some common packages that we may use:
- java.lang.* (DEFAULT)
- java.awt.*
- javax.swing.*
- java.io.*
- java.net.*
- import makes a package name available in a shorter form.
- Example: “import java.awt.*;” makes everything in class java.awt accessible as its short name.
- Afterward, for example, you can use the short name “Graphics” instead of “java.awt.Graphics”.
Methods
- Don't want to write an overly long main() method
- Methods are functions: they do a certain well-defined task and should be as small as possible
Information Protection (Methods and Variables)
- public - Can be accessed anywhere, including other classes, packages
- private - Can only be accessed from methods within the same class
- protected - Can be accessed in all methods of classes in the same package.
More Java Fundamentals
- Static methods are methods that belong to a class itself, rather than class instances (objects).
- You can define your own constants by using “static final”.
- Example: public static final int MAX_LEVELS = 10;
- But using a public Enum would be preferred if you have a group of related constants that you want to use in a game.
Random Numbers
- A part of almost every game
- Can use the Random class or Math.random()
- import java.util.Random;
- Creating and using a random number generator:
Random generator = new Random(); // To get a random integer where the range of your random number // is between 0 to N-1: int throw = generator.nextInt(N); // To start at 1: int throw = generator.nextInt(N - 1) + 1; // Setting the random seed (an integer used to set the starting point // for generating a series of random numbers): Random generator = new Random (SEED); // or use generator.setSeed(SEED); // In order to generate truly random numbers, the seed should be an // unpredictable value (e.g., entropy created by random key presses // or mouse movements).
Java and Game Programming
- Java in “real games”
- Tom Clancy's Politika, Star Wars Galaxies (the game logic), Puzzle Pirates, Galactic Village, Runescape, freeware/shareware games, MINECRAFT
- Java game engines or game development libraries exist (partial list at http://en.wikipedia.org/wiki/List_of_game_engines)
- Most significant may be LWJGL due to Minecraft
- We will introduce the Java-based, multi-platform game development framework, LibGDX, in this class as an alternative to using pure Java.
- Uses LWJGL and other libraries
- Recent Gamasutra blog entry on LibGDX and game development: http://www.gamasutra.com/blogs/YanickBourbeau/20150902/252624/Linux_game_development_in_2015.php
Small, old Java game example: jSpout
-
- Download and run with $ java -jar jspout.jar.
Newer Java game example: Flywrench4k
- 2013 winner of Java4K competition
-
- Windows: Run “Configure Java” to add security exception for http://www.x2d.org.
- Can't run in school Windows labs at all.
- Java applets are dead.
cs498gd/java_and_game_programming.txt · Last modified: 2016/09/16 18:19 by jchung