Academic Tutorials



English | French | Portugese | German | Italian
Home Advertise Payments Recommended Websites Interview Questions FAQs
News Source Codes E-Books Downloads Jobs Web Hosting
Chats

J2ME
Introduction to J2ME
J2ME Development Kit
Understanding the Process of MIDlet
The MIDlet Lifecycle
User Interface Architecture
Alert
List
Text Box
Form
Images, Tickers and Gauges
Handling User Commands
Working with the Low-Level API
J2ME Gaming API
A Very Short Primer on Game Building
Building a J2ME Game
Defining Game Characteristics
TiledLayer
Sprites and LayerManager
Managing Layers
Sprites and Detecting Collisions
Mobile Media API
Using Mobile Media API (MMAPI)
Streaming media over the network

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
CSS 1.0
CSS 2.0
HLML
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
CVS
Python
Apple Script
PL/SQL Tutorial
SQL Server
PHP
.NET (dotnet)
Microsoft.Net
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
VC++
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Photoshop Tutorial
Gimp Tutorial
Matlab
Gnuplot Programming
GIF Animation Tutorial
Scientific Visualization Tutorial
Graphics
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Weblogic Tutorial
SEO
Web Site Hosting
Domain Name
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Eclipse
J2ME
JBOSS
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Cobol
Assembly Language
Mainframe
Forth Programming
Lisp Programming
Pascal
Delphi
Fortran
OOPs
Data Warehousing
CGI Programming
Emacs Tutorial
Gnome
ILU
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills
Database Tutorials
Oracle
MySQL
Operating System
BSD
Symbian
Unix
Internet
IP-Masquerading
IPC
MIDI
Software Testing
Testing
Firewalls
SAP Module
ERP
ABAP
Business Warehousing
SAP Basis
Material Management
Sales & Distribution
Human Resource
Netweaver
Customer Relationship Management
Production and Planning
Networking Programming
Corba Tutorial
Networking Tutorial
Microsoft Office
Microsoft Word
Microsoft Outlook
Microsoft PowerPoint
Microsoft Publisher
Microsoft Excel
Microsoft Front Page
Microsoft InfoPath
Microsoft Access
Accounting
Financial Accounting
Managerial Accounting
Network Sites


Defining Game Characteristics


Previoushome Next






Defining Game Characteristics


A D V E R T I S E M E N T

A game where all you have to do is to move the central character left and right is not very fun. Let's make some modifications to the game skeleton in Listing 1 to define this game a little better. To start with, specify a boundary for your game. It is essential to do this, because it helps to make your game consistently sized across different devices. To do this, start by defining some constants that are shown in the code here:

// the game boundary
public static final int GAME_WIDTH = 160;
public static final int GAME_HEIGHT = 160;

// the shifted x,y origin of the game
public final int GAME_ORIGIN_X = (getWidth() - GAME_WIDTH)/2;
public final int GAME_ORIGIN_Y = (getHeight() - GAME_HEIGHT)/2;

// the height of sections below and above the couple
public final int SECTION_HEIGHT = 64;

// the base on which the couple will move
public final int BASE = GAME_ORIGIN_Y + GAME_HEIGHT - SECTION_HEIGHT;

// the max height the couples can jump
public final int MAX_HEIGHT = 32;

(Note that I have introduced a game characteristic that indicates that this couple may soon be jumping on the screen, with the help of the MAX_HEIGHT constant.) On the screen, these constants help define the boundary of the game and its sole element (the couple), as shown in Figure 2.

Figure 2
Figure 2. Defining the game boundaries using the game constants

Of course, now you need to modify the rest of the code to use these constants. Add a new method to Listing 1 called buildGameScreen(Graphics g), as shown in code here:

private void buildGameScreen(Graphics g) {

  // set the drawing color to black
  g.setColor(0x000000);

  // draw the surrounding rectangle
  g.drawRect(GAME_ORIGIN_X, GAME_ORIGIN_Y, GAME_WIDTH, GAME_HEIGHT);

  // draw the base line
  g.drawLine(GAME_ORIGIN_X, BASE, GAME_ORIGIN_X + GAME_WIDTH, BASE);

  // draw the maximum line to where the couple can jump to
  g.drawLine(GAME_ORIGIN_X, BASE - MAX_HEIGHT,
    GAME_ORIGIN_X + GAME_WIDTH, BASE - MAX_HEIGHT);

}

Also add a call to this method in the updateGameScreen() method, before the couple image is drawn. The game boundaries have been defined and the only thing left to do is to make the starting position for the couple image as the BASE and not CENTER_Y. Change this in the start() method by setting coupleY = BASE;.

The couple image can move left and right with the left and right game keys, but now we ensure that it does not move past the game boundary. This was a problem in Listing 1, too, but in that case, the image simply vanished off the screen, as the boundary was the edge of the screen. It will look very odd if the image went past the boundaries now. Therefore, modify the left and right press actions in the calculateCoupleX() method to restrict movement beyond the boundaries. This modified method is listed here:

private void calculateCoupleX(int keyState) {

  // determines which way to move and changes the
  // x coordinate accordingly
  if((keyState & LEFT_PRESSED) != 0) {
    coupleX =
      Math.max(
        GAME_ORIGIN_X + coupleImg.getWidth()/2,
        coupleX - dx);
  }
  else if((keyState & RIGHT_PRESSED) != 0) {
    coupleX =
      Math.min(
        GAME_ORIGIN_X + GAME_WIDTH - coupleImg.getWidth()/2,
        coupleX + dx);;
  }

}

This method now uses Math.max() and Math.min() methods to restrict the couple image within the game boundaries. Notice that it also incorporates the width the of the image in these calculations.

I spoke earlier about making the couple image jump around on the screen. Let's see how this can be achieved by adding a method to move the image along the Y axis, independently of the user playing the game.

Add three new instance variables to Listing 1, called up, jumpHeight, and random, as shown here:

// a flag to indicate which direction the couple are moving
private Boolean up = true;

// indicates the random jump height, calculated for every jump
private int jumpHeight = MAX_HEIGHT;

// random number generator
public Random random = new Random();

As you can see, jumpHeight is initialized to MAX_HEIGHT. This jumpHeight variable will be calculated for each jump that the couple make and it will be set to a random value each time. This is shown in the calculateCoupleY() method shown here:

private void calculateCoupleY(int keyState) {

  // check if the couple were on the way up
  if(up) {

    // if yes, see if they have reached the current jump height
    if((coupleY > (BASE - jumpHeight + coupleImg.getHeight()))) {

      // if not, continue moving them up
	  coupleY -= dy;
	} else if(coupleY == (BASE - jumpHeight + coupleImg.getHeight())) {

	  // if yes, start moving them down
	  coupleY += dy;

	  // and change the flag
	  up = false;

	}

  } else {

    // the couple are on their way down, have they reached base?
    if(coupleY < BASE) {

      // no, so keep moving them down
	  coupleY += dy;

	} else if(coupleY == BASE) {

	  // have reached base, so calculate a new
	  // jump height which is not more than MAX_HEIGHT
	  int hyper = random.nextInt(MAX_HEIGHT + 1);

	  // but make sure that this it is atleast greater than the image height
	  if(hyper > coupleImg.getHeight()) jumpHeight = hyper;

	  // move the image up
	  coupleY -= dy;

	  // and reset the flag
	  up = true;

	}
  }
}

Note that since this method doesn't depend on the user pressing the up or down game keys, it has no use for the keyState information. But this value is passed to it nonetheless, in order to maintain conformity with the calculateCoupleX() method. This method starts moving the couple image by changing the coupleY variable in the upwards direction until it reaches the current jump height (which is the MAX_HEIGHT at starting). Once it reaches this jump height, it starts moving it in the opposite direction until it reaches BASE. At this point, a new jump height value, between the MAX_HEIGHT and couple image heights, is randomly calculated and the couple start jumping again.

The overall effect is of a randomly jumping couple who can be moved left and right by the user playing the game. A snapshot is shown in Figure 3.

Figure 3
Figure 3. Game snapshot



Be the first one to comment on this page.




  J2ME eBooks

No eBooks on J2ME could be found as of now.

 
 J2ME FAQs
More Links » »
 
 J2ME Interview Questions
More Links » »
 
 J2ME Articles
More Links » »
 
 J2ME News
More Links » »
 
 J2ME Jobs
More Links » »

Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previoushome Next

Keywords: Defining Game Characteristics, j2me introduction, J2ME Tutorial, j2me tutorial netbeans, j2me background process, j2me development kit, J2ME tutorial pdf, history of J2ME, basic J2ME, syntax use in J2ME, J2ME training courses, J2ME download.

HTML Quizzes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
CSS 1.0 Quiz
CSS 2.0 Quiz
HLML Quiz
XML Quizzes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizzes
JavaScript Quiz
VBScript Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizzes
ASP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
CVS Quiz
Python Quiz
Apple Script Quiz
PL/SQL Quiz
SQL Server Quiz
PHP Quiz
.NET (dotnet) Quizzes
Microsoft.Net Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
VC++ Quiz
Multimedia Quizzes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Photoshop Quiz
Gimp Quiz
Matlab Quiz
Gnuplot Programming Quiz
GIF Animation Quiz
Scientific Visualization Quiz
Graphics Quiz
Web Building Quizzes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Weblogic Quiz
SEO Quiz
Web Site Hosting Quiz
Domain Name Quiz
Java Quizzes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Eclipse Quiz
J2ME Quiz
JBOSS Quiz
Programming Langauges Quizzes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Cobol Quiz
Assembly Language Quiz
Mainframe Quiz
Forth Programming Quiz
Lisp Programming Quiz
Pascal Quiz
Delphi Quiz
Fortran Quiz
OOPs Quiz
Data Warehousing Quiz
CGI Programming Quiz
Emacs Quiz
Gnome Quiz
ILU Quiz
Soft Skills Quizzes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz
Database Quizzes
Oracle Quiz
MySQL Quiz
Operating System Quizzes
BSD Quiz
Symbian Quiz
Unix Quiz
Internet Quiz
IP-Masquerading Quiz
IPC Quiz
MIDI Quiz
Software Testing Quizzes
Testing Quiz
Firewalls Quiz
SAP Module Quizzes
ERP Quiz
ABAP Quiz
Business Warehousing Quiz
SAP Basis Quiz
Material Management Quiz
Sales & Distribution Quiz
Human Resource Quiz
Netweaver Quiz
Customer Relationship Management Quiz
Production and Planning Quiz
Networking Programming Quizzes
Corba Quiz
Networking Quiz
Microsoft Office Quizzes
Microsoft Word Quiz
Microsoft Outlook Quiz
Microsoft PowerPoint Quiz
Microsoft Publisher Quiz
Microsoft Excel Quiz
Microsoft Front Page Quiz
Microsoft InfoPath Quiz
Microsoft Access Quiz
Accounting Quizzes
Financial Accounting Quiz
Managerial Accounting Quiz
Testimonials | Contact Us | Link to Us | Site Map
Copyright ? 2008. Academic Tutorials.com. All rights reserved Privacy Policies | About Us
Our Portals : Academic Tutorials | Best eBooksworld | Beyond Stats | City Details | Interview Questions | Discussions World | Excellent Mobiles | Free Bangalore | Give Me The Code | Gog Logo | Indian Free Ads | Jobs Assist | New Interview Questions | One Stop FAQs | One Stop GATE | One Stop GRE | One Stop IAS | One Stop MBA | One Stop SAP | One Stop Testing | Webhosting in India | Dedicated Server in India | Sirf Dosti | Source Codes World | Tasty Food | Tech Archive | Testing Interview Questions | Tests World | The Galz | Top Masala | Vyom | Vyom eBooks | Vyom International | Vyom Links | Vyoms | Vyom World | Important Websites
Copyright ? 2003-2024 Vyom Technosoft Pvt. Ltd., All Rights Reserved.