You are on page 1of 9

GAME PROGRAMMING

A subset of game development.


A programming of computers for professional with a personal purpose
or simply for fun and enjoyment.
Different types of Games and Categorize:
o Puzzle jigsaw, rebus or pictogram, Rubiks cube.
o Traditional Card and Board Games.
o Adventure/ Role-Playing Games(RPG)
o Strategy warcraft, civilization, age of empires, might and
magic.
Category according to genre:
o Single or Multi-Player
o Real-time or Turn-Based
o First-Person or Third Person
o 2D or 3D
o Flying, Driving or Spaceflight simulations
o Sports Games
o Scrolling adventures games
Game Design Consideration
o Easy to Understand goal, challenges, rewards
o Easy to play yet challenging
o Challenging yet not impossible to win
o Short play times and interruptible
o Use the network but avoid latency issues
Game Advance Programming Interface (API)
o Help the developers for a better and faster User Interface with
better usability and still save devices resources like memory.
o Can be found in
Javax.microedition.cdui.game




Includes five classes
GameCanvas
Layer
LayerManager
Sprite
TiledLayer
Action Game Loop
An action game requires a processing loop that keeps things moving.









Adams Apple
o Objective of the game
Collects as many apples in the given time limit.
Avoid touching the snakes
Avoid being spit on by snakes
Game Loop Thread
Game loop needs to be independent of application==>Thread




Start
React to User Input
Move and Animated
Shapes
Draw Graphics
Detect and React to
Collisions
Game
Over?
End
YES
NO
Make class implement Runnable interface
Public class ClassName
Implements Runnable

Add thread initialization to constructor
Thread t = new Thread(This);
t.start();
Define run() method invoked by
Start()
public void run(){
While(running){
//do work here
}
}
Canvas run() Method
o Runnable interface requires implementation of the run()
method
o Game processing will be performed in this method
o Loop until game over
o Repaint at each pass of the loop
//in MyCanvas.java
//run method
Public void run(){
While (!gameOver){
// Process game here
repaint();
}}
Terminating Game Thread
o Need to stop game thread when game exits
//in MyCanvas.java
//CommandAction Method
public static void commandAction(Command c, Diplayable d){
if(c==cmdExit){
gameOver = true;
theMidlet.destroyApp(true);
theMidlet.NotifyDestroyed(true);
}
}
Cycle Rate

Rate by which game cycle loops(CPS)
Depends on the actual processes being performed at the
moment , fastest possible since only invoking repaint
Need to reduce so as not to hog CPU
o Set target CPS
o Let thread sleep if finished early
Cycle Rate Measurement
Use current time to count the number of cycles per second.






Game World
o When the game Character exits
o Graphically, the map
o Construction
Using Images
Using Primitives Graphic Methods
Using Tiles
o Size
Resizable
Fixed
Smaller Positioning
Larger than display Planning
Infinite Scrolling
Using Images
Can have very artistic maps
Fixed size
Using Primitive Graphic Methods
Applicable for simple designs
Resizable
A method may be defined for map creation =>
Reduces code clutter
Creating Map Image
More convenient to create the map on an image => will invoke
drawing () instead of redraw everything.
Using Tiles
o Map divided into grids
o Tiles sized equal to grids
o Map designed by laying out files
According to a predefined matrix
Random map design
o Tiles may also be designed to constrain in sprite movement


Tiles Sets
o For convenience, tiles are typically stored in a single image file,
called a tile set.
Extracting Tiles
o Individual tile extracting
Set clipping window to accommodate 1 tile
Adjust the display position to get desired title
Random
Pseudo random number generator
Contained in java.util.*;
Declare the random generator;
Private Random rand;
Instantiate and seed the generator
Rand = new Random(seed);
Generate random number and adjust
Math.abs(rand.nextInt()) % N
Sprites
In gaming, sprites is any graphic object that appearance on the
screen
Stationary or moving
Animated or not
Game story is affected by the interaction, typically by collision,
among the sprites
Game needs to keep track of each sprite
Sprites have common properties
Image
Location
Dimensions
Must be able to render the sprite on the canvas => pass
graphics
Context




Player and NPCs
Sprites may be categorized into
Player controlled by the player.
Non-Player Character friendly or non-friendly, controlled
by game logic
Better to have subclasses for each type
Image loaded in subclass
Movement defined according to subclass
Animation defined according to subclass
Adding More NPC
o Adding more NPCs of the same class requires
Declaring sprites variable
private Snake snake, snake.*;
instantiating sprite in canvas constructor
o snake2 = new Snake.getWidht()/2,200;
Adding sprites paint() In canvas paint();
Snakes.paint(g);
Sprite Manager
o More convenient if sprites can be managed collectively.
o Can create a class containing a vector or an array for the
sprites
Sprite Creation Using Sprite Manager
Declare sprite manager
Add NPCs constructor
Invoke sprite manager paint() In canvas paint()
Moving Sprites
Moving by modifying the location directly
Gives better control over the motion
Move using sprite movement methods
Encapsulates the object
More descriptive of the actions performance by the sprite
Enemy AI
A vital components in action games is the intelligence of the enemy
o Determines how enemy reacts to the player character
o Different levels of enemy intelligence can determine game level
Need to modify Enemy Sprite Class
o Add AI decision logic
o Add methods for different actions
Controlling Sprite Speed
o Add a cycle time property to the sprite time between
successivemoves.
o Define cycle() method
Remember time of last move
Invoke move() when lapse longer than cycle time
Create mutator to change the speed low number is faster
Panning
If game world is larger than the display, player character may be
constrained to stay in the middle of the screen.
Game world image is translated according to the position of the player
character on the screen.
Sprite Animation
o Sprite may be animated by changing the sprite image displayed
o Images typically stored in a single file, called a filmstrip.
o Each segments is called a frame; leftmost frame is index 0
o Sequencing frames produces different effects
o Use arrays to store the desired sequences or one array with
start and length of each sequence
Animation Set-Up
Need to know the number of frames contained in the
image and frame sequence
Need to remember the current frame to display



Sprite Creation at Game Time
o Sprite may also be created during playing of the game.
NPC sprites may be scheduled to appear at certain point
in the game
Player and NPC sprites may fire projectile/s
o A projectile may be given a lifespan by duration, distance, or
scope
o Firing rate may be controlled by number of projectiles,
weapon cool time.
Destroying Projectiles
o Projectiles may be destroyed by overriding the cycle() method of the
projectile
Out of bounds checked against map dimensions
Age checked against creation time
Collision Detection Algorithm
o Ideally, need to check if opaque pixels overlap
o The simplest, and generally the fastest, method is to treat all
sprites as rectangles
Bounding rectangle may be made smaller
Sprites collide if their bounding rectangles overlap
o May also define a central mass
o Define the center and radius of each sprite
o Sprite collide if distance between centers less than sum of
radii
Simple Collision Detection Code
o Typically, player character checks for collision against all enemy
sprites
o Can use multiple sprite managers for easy collision testing
Enemy Sprites and Projectiles
Friendly Sprites and Projectiles
End of Game
End of game may be determined by different factors depending on game
design
Number of Lives of player left
Player energy level
Time limit

You might also like