Home
Help
Search
Calendar
Login
Register
Please
login
or
register
.
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News:
New forum theme up and running!
Charas-Project
»
Off-Topic
»
Archive
»
Really Old Stuff
»
General programming
»
A mobile project
« previous
next »
Print
Pages: [
1
]
Author
Topic: A mobile project (Read 2407 times)
maxine
Average scripter
Zealot
Posts: 736
¨Dude i am 16 with 2 balls and hair
A mobile project
«
on:
November 28, 2006, 08:06:27 AM »
Hey all, im doing some mobile game and its going pretty well but the same time im pretty stuck. Here is the source code for my main form (Where everything happens):
package Hello;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
public class SCanvas extends GameCanvas implements Runnable {
private Display display;
private boolean sleeping;
private long frameDelay;
private int inputDelay;
private LayerManager layers;
private int xView, yView;
private TiledLayer backgroundLayer;
private TiledLayer barrierLayer;
private Sprite personSprite;
private Player musicPlayer;
private int[] walkLeft = {9, 10, 11};
private int[] walkRight = {3, 4, 5};
private int[] walkUp = {0, 1, 2};
private int[] walkDown = {6, 7, 8};
private int uindex = 0;
private int dindex = 0;
private int rindex = 0;
private int lindex = 0;
private int index = 0;
private boolean keyUp;
private boolean keyLeft;
private boolean keyDown;
private boolean keyRight;
private Image infoBar;
private int energy = 40;
public SCanvas(Display d) {
super(true);
display = d;
// Set the frame rate (30 fps)
frameDelay = 10;
// Clear the input delay
inputDelay = 0;
}
public void start() {
// Set the canvas as the current screen
display.setCurrent(this);
// Create the background and barrier tiled layers
try {
infoBar = Image.createImage("/InfoBar.png");
backgroundLayer = new TiledLayer(16, 16, Image.createImage("/Tileset.PNG"), 32, 32);
barrierLayer = new TiledLayer(16, 16, Image.createImage("/Tileset.PNG"), 32, 32);
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Setup the background tiled layer map
int[] backgroundMap = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
for (int i = 0; i < backgroundMap.length; i++) {
int column = i % 16;
int row = (i - column) / 16;
backgroundLayer.setCell(column, row, backgroundMap
);
}
// Setup the barrier tiled layer map
int[] barrierMap = {
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 0, 2, 0, 2, 2, 2, 0, 0, 2, 2, 2, 0, 0, 0, 2,
2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2,
2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2,
2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2,
2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2,
2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2,
2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2,
2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2,
2, 0, 2, 0, 2, 2, 2, 0, 0, 2, 2, 2, 0, 2, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
};
for (int i = 0; i < barrierMap.length; i++) {
int column = i % 16;
int row = (i - column) / 16;
barrierLayer.setCell(column, row, barrierMap
);
}
// Initialize the person sprite
try {
//infoBar = new Sprite(Image.createImage("/InfoBar.png"), )
personSprite = new Sprite(Image.createImage("/Person.png"), 24, 32);
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Create the layer manager
layers = new LayerManager();
layers.append(personSprite);
layers.append(barrierLayer);
layers.append(backgroundLayer);
xView = (backgroundLayer.getWidth() - getWidth()) / 2;
yView = (backgroundLayer.getHeight() - getHeight()) / 2;
layers.setViewWindow(xView, yView, getWidth(), getHeight());
personSprite.setPosition(xView + (getWidth() - personSprite.getWidth()) / 2,
yView + (getHeight() - personSprite.getHeight()) / 2);
// Initialize and start the music player
try {
InputStream is = getClass().getResourceAsStream("Music.mid");
musicPlayer = Manager.createPlayer(is, "audio/midi");
musicPlayer.prefetch();
musicPlayer.setLoopCount(-1);
// musicPlayer.start();
}
catch (IOException ioe) {
}
catch (MediaException me) {
}
// Start the animation thread
sleeping = false;
Thread t = new Thread(this);
t.start();
}
public void stop() {
// Close the music player
musicPlayer.close();
// Stop the animation
sleeping = true;
}
public void run() {
Graphics g = getGraphics();
// The main game loop
while (!sleeping) {
update();
draw(g);
try {
Thread.sleep(frameDelay);
}
catch (InterruptedException ie) {}
}
}
private void update() {
// Process user input to move the background layer and animate the person
if (++inputDelay > 2) {
int keyState = getKeyStates();
int xMove = 0, yMove = 0;
if ((keyState & LEFT_PRESSED) != 0){
if (keyLeft){
keyUp = false;
keyRight = false;
keyDown = false;
personSprite.setFrame(walkLeft[index++]);
xMove = -4;
}
}
else if ((keyState & RIGHT_PRESSED) != 0){
if (keyRight){
keyUp = false;
keyLeft = false;
keyDown = false;
personSprite.setFrame(walkRight[index++]);
xMove = 4;
}
}
if ((keyState & UP_PRESSED) != 0){
if (keyUp){
keyRight = false;
keyLeft = false;
keyDown = false;
personSprite.setFrame(walkUp[index++]);
yMove = -4;
}
}
else if ((keyState & DOWN_PRESSED) != 0){
if (keyDown){
keyUp = false;
keyLeft = false;
keyRight = false;
personSprite.setFrame(walkDown[index++]);
yMove = 4;
}
}
if (xMove != 0 || yMove != 0) {
layers.setViewWindow(xView + xMove, yView + yMove, getWidth(), getHeight());
personSprite.move(xMove, yMove);
personSprite.nextFrame();
}
keyUp = true;
keyDown = true;
keyRight = true;
keyLeft = true;
if(index >= 2)
index = 0;
// Check for a collision with the person and the barrier tiled layer
if (personSprite.collidesWith(barrierLayer, true)) {
// Play a collision sound
try {
// Manager.playTone(ToneControl.C4 + 12, 100, 100);
}
catch (Exception e) {
}
// Restore the original view window and person sprite positions
layers.setViewWindow(xView, yView, getWidth(), getHeight() - infoBar.getHeight());
personSprite.move(-xMove, -yMove);
}
else {
// If there is no collision, commit the changes to the view window position
xView += xMove;
yView += yMove;
inputDelay = 0;
}
}
}
private void draw(Graphics g) {
// Draw the layers
g.drawImage(infoBar, 0, 0, Graphics.TOP | Graphics.LEFT);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
g.drawString("Energy:", 2, 1, Graphics.TOP | Graphics.LEFT);
g.setColor(32, 32, 255); // blue
g.fillRect(40, 3, energy, 12);
layers.paint(g, 0, infoBar.getHeight());
// Flush the offscreen graphics buffer
flushGraphics();
}
}
Maybe later today i might post some screenshots. What this game does so far is:
*You have a character (Made in charasEX by now) and you can controll him in all directions.
*There are boundaries (Walls) which you ccant go through.
*You have a health that is controlled so you actually can lose/gain HP.
Stuff i might do:
*Make another layer (top layer) which will be the layer that the player will pass under.
*Find a descent tilemaker so the game can contain more advanced maps.
*Char maker, so the player wont be created THAT easily.
*Enemies which i will make different. Like enemies that follows the player, "enemies" that walks in a certain pattern.
*Map generating so that you can visit several maps in the game.
Mobile games can only be up to some Mb i think, so you cant put in HUGE ammounts of images. Well anybody interested to do charsets/Chipsets?
PS. Again, screenies will come later.
Logged
Print
Pages: [
1
]
« previous
next »
Charas-Project
»
Off-Topic
»
Archive
»
Really Old Stuff
»
General programming
»
A mobile project