Game.java
package com.android.skeleton;
import java.util.LinkedList;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceHolder;
public abstract class Game implements Runnable {
private LinkedList<Sprite> spriteList;
public LinkedList<Sprite> sprites(){
return spriteList;
}
private LinkedList<Rect>clearList;
private Thread gameThread;
private Bitmap backgroundImage=null;
Canvas canvas;
SurfaceHolder surfaceHolder = null;
Canvas backGraphics;
public abstract void gameStart();
public abstract void gameEnd();
public abstract void gameUpdate(Canvas c);
public abstract void spriteCollide(Sprite s1, Sprite s2);
public void init(){
spriteList = new LinkedList<Sprite>();
clearList = new LinkedList<Rect>();
}
public void setDebugOn(){
for(Sprite s: spriteList)
s.showRectangle();
}
public void setDebugOff(){
for(Sprite s: spriteList)
s.hideRectangle();
}
public void gameAddSprite(Sprite s){
spriteList.add(s);
}
public void gameAddSprites(Sprite []slist){
for(Sprite sp: slist)
spriteList.add(sp);
}
public void gameSetBackground(Bitmap i){
backgroundImage = i;
clearList.add(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()));
}
public void start(){
gameThread = new Thread(this);
gameThread.start();
gameStart();
}
public void stop(){
gameEnd();
gameThread = null;
}
public void update(Canvas c){
for(Sprite s: spriteList){
if(s.needsCleared()){
}
gameUpdate(backGraphics);
// Now that we know what needs patched up,
// draw it all...
paint(c);}
}
public void paint(Canvas c){
if(backgroundImage != null) {
for(Rect r: clearList){
backGraphics.drawBitmap(backgroundImage,
r.left, r.top, null);
}
} else {
for(@SuppressWarnings("unused") Rect r: clearList){
}
}
clearList.clear();
int n = spriteList.size();
for(int i=n-1; i>=0; i--){
spriteList.get(i).draw(backGraphics);
}
}
public void run() {
while(gameThread == Thread.currentThread()){
if(surfaceHolder.getSurface().isValid()){
Canvas canvas = surfaceHolder.lockCanvas();
gameCheckInputs();
checkCollisions();
paint(canvas);
try{
Thread.sleep(40);
}
catch(InterruptedException ex){
ex.printStackTrace();
}
}
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
private void checkCollisions(){
if(spriteList.size() > 1){
int nsprites = spriteList.size();
for(int i=0; i<nsprites; i++) if(spriteList.get(i).isActive()){
for(int j = i+1; j<nsprites; j++) if(spriteList.get(j).isActive()){
if(spriteList.get(i).getRect().intersect(spriteList.get(j).getRect())){
spriteCollide(spriteList.get(i), spriteList.get(j));
}
}
}
}
}
}
MyGame.java
package com.android.skeleton;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.provider.MediaStore.Audio;
public class MyGame extends Game{
private Bitmap background;
private Bitmap gameover;
public final int PLAYER = 0;
public final int ENEMY = 1;
public final int EXIT = 2;
public final int BIGENEMY = 3;
private int health;
//Sprites
Sprite player;
Enemy enemy1;
Enemy enemy2;
BigEnemy enemy3;
Enemy enemy4;
Enemy enemy5;
Enemy enemy6;
Exit exit;
//Images
Bitmap cat;
Bitmap jellyfish;
Bitmap bigdog;
Bitmap exitTile;
Audio music;
Audio damageSound;
Context context;
InputStream inputStream = null;
@Override
public void gameStart() {
// TODO Auto-generated method stub
//Load images
try{
AssetManager assetManager = context.getAssets();
inputStream = assetManager.open("/resources/blackcat.png");
cat = BitmapFactory.decodeStream(inputStream);
inputStream.close();
inputStream = assetManager.open("/resources/Jellyfish1.png");
jellyfish = BitmapFactory.decodeStream(inputStream);
inputStream.close();
inputStream = assetManager.open("/resources/Dog.png");
bigdog = BitmapFactory.decodeStream(inputStream);
inputStream.close();
inputStream = assetManager.open("/resources/exit.png");
exitTile = BitmapFactory.decodeStream(inputStream);
inputStream.close();
inputStream = assetManager.open("/resources/kitchen.png");
background = BitmapFactory.decodeStream(inputStream);
inputStream.close();
inputStream = assetManager.open("/resources/endscreen.png");
gameover = BitmapFactory.decodeStream(inputStream);
inputStream.close();
}catch(IOException ex){}
gameSetBackground(background);
//Load sounds / music
//Create sprites
player = new Sprite(cat, 0);
enemy1 = new Enemy(jellyfish, 0);
enemy2 = new Enemy(jellyfish, 0);
enemy4 = new Enemy(jellyfish, 0);
enemy5 = new Enemy(jellyfish, 0);
enemy6 = new Enemy(jellyfish, 0);
enemy3 = new BigEnemy(bigdog, 1.4);
exit = new Exit (exitTile, 0);
//Initialise sprite properties
player.setLocation(320, 480);
player.setType(0);
player.setActive();
health = 10;
enemy1.setLocation(0, 160);
enemy1.setVelocity(5, 0);
enemy1.setType(1);
enemy1.setActive();
enemy2.setLocation(640, 320);
enemy2.setVelocity(-5, 0);
enemy2.setType(1);
enemy2.setActive();
Random random = new Random();
Random random2 = new Random();
int i = Math.abs(random.nextInt() % 10);
int j = Math.abs(random2.nextInt() % 10);
while (i < 2){
i = Math.abs(random.nextInt() % 10);
}
while (j < 2){
j = Math.abs(random.nextInt() % 10);
}
enemy4.setLocation(640, 0);
enemy4.setVelocity(-i, j);
enemy4.setType(1);
enemy4.setActive();
Random random3 = new Random();
Random random4 = new Random();
int k = Math.abs(random3.nextInt() % 10);
int l = Math.abs(random4.nextInt() % 10);
while (k < 2){
k = Math.abs(random.nextInt() % 10);
}
while (l < 2){
l = Math.abs(random.nextInt() % 10);
}
enemy5.setLocation(0, 480);
enemy5.setVelocity(k, l);
enemy5.setType(1);
enemy5.setActive();
enemy6.setLocation(600, 440);
enemy6.setVelocity(0, -5);
enemy6.setType(1);
enemy6.setActive();
exit.setLocation(600, 0);
exit.setVelocity(0, 0);
exit.setType(2);
exit.setActive();
//Add sprites to game
gameAddSprite(player);
gameAddSprite(enemy1);
gameAddSprite(enemy2);
gameAddSprite(enemy3);
gameAddSprite(enemy4);
gameAddSprite(enemy5);
gameAddSprite(enemy6);
gameAddSprite(exit);
//Play music track
}
@Override
public void gameEnd() {
// TODO Auto-generated method stub
player.setInactive();
enemy1.setInactive();
enemy2.setInactive();
enemy3.setInactive();
enemy4.setInactive();
enemy5.setInactive();
enemy6.setInactive();
exit.setInactive();
gameSetBackground(gameover);
}
@Override
public void gameUpdate(Canvas c) {
// TODO Auto-generated method stub
player.update();
enemy1.rotate(0.1);
enemy1.update();
enemy2.rotate(0.1);
enemy2.update();
enemy3.update();
enemy4.rotate(0.1);
enemy4.update();
enemy5.rotate(0.1);
enemy5.update();
enemy6.rotate(0.2);
enemy6.update();
if(enemy3.getPosy() == 430){
enemy3.setInactive();
}
if(health == 0){
gameEnd();
}
}
public void draw(Canvas c){
}
@Override
public void spriteCollide(Sprite s1, Sprite s2) {
// TODO Auto-generated method stub
if(s2.getType() == ENEMY && s1.getType() == PLAYER)
{
health -= 1;
}
if(s2.getType() == BIGENEMY && s1.getType() == PLAYER)
{
health -= 1;
}
if (s2.getType() == EXIT && s1.getType() == PLAYER)
{
gameEnd();
}
if(s2.getType() == ENEMY && s1.getType() == ENEMY)
{
double i = s2.getVelx();
double j = s2.getVely();
double k = s1.getVelx();
double l = s1.getVely();
s2.setVelocity(-i, -j);
s1.setVelocity(-k,-l);
}
}
public void actionPerformed() {
// TODO Auto-generated method stub
//Create random spawn positions for bigdogs
Random random = new Random();
Random random2 = new Random();
int xPos = Math.abs(random.nextInt() % 640);
int yVel = Math.abs(random2.nextInt() % 20);
//Make sure y velocity is between 10 and 30. Set this way for now
//to hide problems with individual enemy3 sprites becoming inactive.
while (yVel < 10)
yVel = Math.abs(random.nextInt() % 30);
//Spawn big dogs with random x position and y velocity
//according to timer settings.
enemy3.setLocation(xPos, 0);
enemy3.setActive();
enemy3.setVelocity(0, -yVel);
enemy3.setType(3);
}
@Override
public void gameCheckInputs() {
// TODO Auto-generated method stub
}
}
package com.android.skeleton;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
public class ScreenImage {
// The sprite image...
private Bitmap image;
Context context;
InputStream inputStream = null;
public ScreenImage(String fileName){
try{
AssetManager assetManager = context.getAssets();
inputStream = assetManager.open(fileName);
image = BitmapFactory.decodeStream(inputStream);
inputStream.close();
}catch(IOException ex){}
}
public ScreenImage(Bitmap image){
this.image = image;
}
public Bitmap getImage() {
return image;
}
public int getWidth(){
return image.getWidth();
}
public int getHeight(){
return image.getHeight();
}
public void drawOn(Canvas c, int x, int y){
c.drawBitmap(image, x, y, null);
}
public void drawOn(Canvas c, int x, int y, double angleInRads){
}
public void drawSegmentOn(Canvas c, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2){
}
}
Sprite.java
package com.android.skeleton;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
public class Sprite {
private Bitmap bmp;
private ScreenImage image; // Bitmap used
private double posx, posy; // Where the sprite is drawn
private int w, h; // Size of bitmap in pixels
private double velx, vely; // Motion vector
private Rect rect; // Enclosing rectangle
private boolean updated;
private double rotation; // Angle it is drawn at (radians)
private double orientation; // Correction for image angle (radians)
private boolean lockAttitudeToMotion;
private boolean showEnclosingRectangle=false;
private boolean active=false;
private boolean clearRect=false; // Clear the area this sprite occupied.
private int type=0;
public Sprite(){
posx=posy=0.0;
velx=vely=0.0;
rect = new Rect(0, 0, 0, 0);
lockAttitudeToMotion = true;
}
public Sprite(Bitmap img, double orient){
posx=posy=0;
velx=vely=0;
orientation = orient;
image=new ScreenImage(img);
w = image.getWidth();
h = image.getHeight();
rect = new Rect(0, 0, w, h);
lockAttitudeToMotion = true;
}
public void setType(int type){
this.type = type;
}
public int getType(){
return type;
}
public boolean isUpdated(){
return updated;
}
public void setAttitudeLock(){
lockAttitudeToMotion = true;
}
public void clearAttitudeLock(){
lockAttitudeToMotion = false;
}
public void showRectangle(){
showEnclosingRectangle = true;
}
public void hideRectangle(){
showEnclosingRectangle = false;
}
public void setActive(){
active = true;
}
public void setInactive(){
clearRect = true;
active = false;
}
public boolean isActive(){
return active;
}
public void setToClear(){
clearRect = true;
}
public boolean needsCleared(){
boolean clr = clearRect;
clearRect = false;
return clr;
}
public double getPosx() {
return posx;
}
public void setPosx(double posx) {
this.posx = posx;
updated = true;
}
public double getPosy() {
return posy;
}
public void setPosy(double posy) {
this.posy = posy;
updated = true;
}
public void setLocation(double x, double y){
setPosx(x);
setPosy(y);
}
public void move(double dx, double dy){
setPosx(getPosx()+dx);
setPosy(getPosy()+dy);
}
public int centreX(){
return (int)posx+w/2;
}
public int centreY(){
return (int)posy+h/2;
}
protected ScreenImage getImage(){
return image;
}
private void calcRect(){
int hw = (int)(w*Math.abs(Math.cos(rotation+orientation))+h*Math.abs(Math.sin(rotation+orientation)))/2;
int hh = (int)(h*Math.abs(Math.cos(rotation+orientation))+w*Math.abs(Math.sin(rotation+orientation)))/2;
rect.left = centreX() - hw;
rect.bottom = centreY() - hh;
}
public Rect getRect(){
calcRect();
return rect;
}
/**
* How fast and in what direction is it moving?
* @return - vector components for x & y
*/
public double getVelx() {
return velx;
}
public void setVelx(double velx) {
this.velx = velx;
if(lockAttitudeToMotion)
setRotation(getDirection());
}
public double getVely() {
return vely;
}
public void setVely(double vely) {
this.vely = vely;
if(lockAttitudeToMotion)
setRotation(getDirection());
}
public void setVelocity(double velx, double vely){
setVelx(velx);
setVely(vely);
}
public double getSpeed(){
return Math.hypot(velx, vely);
}
public void setSpeed(double speed){
// Current direction...
double angle = Math.atan2(vely, velx);
velx = speed * Math.cos(angle);
vely = speed * Math.sin(angle);
}
public double getDirection(){
return -Math.atan2(vely, velx);
}
public void setDirection(double angle){
double speed = Math.hypot(velx, vely);
velx = speed * Math.cos(angle);
vely = -speed * Math.sin(angle);
if(lockAttitudeToMotion)
setRotation(angle);
}
public void turn(double angleInRads){
double dir = getDirection();
dir += angleInRads;
setDirection(dir);
if(lockAttitudeToMotion)
setRotation(dir);
}
public double getRotation() {
return rotation;
}
public void setRotation(double rotation) {
this.rotation = rotation;
updated = true;
}
public void rotate(double angleInRads){
rotation += angleInRads;
if(rotation<0) rotation += Math.PI*2;
if(rotation>=Math.PI*2)rotation -= Math.PI*2;
updated = true;
}
public void draw(Canvas canvas){
if(!active) return;
canvas.drawBitmap(bmp, (float)posx, (float)posy, null);
setToClear();
if(showEnclosingRectangle)
drawRectangle(canvas);
}
private void drawRectangle(Canvas canvas){
getRect();
canvas.drawRect(rect.left+3, rect.bottom+3, rect.width()-6, rect.height()-6, null);
}
public void update(){
if(!active) return;
setPosx(getPosx()+velx);
setPosy(getPosy()+vely);
getRect();
if(posx<0){
setPosx(0);
setVelx(-velx);
}
if(posy<0){
setPosy(0);
setVely(-vely);
}
}
public String toString(){
String s = String.format("Sprite: (%d,%d) @ (%5.2f,%5.2f) Velocity: (%5.2f, %5.2f) Rotation: %5.2f Direction: %5.2f", w, h, posx, posy, velx, vely, Math.toDegrees(rotation+orientation), Math.toDegrees(getDirection()));
return s;
}
}
I've probably made quite a lot of mistakes in this as I'm not very familiar with Android. Any help would be massively appreciated.

New Topic/Question
Reply


MultiQuote





|