Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

package com.snc.

gamedevpart2;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class gamepart2 extends Activity {

GameView gameView;

@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);

gameView = new GameView(this);


setContentView(gameView);

class GameView extends SurfaceView implements


Runnable {

Thread gameThread = null;


SurfaceHolder myHolder;
volatile boolean playing;
Canvas canvas;
Paint paint;
Bitmap bmap;
boolean isMoving = false;
float walkspeedpersecond = 150;
float bmapxpos = 10;

public GameView(Context context) {


super(context);

myHolder = getHolder();

paint = new Paint();


bmap =
BitmapFactory.decodeResource(this.getResources(),
R.drawable.bob);

@Override
public void run() {
while (playing) {
update();
draw();
}

public void update() {

if (isMoving) {
bmapxpos = bmapxpos +
(walkspeedpersecond);
}

public void draw() {


if (myHolder.getSurface().isValid()) {

canvas = myHolder.lockCanvas();
canvas.drawColor(Color.argb(255, 26,
128, 182));
canvas.drawBitmap(bmap, bmapxpos,
200, paint);

myHolder.unlockCanvasAndPost(canvas);
}

public void pause() {

public void resume() {

playing = true;
gameThread = new Thread(this);
gameThread.start();

}
@Override
public boolean onTouchEvent(MotionEvent
motionEvent) {

switch (motionEvent.getAction() &


MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
isMoving = true;
break;
case MotionEvent.ACTION_UP:
isMoving = false;
break;
} return true;
}

@Override
protected void onResume(){
super.onResume();
gameView.resume();
}

You might also like