Date post: | 18-Dec-2015 |
Category: |
Documents |
Upload: | silvia-long |
View: | 213 times |
Download: | 0 times |
Molde University College INF 245 Fall 2007 OBø
1
INF245 Mobile applications Games Programming
using MIDP
H 2007
Ola Bø
Molde University College INF 245 Fall 2007 OBø
2
First some business information (source Gaute Godager, Funcom 2004)
The market for mobile games 175 000 000 active players Ordinary games (not mobile)
Development cost 10-250 MNOK Developed by teams with three groups of personnel
Programmers Artist/Gaphics Desinger
For games distributed through shops profits are divided Developer 10-20 % Distributor 30 % Publisher the rest 5 % of the titles makes 80% of the money (200 GNOK)
Molde University College INF 245 Fall 2007 OBø
3
Many kinds of games
Graphics- or text-based games One or more playyers On one unit or networked Build upon board games? Action games
Molde University College INF 245 Fall 2007 OBø
4
Levels of networking in mobile gaming (Powers 2006)
Single player with community features (global high-score, tournaments, chat)
Turn based games(chess, checkers,...)
On-line multiplayer
Molde University College INF 245 Fall 2007 OBø
5
Action games
Animation/Simulation partly controlled by user
One or more mobing objects ”Natural movements” Collisions
Molde University College INF 245 Fall 2007 OBø
6
Action games
Program must handle Input from user
Interpret keypresses to control movement Movement
Smooth motion of objects Handle collisions
Display Update screen Background and foreground graphics Cinema: 24 frames per second
We need at leas same frame rate to avoid jerky movements
Molde University College INF 245 Fall 2007 OBø
7
Action games in MIDP 1.0
Keyboard input results in keyPressed being called All objects in movement must get new positions for
every time screen is redrawn Object is removed from old position Object is drawn in new position
Paint must be called often enough Loop calling repaint Loop must run at least 24 timer per s
Code must be quick enough
Molde University College INF 245 Fall 2007 OBø
8
Aksjonsspill i MIDP 1.0
Input fra brukeren kommer i form av keyPressed() kall
Alle objekter i jevn bevegelse må få oppdatert posisjon for hver gang skjermen tegnes på nytt Objektet må fjernes fra gammel posisjon Objektet tegnes i ny posisjon
Vi må sørge for at Paint kalles tilstrekkelig ofte Loop som kaller rePaint() Loopen må kjøres minst 24 ganger per sekund
Koden må være rask nok
Molde University College INF 245 Fall 2007 OBø
9
public void GameBoard extends Canvas implements Runnable {
public void run() { while (true) { // Update positions // Handle collisions repaint(); // Wait until time for next update. } } public void paint(Graphics g) { // Draw what needs to be drawn } protected void keyPressed(int keyCode) { // Handle input from user. }}
Molde University College INF 245 Fall 2007 OBø
10
Time control Smooth movement demands continual update
Cinema: 24 frames per second 25 fps corresponds to a repaint every 1000ms/25fps=40
ms TICKTIME=40ms
Use System.currentTimeMillis() Register startTime and endTime for each recalculation. Sleep away the remaining time using Thread.sleep(40-
(endTime-startTime) to obtain constant time per frame.
Use rePaint() to order screen update for each frame
Molde University College INF 245 Fall 2007 OBø
11
public void GameBoard extends Canvas implements Runnable {
public void run() { while (true) {//Game loop startTime=System.currentTimeMillis(); // Update positions // Handle collisions endTime=System.currentTimeMillis(); // Wait until time for next update. Thread.sleep(TICKTIME-(endTime-startTime); repaint(); } } public void paint(Graphics g) { // Draw what needs to be drawn } protected void keyPressed(int keyCode) { // Handle input from user. }}
Molde University College INF 245 Fall 2007 OBø
12
Input from the user
keyCode parameter contains code for key pressed getGameAction interprets the key code I the exampe over, the input results in the racket being
moved to the left or to the right, (but not out of the board)
Molde University College INF 245 Fall 2007 OBø
13
Update positions Objects moving with constant speed
sx=vx*t+s0x sy=vy*t+s0y
Simplifications If vx (and vy) is movement per frame
time Then the next position can be
calculated like this sx=s0x+vx sy=s0y+vy
Speed and acceleration can be fractions Problem:
You may not have floating point arithmetics
Floating point may be too slow
sx is the x-coordinate of the objectsy is the y-coordinate of the objectvx is the speed of the object in the x-directions0x is previous x-coordinatet is time
If accelerated motion you shoud addvx=ax+v0xxy=ay+v0yax is acceleration in x-directionay is acceleration in y-direction
Molde University College INF 245 Fall 2007 OBø
14
Updating positions using fractions
But / % and * takes a long time and can result in jerky animations
CLDC 1.1 have floating point, but that is slow too
Using round number in the binary system and binary operations << to multiply>> to divide& to find the restcan speed things up
sxold=sx; int sx0=sxold*1000+sxr;//sx0 i thousandth included old rest int sxq=vx+sx0; //new x in thousandt sx=sxq/1000; //back to integers sxr=sxq%1000; //new rest syold=sy; int sy0=syold*1000+syr; int syq=vy+sy0; sy=syq/1000; syr=syq%1000;
Molde University College INF 245 Fall 2007 OBø
15
Quicker calculations using binary arithmetics
All calculations are fractions with 2^7=128 as denominator to improve precision when compared to integer numbers
uses the following ultra quick operations
<<7 to multiply by 128
>>7 to divide by 128
&0x3f to find rests>> << &-operations are quicker than * / og %. How much quicker? Can be determined experimentally.
Molde University College INF 245 Fall 2007 OBø
16
Collision handlingBall should not go through walls
Ball rebounds.
Ballen changes direction
left is position for left wallBALLR is ball radius
sx is x-coordinate for ball center
Add point
Accelerates to simulate gravitation
Molde University College INF 245 Fall 2007 OBø
17
Paint()
Mobing objects must first be deleted in old position before being redrawn Can be avoided by double-buffering and drawing
everything for each repaint. Do not redraw more than necessary
Only redraw what has changed since last redraw. If showNotify() has been called all should be redrawn Complex drawing operations can be replaced by
copying a picture of the drawing Find five improvements in code on next page
Molde University College INF 245 Fall 2007 OBø
19
MIDP 2.0javax.microedition.lcdui.game
Layers Layers over each other. z=0 is the bottom layer TileLayer with tiles
The tiles are fetched from a picture-file using a tilenr A sprite is a layer animating an object
The pictures of the animation are fetched from file and displayed in a programmable order
Layers and sprites are an old invention used on game consoles of the eighties. They can be used on mobile devices today even if mobile devices have better capacity
Transparency-handling
Molde University College INF 245 Fall 2007 OBø
21
Sprites
A partly transparent two-dimensional animation shown in a plan in a threedimensional scene (source: Wikipedia) May be rotated in the plane Always seen from the same side Can overlap and be overlapped by other objects
Stored as a picture series shown in quick succession
The application can control where in the picture series animation should start and which way to go
Molde University College INF 245 Fall 2007 OBø
22
MIDP 2.0
Polled keyboard makes one loop possible Better control over painting timing Sprites with collision detection
Molde University College INF 245 Fall 2007 OBø
24
javax.microedition.lcdui.game
Layer
paint()setVisible()
(from game)
LayerManager
append()getLayerAt()insert()paint()remove()
(from game)
-component[]
GameCanvas
GameCanvas()getGraphics()flushGraphics()getKeyStates()paint()
(from game)
Sprite
collidesWith()defineCollisionRectangle()defineReferencePixel()paint()setFrame()setFrameSequence()setImage()setRefPixelPosition()
(from game)
TiledLayer
createAnimatedTile()paint()setCell()setStaticTileSet()
(from game)
Canvas(from lcdui)
Molde University College INF 245 Fall 2007 OBø
25
Sound
Sound can be controlled using the multimedia API - JSR 135
Use of vibration? Use of camera
Molde University College INF 245 Fall 2007 OBø
26
Other possibilities and limitations 3D
3D API JSR 184
Speed problems Slow game and 3D apis
reported one some platforms Program size problem
May be alleviated using obfuscation
Networking problems Latency (Powers 2006)
seconds over WWAN Bandwidth
The usual suspects Screen Input devices Heterogeneity Mobile context of use
Molde University College INF 245 Fall 2007 OBø
27
Multiplayer demands communications
Possible solutions Bluetooth
+low cost +social -short range - limited number of participants - setup
WWAN + range + can support many participants - cost - latency time - unpredictable bandwdth - handling of users – may need lobby to meet
SMS + simplicity + availability - - cost - - latency time
Molde University College INF 245 Fall 2007 OBø
28
Alternatives to J2ME
“Native” gaming-applications Programmer must learn the platform Code might run faster Code might use more of the platform capabilities Separate version for each platform
Dedicated gaming platforms – Mophun
Molde University College INF 245 Fall 2007 OBø
29
Final Questions
What kinds of desktop games you know of might be fun played on a mobile platform?
Molde University College INF 245 Fall 2007 OBø
30
Sources Papers
Jonathan Knudsen: Creating 2D Action Games with the Game APIhttp://developers.sun.com/techtopics/mobility/midp/articles/game/
Bill Day: Wireless Game Development Now and Future http://developers.sun.com/events/techdays/presentations/brazil/WirelessGameDevelopment.pdf
Developers Training Material: Optimizing Your J2ME Applications for the Sony Ericsson T 610 Z600
Michael Powers(2006) Mobile Online Games articles series. http://developers.sun.com/mobility/midp/articles/gamepart1/http://developers.sun.com/mobility/midp/articles/gamepart2/http://developers.sun.com/mobility/midp/articles/gamepart3/
Book Martin J. Wells: J2ME Game Programming
Recommended if you are interested in programming action-games