You are on page 1of 4

import javax.microedition.lcdui.

*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
/**
*
* @author p08292350
*/
public class RRCanvas extends GameCanvas implements Runnable {
private Random
rand;
private Display
display;
private boolean
sleeping;
private long
frameDelay;
private Image
infoBar;
private LayerManager layers;
private TiledLayer
backgroundLayer;
private Player
player = null;
private int
Width;
private int
Height;
private int
backWidth;
private int
velocity;
private int
xView,yView;
ImageFunction imgFun = new ImageFunction();
Image img = null;
Image img2 = null;
public RRCanvas(Display d) {
super(true);
display = d;
Width = getWidth();
Height = getHeight();
// Initialize the random number generator
rand = new Random();
// Set the frame rate (30 fps)
frameDelay = 33;
}
public void start() {
// Set the canvas as the current screen
display.setCurrent(this);
backWidth = (int)(getWidth()/6)+1;
try {
img = Image.createImage("/infoImage.png");
infoBar = imgFun.rescaleImage(img, Width, 20);
img = Image.createImage("/tileset.png");
backgroundLayer = new TiledLayer(12, 64, img, 32, 32);
player = new Player(Image.createImage("/bikeImage.png"),0,0.0f);
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
int[] layerMap = {
5, 6, 7, 8, 1, 2, 3, 4, 8, 7, 6, 5,
5, 6, 7, 8, 1, 2, 3, 4, 8, 7, 6, 5,
5, 6, 7, 8, 1, 2, 3, 4, 8, 7, 6, 5,
5, 6, 7, 8, 1, 2, 3, 4, 8, 7, 6, 5,

5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,

6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6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
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,
4,

8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,
8,

7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,
7,

6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,

5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,

};
for (int i = 0; i < layerMap.length; i++) {
int column = i % 12;
int row = (i - column) / 64;
backgroundLayer.setCell(column, row, layerMap[i]);
}
backgroundLayer.setPosition((getWidth() - backgroundLayer.getWidth()) / 2,
(getWidth() - backgroundLayer.getWidth()) / 2);
// Create the layer manager
layers = new LayerManager();
layers.append(player);
layers.append(backgroundLayer);
NewGame();
// Start the animation thread
sleeping = false;
Thread t = new Thread(this);
t.start();
}
public void stop() {
// 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) {}
}
}
public void update()
{
// Process user input to move the water layer and animate the player
int keyState = getKeyStates();
int xMove = 0;
if ((keyState & LEFT_PRESSED) != 0) {
xMove = -4;
}
else if ((keyState & RIGHT_PRESSED) != 0) {
xMove = 4;
}
if ((keyState & UP_PRESSED) != 0) {
velocity += 0.1;
}
//
else if ((keyState & DOWN_PRESSED) != 0) {
//
velocity = 1;
//
}

layers.setViewWindow(xView, player.getY()-player.getHeight(), getWidth(),


getHeight() - infoBar.getHeight());
// backgroundLayer.move(xView, -velocity);
player.move(xMove, -velocity);
//
//
//
//
//
//
//
//
//
ion
//
//
//

// Check for a collision with the player and the land tiled layer
if (playerSprite.collidesWith(landLayer, true)) {
// Restore the original view window and player sprite positions
layers.setViewWindow(xView, yView, getWidth(),
getHeight() - infoBar.getHeight());
playerSprite.move(-xMove, -yMove);
}
else {
// If there is no collision, commit the changes to the view window posit
xView += xMove;
yView += yMove;
}
velocity += 0.2;

}
private void draw(Graphics g) {
g.setColor(0,0,0);
g.fillRect(0, 0, Width, Height);
g.drawImage(infoBar, 0, 0, Graphics.TOP | Graphics.LEFT);
layers.paint(g, 0, infoBar.getHeight());
// Flush the offscreen graphics buffer
flushGraphics();
}
private void NewGame()
{
velocity = 2;
player.setVisible(true);
player.setPosition(getWidth()/2, player.getHeight());
xView = 0;
yView = player.getY() - ((getHeight() - player.getHeight()) / 2);
layers.setViewWindow(xView, yView, getWidth(), getHeight() - infoBar.getHe
ight());
}
}

You might also like