User Tools

Site Tools


cs498gd:java_and_game_programming

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

Small, old Java game example: jSpout

Newer Java game example: Flywrench4k


cs498gd/java_and_game_programming.txt · Last modified: 2016/09/16 18:19 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki