Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {
- Button arriba, abajo, derecha, izquierda;
- private Juego juego;
- private Bitmap personaje;
- private Sprite sprite;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- juego = (Juego) findViewById(R.id.customView);
- personaje = BitmapFactory.decodeResource(getResources(), R.drawable.bad1);
- sprite = new Sprite(juego, personaje);
- arriba = (Button)findViewById(R.id.arriba);
- abajo = (Button)findViewById(R.id.abajo);
- derecha = (Button)findViewById(R.id.derecha);
- izquierda = (Button)findViewById(R.id.izquierda);
- arriba.setOnClickListener(this);
- arriba.setOnLongClickListener(this);
- abajo.setOnClickListener(this);
- abajo.setOnLongClickListener(this);
- derecha.setOnClickListener(this);
- derecha.setOnLongClickListener(this);
- izquierda.setOnClickListener(this);
- izquierda.setOnLongClickListener(this);
- }
- @Override
- public void onClick(View v) {
- juego.parar();
- }
- @Override
- public boolean onLongClick(View v) {
- juego.mover();
- switch (v.getId()){
- case R.id.arriba:
- juego.arriba();
- break;
- case R.id.abajo:
- juego.abajo();
- break;
- case R.id.derecha:
- juego.derecha();
- break;
- case R.id.izquierda:
- juego.izquierda();
- break;
- }
- return false;
- }
- }
- public class Juego extends SurfaceView{
- private Bitmap personaje;
- private Sprite sprite;
- public Juego(Context context, AttributeSet attrs) {
- super(context, attrs);
- personaje = BitmapFactory.decodeResource(getResources(), R.drawable.bad1);
- sprite = new Sprite(this, personaje);
- }
- public void mover(){
- sprite.mover();
- invalidate();
- }
- public void parar(){
- sprite.parar();
- invalidate();
- }
- public void arriba(){
- sprite.dir= 3;
- invalidate();
- }
- public void abajo(){
- sprite.dir= 0;
- invalidate();
- }
- public void derecha(){
- sprite.dir= 2;
- invalidate();
- }
- public void izquierda(){
- sprite.dir= 1;
- invalidate();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- sprite.onDraw(canvas);
- invalidate();
- }
- }
- private static final int Horizontal = 4;
- private static final int Vertical = 3;
- private int x = 0;
- private int y = 0;
- private Juego juego;
- private Bitmap personaje;
- private int currentFrame = 0;
- private int ancho;
- private int alto;
- int dir = 2;
- private int velocidadx = 0;
- private int velocidady = 0;
- long fps;
- private long timeThisFrame;
- private long lastFrameChangeTime = 0;
- private int frameLengthInMilliseconds = 100;
- int cantFrames = 3;
- boolean playing = false;
- public Sprite(Juego juego, Bitmap personaje){
- this.juego = juego;
- this.personaje = personaje;
- this.ancho = personaje.getWidth() / Vertical;
- this.alto = personaje.getHeight() / Horizontal;
- }
- public void mover(){
- playing = true;
- }
- public void parar(){
- playing = false;
- }
- private void update() {
- if (playing) {
- long startFrame = System.currentTimeMillis();
- if (startFrame > lastFrameChangeTime + frameLengthInMilliseconds) {
- lastFrameChangeTime = startFrame;
- currentFrame++;
- if (currentFrame >= cantFrames) {
- currentFrame = 0;
- }
- }
- long startFrameTime = System.currentTimeMillis();
- timeThisFrame = System.currentTimeMillis() - startFrameTime;
- if (timeThisFrame >= 1) {
- fps = 1000 / timeThisFrame;
- }
- if (dir== 0 && y < juego.getHeight() - alto - velocidady) {
- velocidadx = 0;
- velocidady = 5;
- }
- if (dir== 1 && x > ancho){
- velocidadx = -5;
- velocidady = 0;
- }
- if (dir== 2 && x < juego.getWidth() - velocidadx){
- velocidadx = 5;
- velocidady = 0;
- }
- if (dir== 3 && y > alto){
- velocidadx = 0;
- velocidady = -5;
- }
- x = x + velocidadx;
- y = y + velocidady;
- }
- }
- public void onDraw(Canvas canvas) {
- update();
- int srcX = currentFrame * ancho;
- int srcY = alto * dir;
- Rect src = new Rect(srcX, srcY, srcX + ancho, srcY + alto);
- Rect dst = new Rect(x, y, x + ancho, y + alto);
- canvas.drawBitmap(personaje, src, dst, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement