This site requires JavaScript, please enable it in your browser!
Greenfoot back
justina
justina wrote ...

2012/4/21

integrate a database in android game

justina justina

2012/4/21

#
i just finished building an android game " Kill Them All", now i wanna add a high scores database this is the code of the game:
package kill.them.all;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class GameView extends SurfaceView {
	private Bitmap bmp;
	private SurfaceHolder holder;
	private GameLoopThread gameLoopThread;
	private List<Sprite> sprites = new ArrayList<Sprite>();
	private long lastClick;
	private Bitmap bmpBlood;
         privare int score;
	private List<TempSprite> temps = new ArrayList<TempSprite>();

	public GameView(Context context) {
		super(context);
		gameLoopThread = new GameLoopThread(this);
		holder = getHolder();
		holder.addCallback(new Callback() {

			public void surfaceDestroyed(SurfaceHolder holder) {

			}

			public void surfaceCreated(SurfaceHolder holder) {
				createSprites();
				gameLoopThread.setRunning(true);
				gameLoopThread.start();

			}

			public void surfaceChanged(SurfaceHolder holder, int format,
					int width, int height) {
			}
		});
	
	bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.blood1);
	}
	private void createSprites() {
		sprites.add(createSprite(R.drawable.bad1));
	

	}

	private Sprite createSprite(int resouce) {
		Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
		return new Sprite(this, bmp);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		  for (int i = temps.size() - 1; i >= 0; i--) {
	            temps.get(i).onDraw(canvas);
	      }
		for (Sprite sprite : sprites) {
			sprite.onDraw(canvas);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (System.currentTimeMillis() - lastClick > 300) {
            lastClick = System.currentTimeMillis();
		  synchronized (getHolder()) {
			  float y=event.getY();
				float x=event.getX();
		for (int i = sprites.size() - 1; i >= 0; i--) {
			Sprite sprite = sprites.get(i);
			if (sprite.isCollition(x,y)) {
				sprites.remove(sprite);
	
				temps.add(new TempSprite(temps, this, x, y, bmpBlood));
                                  score++;
			    break;
			}
		}
		}
		}
		return true;
	}
}
and i found this database tutorial database but i don't know where and how to add it! -_-'
2patrickMurphy 2patrickMurphy

2012/4/21

#
databases are an expertise i reccomend going to websites like StackOverflow or DreamInCode.. For doing a hi-score database all you need to do is store information and retrieve it. Have like a top 10 and the highest number is stored on the first slot and so on and so forth while anything below the 10th slow gets deleted. As for how to do it i dont know look up more tutorlas of MySQL and SQL databases then see how they do a simple retreival database and move on to sqlite from there, i THINK its only syntax and knowing how things work that differs evrything else shoud be the same. Good Luck!
justina justina

2012/4/21

#
thanks dude,that was quick!! xD okay let's just say that i will forget about doing a databse how can i just display "game is over" and the score after 20 seconds from starting the game? :p
2patrickMurphy 2patrickMurphy

2012/4/21

#
This is a guess i havnt done a scoreboard yet so i wuldnt know. First you must create a GUI for the user to see, chose your windows and where you want to display and how. Then do the same steps i mentioned in the database the only differences are your storing them in variables such as Array's. The only difference between a database and a data structure, is a database is pernament. Same implementation for the most part (as in how you do it not syntax and everything else). You have to intialize your answer, retrieve it then store it and display it. Its pretty straight forward. I am too busy to really sit down and help you code this so go ahead and go to the websites i reccomended. They will be more then happy to help.
caoquocthi caoquocthi

2013/4/25

#
hello Justina I really like game Kill Them All but I have not finished this game yet , Can you share code for me , please?? my email: caoquocthi@zing.vn thank you very much :))
You need to login to post a reply.