Android Studio Space Shooter问题

时间:2018-04-23 07:09:06

标签: java android-studio

在此之前,我只想说我是新手。 我目前正试图在Android上为一个班级项目制作太空射击游戏,但我更关心的是学习更多东西。

我在屏幕上设置了布局,左,右和中间按钮(这个用于拍摄),以及在X坐标上根据箭头方向移动的太空船(位图)。但是我无法想出子弹的代码。我需要做到这一点,因此它获得了船的X位置,但它之后并没有跟随,因为左右不停(X ++和X--),我可以在成功中解雇。此外,我可以在随机的X位置产生敌人,但是由于Android工作室与团结不同,我不知道我应该如何销毁游戏对象,我想要发送它'屏幕上的位置,但这不是我的目标。如果有人能帮助我,我会很感激。

package com.example.malditosvirus2.spacegame;

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.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import java.util.Random;


public class GameLoop extends SurfaceView {
    private
    Bitmap bmp;
    private Bitmap bmp2;
    public int Ys;
    private SurfaceHolder holder;
    private Bitmap imp;
    private Bitmap red;
    private Bitmap lef;
    private Bitmap rig;

    private GameLoopThread gameLoopThread;

    // private SpriteAnimation sprite;
    //private SpriteAnimation sprite2;

    public int b;
    public int a;
    public int Xs;
    private int xPos;
    private int yPos;
    private GameLoop gameView;
    private Bitmap backGround1;
    private Bitmap backGround2;
    private Bitmap dea;
    private int bg1Y = 0;
    private int bg2Y = 0;
    private int speedScroll = 2;
    private int yLowerLimit;
    private int Ysize;
    private int Xsize;
    private int Xmove;
    private int Ymove;
    boolean right = false;
    boolean left = false;
    boolean shooting = false;
    private boolean start = false;
    private int y=-250;
    private int x;
    private int XB=Xmove;
    private int YB=Ysize;
    private Bitmap bul;





    public GameLoop(Context context) {
        super(context);
        gameLoopThread = new GameLoopThread(this);

        holder = getHolder();
        xPos = this.getWidth() / 2;


        //  for (int i = 0; i < tot; i++) {
        //  x[i]=rand.nextInt (1000)+ 100;
        //x[i] = rnd.nextInt(Xsize * 2 - dea.getWidth());
        //Xs = gameView.getWidth() /3;
        // Ys = gameView.getHeight();

        //}

        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

                boolean retry = true;

                gameLoopThread.setRunning(false);
                while (retry) {
                    try {
                        gameLoopThread.join();
                        retry = false;
                    } catch (InterruptedException e) {
                    }
                }
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {

                gameLoopThread.setRunning(true);
                gameLoopThread.start();

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder,
                                       int format, int width,
                                       int height) {

            }


        });


        backGround1 = BitmapFactory.decodeResource(getResources(), R.drawable.bg1);
        backGround2 = BitmapFactory.decodeResource(getResources(), R.drawable.bg1);

        bg1Y = 0;
        bg2Y = (backGround1.getHeight());

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);

        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        Xsize = width / 2;
        Xmove = Xsize;
        Ysize = height;
        yLowerLimit = height - 100;
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.xwing);
        rig = BitmapFactory.decodeResource(getResources(), R.drawable.right);
        lef = BitmapFactory.decodeResource(getResources(), R.drawable.left);
        red = BitmapFactory.decodeResource(getResources(), R.drawable.redball);
        dea = BitmapFactory.decodeResource(getResources(), R.drawable.deathstar);
        bul= BitmapFactory.decodeResource(getResources(), R.drawable.bullet);










        //bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.cow);
        //sprite = new Sprite(this, bmp);
        //sprite2 = new SpriteAnimation(this, bmp2);





    }



    //criar no modo override OnTouchScreen para cima baixo, utilizar codigo anterior


    @Override
    public void onDraw(Canvas canvas) {
        if (right == false && left == false) {
            Xmove = Xsize - bmp.getWidth() + 110;
        }
        if (right == true) {
            //for (Xmove = 0; Xmove < Xsize * 2 - bmp.getWidth(); Xmove += 10) ;
            Xmove += 20;
            if
                    (Xmove >= Xsize * 2 - bmp.getWidth()) {
                Xmove -= 20;
                Xmove = Xsize * 2 - bmp.getWidth();
            }
        }
        if (left == true) {
            //for (Xmove = 0 - bmp.; Xmove > 0; Xmove -= 10) ;
            Xmove -= 20;
            if (Xmove <= 0) {
                Xmove += 20;
            }
        }
        if (shooting == true) {
            YB -= 20;


            if (shooting == true && right == true) {
                XB--;
            }
            if (shooting == true && left == true) {
                XB++;

                if (start == true) {
                    y += 30;
                }
                canvas.drawColor(Color.BLACK);
                doDrawRunning(canvas);
                //sprite.onDraw(canvas, xPos, yPos);
                canvas.drawBitmap(red, Xsize - 150, Ysize - red.getHeight() - 10, null);
                canvas.drawBitmap(lef, red.getWidth() - 50, Ysize - rig.getHeight() - 10, null);
                canvas.drawBitmap(rig, Xsize * 2 - rig.getWidth() * 2 + 50, Ysize - rig.getHeight() - 10, null);
                canvas.drawBitmap(bul, XB, YB, null);
                canvas.drawBitmap(bmp, Xmove, Ysize / 2 + 500, null);
//
            }

            //a = xPos + 80;
            // b = yPos + 80;
            //sprite2.Draw(canvas, a, b);
            //
        }
    }
        private void doDrawRunning (Canvas canvas){
            Log.i("MovingBackGround", "doDrawRunning");
            bg1Y += speedScroll;
            bg2Y += speedScroll;

            Log.i("MobindBackGround", "bg1Y:" + bg1Y);
            Log.i("MobindBackGround", "bg2Y:" + bg2Y);

            if (bg1Y > yLowerLimit) {
                bg1Y = bg2Y - backGround1.getHeight();
                Log.i("MovingBackGround", "novo bg1Y:" + bg1Y);
            } else if (bg2Y > yLowerLimit) {
                bg2Y = bg1Y - backGround2.getHeight();
                Log.i("MovingBackGround", "novo bg2Y:" + bg2Y);


            }

            canvas.drawBitmap(backGround1, 0, bg1Y, null);
            canvas.drawBitmap(backGround2, 0, bg2Y, null);


        }
}

0 个答案:

没有答案