Sign in to follow this  
robertosaget

Android pong game torubles

Recommended Posts

robertosaget    106
After spending a lot of time trying to make a calculator app. I succeeded and wanted to move on to making pong. Pong had a lot of interesting challenges. I decided that i would take the lunar lander code from google and modify it and subtract from it to build a pong game. I subtracted a lot of the menu processing and used all of their variables and such in case of them being highly important. I have a background(background), a ball(mbackgroundimage), 2 paddles, and movement. The ball moves and bounces fine but i want to display the score to a text view that is built into the layout. The problem is updating the textview whenever someone scores. So far i dont know how to do that and I have tried a lot. I have tried using a handler but so far with no success. Here is my code(Well the abridged version because their are a lot of comments and broken areas).

[code]
public class basicgame extends Activity {

/** Called when the activity is first created. */


/** A handle to the thread that's actually running the animation. */
private LunarThread mLunarThread;

/** A handle to the View in which the game is running. */
private gameview mLunarView;



public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


// score.setTextSize(20);

mLunarView = (gameview) findViewById(R.id.lunar);
mLunarThread = mLunarView.getThread();
mLunarView.setTextView((TextView) findViewById(R.id.scoreview));
mLunarView.postInvalidate();

}


}


[/code]




[code]



public class gameview extends SurfaceView implements SurfaceHolder.Callback
{

public float player1y =0;
public float player2y =0;
public String playerscore = "";
int player1score =0;
int player2score = 0;

public class LunarThread extends Thread {

public int mycounter = 0;
int x =0;
int y = 0;
int velx = 5;
int vely = 5;



DisplayMetrics display = new DisplayMetrics();



Drawable background;
private Drawable mBackgroundImage;


private Drawable player1;
private Drawable player2;





/** Message handler used by thread to interact with TextView */
private Handler mHandler;







/** Indicate whether the surface has been created & is ready to draw */
private boolean mRun = false;

/** Scratch rect object. */


/** Handle to the surface manager object we interact with */
private SurfaceHolder mSurfaceHolder;


public LunarThread(SurfaceHolder surfaceHolder, Context context,
Handler handler) {

// get handles to some important objects
mSurfaceHolder = surfaceHolder;
mHandler = handler;
mContext = context;

Resources res = context.getResources();
// cache handles to our key sprites & other drawables


// load background image as a Bitmap instead of a Drawable b/c
// we don't need to transform it and it's faster to draw this way
mBackgroundImage = getResources().getDrawable(R.drawable.icon);
background = getResources().getDrawable(R.drawable.kittens);
player1 = getResources().getDrawable(R.drawable.paddle);
player2 = getResources().getDrawable(R.drawable.paddle);

}




@Override
public void run() {
while (mRun) {

Canvas c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {

doDraw(c);

}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}

}
}


public void setRunning(boolean b) {
mRun = b;
}


public void setSurfaceSize(int width, int height) {
// synchronized to make sure these all change atomically
synchronized (mSurfaceHolder) {



}
}


boolean doKeyDown(int keyCode, KeyEvent msg) {
synchronized (mSurfaceHolder) {




return false;
}
}


private void doDraw(Canvas canvas) {

canvas.save();


canvas.drawColor(Color.BLACK);
background.setBounds(0,0,canvas.getWidth(),canvas.getHeight());
mBackgroundImage.setBounds(x, y,x+ 100,y+ 100);

player1.setBounds(0, (int)player1y, 50, (int)player1y+75);
player2.setBounds(canvas.getWidth()-50,(int)player2y,canvas.getWidth(),(int)player2y+75);
// Log.i("game","X "+x + " Y "+y );
background.draw(canvas);
mBackgroundImage.draw(canvas);
player1.draw(canvas);
player2.draw(canvas);
if(player1y<0)
{
player1y =0;
}
if(player1y>canvas.getHeight()-75)
{
player1y = canvas.getHeight()-75;
}
if(player2y<0)
{
player2y =0;
}
if(player2y>canvas.getHeight()-75)
{
player2y = canvas.getHeight()-75;
}
int sizex = canvas.getWidth()-100;
int sizey = canvas.getHeight()-100;

x+=velx;
y+=vely;


if(x<0)
{
//x=0;
//velx*=-1;
x = canvas.getWidth()/2;
player2score++;

}
if(y<0)
{
y=0;
vely*=-1;
}
if(x>sizex)
{
//x=sizex;
//velx*=-1;
x = canvas.getWidth()/2;
player1score++;
}
if(y>sizey)
{
y=sizey;
vely*=-1;
}
Rect paddlerect1 = player1.getBounds();
Rect paddlerect2 = player2.getBounds();
Rect ball = mBackgroundImage.getBounds();

if(ball.intersects(ball,paddlerect1))
{
x =50;
velx*=-1;


}
if(ball.intersects(ball,paddlerect2))
{
x = canvas.getWidth()-150;;
velx*=-1;

}

canvas.restore();
//
// invalidate();

}

public LunarThread getThread() {
return thread;
}

private float mPosX;
private float mPosY;

private float mLastTouchX;
private float mLastTouchY;

private static final int INVALID_POINTER_ID = -1;

private int mActivePointerId = INVALID_POINTER_ID;


public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
thread.setSurfaceSize(width, height);
}

/*
* Callback invoked when the Surface has been created and is ready to be
* used.
*/
public void surfaceCreated(SurfaceHolder holder) {
// start the thread here so that we don't busy-wait in run()
// waiting for the surface to be created
thread.setRunning(true);
thread.start();
}



public void surfaceDestroyed(SurfaceHolder holder) {
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
}

/** Handle to the application context, used to e.g. fetch Drawables. */
private static Context mContext;

/** Pointer to the text view to display "Paused.." etc. */
private TextView mStatusText;
@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();

switch (action) {

case MotionEvent.ACTION_MOVE: {
// Find the index of the active pointer and fetch its position
int pointerCount = ev.getPointerCount();


for (int i = 0; i < pointerCount; i++) {
float p1y = ev.getY(i);
float p1x = ev.getX(i);
if(p1x<300)
{
player1y = p1y;
}
else if(p1x>300)
{
player2y = p1y;
}
}
//player1y = p1y;

invalidate();
break;
}

}
return true;


}
public LunarThread getThread() {
return thread;
}
/** The thread that actually draws the animation */
public LunarThread thread;
public void setTextView(TextView textView) {
mStatusText = textView;
}

public gameview(Context context, AttributeSet attrs)
{
super(context, attrs);

// register our interest in hearing about changes to our surface
SurfaceHolder holder = getHolder();
holder.addCallback(this);

// create thread only; it's started in surfaceCreated()
thread = new LunarThread(holder, context, new Handler() {
@Override
public void handleMessage(Message msg) {

playerscore = "Player 1: "+ player1score +" Player 2: " + player2score;
mStatusText.setText(msg.getData().getString(playerscore));



}
});






// setFocusable(true); // make sure we get key events
this.setFocusableInTouchMode(true);
}
public void surfaceDestroyed(SurfaceHolder holder) {
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
// start the thread here so that we don't busy-wait in run()
// waiting for the surface to be created
thread.setRunning(true);
thread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
thread.setSurfaceSize(width, height);
}




}



[/code]

What can i do to post in the score textview and update it with playerscore?

Thanks

EDIT: A friend told me the reason the frame rate takes a nose dive is the way i am drawing everything constantly. How can i perfect this and use gl or a new way of drawing?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this