代码之家  ›  专栏  ›  技术社区  ›  Niklas Rosencrantz

改善太空外星人的行动

  •  7
  • Niklas Rosencrantz  · 技术社区  · 6 年前

    enter image description here

    我编写了一个由太空入侵者和 Moon Patrol 。水平射击外星人是可能的(见上图)。

    也可以垂直射击外星人(见下文)。

    enter image description here

    但是添加外星人并不“缩放”,因此很难添加15个外星人移动,例如所有可能的碰撞。最初的太空入侵者和月球巡洋舰解决了这个问题,有没有可能制定一个不同于我正在使用的战略?外星人的确切移动并不重要,只不过它是“有趣的”。

    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.graphics.Rect;
    import android.support.v4.view.MotionEventCompat;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ParallaxView extends SurfaceView implements Runnable {
    
        List<Background> backgrounds;
    
        private volatile boolean running;
        private Thread gameThread = null;
    
        // For drawing
        private Paint paint;
        private Canvas canvas;
        private SurfaceHolder ourHolder;
    
        // Holds a reference to the Activity
        Context context;
    
        // Control the fps
        long fps = 60;
    
        // Screen resolution
        int screenWidth;
        int screenHeight;
    
        private void update() {
            // Update all the background positions
            for (Background bg : backgrounds) {
                bg.update(fps);
            }
    
        }
    
        ParallaxView(Context context, int screenWidth, int screenHeight) {
            super(context);
    
            this.context = context;
    
            this.screenWidth = screenWidth;
            this.screenHeight = screenHeight;
    
            // Initialize our drawing objects
            ourHolder = getHolder();
            paint = new Paint();
    
            // Initialize our array list
            backgrounds = new ArrayList<>();
    
            //load the background data into the Background objects and
            // place them in our GameObject arraylist
    
            backgrounds.add(new Background(
                    this.context,
                    screenWidth,
                    screenHeight,
                    "bg", 0, 120, 50));
    
            backgrounds.add(new Background(
                    this.context,
                    screenWidth,
                    screenHeight,
                    "grass", 70, 110, 200));
    
            // Add more backgrounds here
    
        }
    
        @Override
        public void run() {
    
            while (running) {
                long startFrameTime = System.currentTimeMillis();
    
                update();
                if (j > 2000) {
                    j = -50;
                    k = 0;
                }
                if (o > 2000) {
                    o = -50;
                    l = 0;
                }
                draw();
    
                // Calculate the fps this frame
                long timeThisFrame = System.currentTimeMillis() - startFrameTime;
                if (timeThisFrame >= 1) {
                    fps = 1000 / timeThisFrame;
                }
            }
        }
    
        int numberOfshots = 1;
        int[] i = new int[200];
        int j = 0;
        int k = 0;
        int l = 0;
        int m = 0;
        int o = 0;
        boolean down = true;
        long lastTurn = System.currentTimeMillis();
        int xbuggy = 0;
        int xbuggy2 = 0;
        boolean down2 = true;
        long lastTurn2 = System.currentTimeMillis();
        long lastTurn3 = System.currentTimeMillis();
        boolean jump = false;
        boolean shoot = false;
        int ind = 0;
    
        private void draw() {
    
            if (ourHolder.getSurface().isValid()) {
                //First we lock the area of memory we will be drawing to
                canvas = ourHolder.lockCanvas();
                if (jump) {
                    xbuggy = xbuggy + 4;
                }
                if (shoot) {
                    xbuggy2 = xbuggy2 + 4;
                }
    
                if (System.currentTimeMillis() - lastTurn3 >= 1000) {
                    // Change direction here
                    jump = false;
                    lastTurn3 = System.currentTimeMillis();
                    xbuggy = 0;
                }
                //draw a background color
                canvas.drawColor(Color.argb(255, 0, 0, 0));
    
                // Draw the background parallax
                drawBackground(0);
    
                // Draw the rest of the game
                paint.setTextSize(60);
                paint.setColor(Color.argb(255, 255, 255, 255));
    
                //canvas.drawText("MOONPATROL3000", 350, screenHeight / 100 * 5, paint);
    
                int resID = context.getResources().getIdentifier("vehicle",
                        "drawable", context.getPackageName());
    
                int alienResID = context.getResources().getIdentifier("object3_hdpi",
                        "drawable", context.getPackageName());
    
                int alienResID2 = context.getResources().getIdentifier("object2_hdpi",
                        "drawable", context.getPackageName());
    
                int alienResID3 = context.getResources().getIdentifier("object1_hdpi",
                        "drawable", context.getPackageName());
    
                // Load the bitmap using the id
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
                Bitmap alienbitmap = BitmapFactory.decodeResource(context.getResources(), alienResID);
                Bitmap alienbitmap2 = BitmapFactory.decodeResource(context.getResources(), alienResID2);
                Bitmap alienbitmap3 = BitmapFactory.decodeResource(context.getResources(), alienResID3);
    
    
    
    
                //paint.setTextSize(220);
                for (int i1 = 0; i1 < numberOfshots; i1++) {
    
    
                    // if horizontal missile hits alien 0
                    if (java.lang.Math.abs(j - i[i1]) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(k +150+ screenHeight / 100 * 45 - (float) (screenHeight * 0.61)) * 2  < (alienbitmap.getHeight() + 60)) {
                        //y1[i2] = -random.nextInt(1000); // reset to new vertical position
                        //score += 1;
                        //onScoreListener.onScore(score);
                        Log.d("missile", "missile hit! ");
                        j=-200;
                    }
    
                    // if vertical missile hits alien 0
                    if (java.lang.Math.abs(j - 185) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(j + 150 + screenHeight / 100 * 45 - (screenHeight / 100 * 95 - i[i1] - xbuggy2)) * 2  < (alienbitmap.getHeight() + 60)) {
                        //y1[i2] = -random.nextInt(1000); // reset to new vertical position
                        //score += 1;
                        //onScoreListener.onScore(score);
                        Log.d("missile", "missile hit! ");
                        j=-200;
                    }
    
    
                    // if horizontal missile hits alien 1, right now this won't happen
                    if (java.lang.Math.abs(j - i[i1]) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(k +150+ screenHeight / 100 * 45 - (float) (screenHeight * 0.61)) * 2  < (alienbitmap.getHeight() + 60)) {
                        //y1[i2] = -random.nextInt(1000); // reset to new vertical position
                        //score += 1;
                        //onScoreListener.onScore(score);
                        Log.d("missile", "missile hit! ");
                        j=-200;
                    }
    
                    // if vertical missile hits alien 1
                    if (java.lang.Math.abs(o + 10 - 185) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(l + screenHeight / 100 * 25 - (screenHeight / 100 * 95 - i[i1] - xbuggy2)) * 2  < (alienbitmap.getHeight() + 60)) {
                        //y1[i2] = -random.nextInt(1000); // reset to new vertical position
                        //score += 1;
                        //onScoreListener.onScore(score);
                        Log.d("missile", "missile hit! ");
                        o=-200;
                    }
    
    
                    canvas.drawText("o", i[i1], (float) (screenHeight * 0.61), paint);
                    canvas.drawText("o", 185, screenHeight / 100 * 95 - i[i1] - xbuggy2, paint);
    
    
    
    
                    if (i1 == numberOfshots - 1 && i[i1] > screenWidth) {
                        if (numberOfshots > 0) numberOfshots--;
                        if (ind > 0) ind--;
                    }
                }
                if (System.currentTimeMillis() - lastTurn >= 2000) {
                    // Change direction here
                    down = !down;
                    lastTurn = System.currentTimeMillis();
                }
    
                if (System.currentTimeMillis() - lastTurn2 >= 7000) {
                    // Change direction here
                    down2 = !down2;
                    lastTurn2 = System.currentTimeMillis();
                }
    
                canvas.drawBitmap(alienbitmap, j, k +150+ screenHeight / 100 * 45, paint);
                canvas.drawBitmap(alienbitmap2, o + 10, l + screenHeight / 100 * 25, paint);
                //canvas.drawBitmap(alienbitmap3, j+20, k+screenHeight / 100 * 5, paint);
                drawBackground(1);
                canvas.drawBitmap(bitmap, 50, (float) (screenHeight * 0.5) - xbuggy, paint);
                // Draw the foreground parallax
    
                for (int n = 0; n < numberOfshots; n++)
                    i[n] = i[n] + 20;
    
                j = j + 10;
                o = o + 7;
                if (!down)
                    k=k+2;
                else
                    k=k-2;
    
                if (!down2)
                    l++;
                else
                    l--;
    
                // Unlock and draw the scene
                ourHolder.unlockCanvasAndPost(canvas);
            }
        }
    
        // Clean up our thread if the game is stopped
        public void pause() {
            running = false;
            try {
                gameThread.join();
            } catch (InterruptedException e) {
                // Error
            }
        }
    
        // Make a new thread and start it
        // Execution moves to our run method
        public void resume() {
            running = true;
            gameThread = new Thread(this);
            gameThread.start();
        }
    
        private void drawBackground(int position) {
    
            // Make a copy of the relevant background
            Background bg = backgrounds.get(position);
    
            // define what portion of images to capture and
            // what coordinates of screen to draw them at
    
            // For the regular bitmap
            Rect fromRect1 = new Rect(0, 0, bg.width - bg.xClip, bg.height);
            Rect toRect1 = new Rect(bg.xClip, bg.startY, bg.width, bg.endY);
    
            // For the reversed background
            Rect fromRect2 = new Rect(bg.width - bg.xClip, 0, bg.width, bg.height);
            Rect toRect2 = new Rect(0, bg.startY, bg.xClip, bg.endY);
    
            //draw the two background bitmaps
            if (!bg.reversedFirst) {
                canvas.drawBitmap(bg.bitmap, fromRect1, toRect1, paint);
                canvas.drawBitmap(bg.bitmapReversed, fromRect2, toRect2, paint);
            } else {
                canvas.drawBitmap(bg.bitmap, fromRect2, toRect2, paint);
                canvas.drawBitmap(bg.bitmapReversed, fromRect1, toRect1, paint);
            }
        }
    
        // Because we call this from onTouchEvent, this code will be executed for both
        // normal touch events and for when the system calls this using Accessibility
        @Override
        public boolean performClick() {
            super.performClick();
            launchMissile();
            return true;
        }
    
        private void launchMissile() {
            i[ind] = 350;
            ind++;
            xbuggy2 = 0;
            shoot = true;
        }
    
        // event listener for when the user touches the screen
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            boolean gameOver = false;
            //if (paused) {
            //   paused = false;
            //}
            int action = MotionEventCompat.getActionMasked(event);
            int coordX = (int) event.getX();
            int coordY = (int) event.getY();
            Log.d("coordY", "coordY " + coordY);
            if (coordX < 220 && xbuggy == 0 && action == MotionEvent.ACTION_MOVE) {
                jump = true;
                shoot = false;
                lastTurn3 = System.currentTimeMillis();
                return true; // do nothing
            }
    
            if (coordX > 219 && action == MotionEvent.ACTION_DOWN) {
                numberOfshots++;
                performClick();
                return true;
            }
            return true;
        }
    }
    

    更新

    我已经开始根据下面的内容来概括外星人的逻辑。

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    
    public class Alien {
        public Alien(){}
        public Alien(Context context, String name) {
            setAlienResID(context.getResources().getIdentifier("object3_hdpi",
                    "drawable", context.getPackageName()));
            setAlienbitmap(BitmapFactory.decodeResource(context.getResources(), this.getAlienResID()));
        }
        public int getAlienResID() {
            return alienResID;
        }
    
        public void setAlienResID(int alienResID) {
            this.alienResID = alienResID;
        }
    
        public Bitmap getAlienbitmap() {
            return alienbitmap;
        }
    
        public void setAlienbitmap(Bitmap alienbitmap) {
            this.alienbitmap = alienbitmap;
        }
    
        public int getWidth() {
            return width;
        }
    
        public void setWidth(int width) {
            this.width = width;
        }
    
        public int getHeight() {
            return height;
        }
    
        public void setHeight(int height) {
            this.height = height;
        }
    
        int alienResID;
        Bitmap alienbitmap;
        int width;
        int height;
    }
    
    
    public class AttackingAlien extends Alien {
        public AttackingAlien(Context context, String name) {
            super(context, name);
        }
    }
    

    更新2

    我改变了策略。现在我正在画一艘宇宙飞船,它将轰炸月球车。

    enter image description here

    相关代码是

    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.graphics.Rect;
    import android.support.v4.view.MotionEventCompat;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ParallaxView extends SurfaceView implements Runnable {
    
        List<Background> backgrounds;
    
        private volatile boolean running;
        private Thread gameThread = null;
    
        // For drawing
        private Paint paint;
        private Canvas canvas;
        private SurfaceHolder ourHolder;
    
        // Holds a reference to the Activity
        Context context;
    
        // Control the fps
        long fps = 60;
    
        // Screen resolution
        int screenWidth;
        int screenHeight;
    
        private void update() {
            // Update all the background positions
            for (Background bg : backgrounds) {
                bg.update(fps);
            }
    
        }
    
        ParallaxView(Context context, int screenWidth, int screenHeight) {
            super(context);
    
            this.context = context;
    
            this.screenWidth = screenWidth;
            this.screenHeight = screenHeight;
    
            // Initialize our drawing objects
            ourHolder = getHolder();
            paint = new Paint();
    
            // Initialize our array list
            backgrounds = new ArrayList<>();
    
            //load the background data into the Background objects and
            // place them in our GameObject arraylist
    
            backgrounds.add(new Background(
                    this.context,
                    screenWidth,
                    screenHeight,
                    "bg", 0, 120, 50));
    
            backgrounds.add(new Background(
                    this.context,
                    screenWidth,
                    screenHeight,
                    "grass", 70, 110, 200));
    
            // Add more backgrounds here
    
        }
    
        @Override
        public void run() {
    
            while (running) {
                long startFrameTime = System.currentTimeMillis();
    
                update();
                if (j > 2000) {
                    j = -50;
                    k = 0;
                }
                if (o > 2000) {
                    o = -50;
                    l = 0;
                }
                draw();
    
                // Calculate the fps this frame
                long timeThisFrame = System.currentTimeMillis() - startFrameTime;
                if (timeThisFrame >= 1) {
                    fps = 1000 / timeThisFrame;
                }
            }
        }
    
        int numberOfshots = 1;
        int[] i = new int[200];
        int j = 0;
        int k = 0;
        int l = 0;
        int m = 0;
        int o = 0;
        boolean down = true;
        long lastTurn = System.currentTimeMillis();
        int xbuggy = 0;
        int xbuggy2 = 0;
        boolean down2 = true;
        long lastTurn2 = System.currentTimeMillis();
        long lastTurn3 = System.currentTimeMillis();
        long lastTurn4 = System.currentTimeMillis();
        boolean jump = false;
        boolean shoot = false;
        int ind = 0;
        int numberOfAlienshots = 1;
        int missileOffSetY = 0;
        private void draw() {
    
            if (ourHolder.getSurface().isValid()) {
                //First we lock the area of memory we will be drawing to
                canvas = ourHolder.lockCanvas();
                if (jump) {
                    xbuggy = xbuggy + 4;
                }
                if (shoot) {
                    xbuggy2 = xbuggy2 + 4;
                }
    
    
    
                if (System.currentTimeMillis() - lastTurn4 >= 2000) {
                    // Change direction here
                   //jump = false;
                    lastTurn4 = System.currentTimeMillis();
                    missileOffSetY = 0;
                }
    
                if (System.currentTimeMillis() - lastTurn3 >= 1000) {
                    // Change direction here
                    jump = false;
                    lastTurn3 = System.currentTimeMillis();
                    xbuggy = 0;
                }
                //draw a background color
                canvas.drawColor(Color.argb(255, 0, 0, 0));
    
                // Draw the background parallax
                drawBackground(0);
    
                // Draw the rest of the game
                paint.setTextSize(60);
                paint.setColor(Color.argb(255, 255, 255, 255));
    
                //canvas.drawText("MOONPATROL3000", 350, screenHeight / 100 * 5, paint);
    
                int resID = context.getResources().getIdentifier("vehicle",
                        "drawable", context.getPackageName());
    
                Alien alien1 = new AttackingAlien(context, "right_side_hdpi");
                Alien alien2 = new AttackingAlien(context, "object2_hdpi");
                Alien alien3 = new AttackingAlien(context, "object1_hdpi");
    
                int alienResID = context.getResources().getIdentifier("right_side_hdpi",
                        "drawable", context.getPackageName());
    
                int alienResID2 = context.getResources().getIdentifier("right_side_hdpi",
                        "drawable", context.getPackageName());
    
                int alienResID3 = context.getResources().getIdentifier("right_side_hdpi",
                        "drawable", context.getPackageName());
    
                // Load the bitmap using the id
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
                Bitmap alienbitmap = BitmapFactory.decodeResource(context.getResources(), alienResID);
                Bitmap alienbitmap2 = BitmapFactory.decodeResource(context.getResources(), alienResID2);
                Bitmap alienbitmap3 = BitmapFactory.decodeResource(context.getResources(), alienResID3);
    
                //paint.setTextSize(220);
    
                //for (int i1 = 0; i1 < numberOfAlienshots; i1++) {
                if (missileOffSetY < 300) {
                    canvas.drawText("|", o + 10 + alienbitmap2.getWidth() / 2, l + screenHeight / 100 * 25 + 75 + missileOffSetY, paint);
    
                    missileOffSetY = missileOffSetY + 10;
                }
    
                for (int i1 = 0; i1 < numberOfshots; i1++) {
    
    
                    // if horizontal missile hits alien 0
                    if (java.lang.Math.abs(j - i[i1]) * 2  < (alien1.getWidth() + 60) && java.lang.Math.abs(k +150+ screenHeight / 100 * 45 - (float) (screenHeight * 0.61)) * 2  < (alien1.getHeight() + 60)) {
                        //y1[i2] = -random.nextInt(1000); // reset to new vertical position
                        //score += 1;
                        //onScoreListener.onScore(score);
                        Log.d("missile", "missile hit! ");
                        j=-200;
                    }
    
                    // if vertical missile hits alien 0
                    if (java.lang.Math.abs(j - 185) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(j + 150 + screenHeight / 100 * 45 - (screenHeight / 100 * 95 - i[i1] - xbuggy2)) * 2  < (alienbitmap.getHeight() + 60)) {
                        j=-200;
                    }
    
    
                    // if horizontal missile hits alien 1, right now this won't happen
                    if (java.lang.Math.abs(j - i[i1]) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(k +150+ screenHeight / 100 * 45 - (float) (screenHeight * 0.61)) * 2  < (alienbitmap.getHeight() + 60)) {
                        j=-200;
                    }
    
                    // if vertical missile hits alien 1
                    if (java.lang.Math.abs(o + 10 - 185) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(l + screenHeight / 100 * 25 - (screenHeight / 100 * 95 - i[i1] - xbuggy2)) * 2  < (alienbitmap.getHeight() + 60)) {
                        o=-200;
                    }
    
    
                    canvas.drawText("o", i[i1], (float) (screenHeight * 0.61), paint);
                    canvas.drawText("o", 185, screenHeight / 100 * 95 - i[i1] - xbuggy2, paint);
    
    
    
    
                    if (i1 == numberOfshots - 1 && i[i1] > screenWidth) {
                        if (numberOfshots > 0) numberOfshots--;
                        if (ind > 0) ind--;
                    }
                }
                if (System.currentTimeMillis() - lastTurn >= 2000) {
                    // Change direction here
                    down = !down;
                    lastTurn = System.currentTimeMillis();
                }
    
                if (System.currentTimeMillis() - lastTurn2 >= 7000) {
                    // Change direction here
                    down2 = !down2;
                    lastTurn2 = System.currentTimeMillis();
                }
    
              //  canvas.drawBitmap(alien1.getAlienbitmap(), j, k +150+ screenHeight / 100 * 45, paint);
                canvas.drawBitmap(alienbitmap2, o + 10, l + screenHeight / 100 * 25, paint);
                //canvas.drawBitmap(alienbitmap3, j+20, k+screenHeight / 100 * 5, paint);
                drawBackground(1);
                canvas.drawBitmap(bitmap, 50, (float) (screenHeight * 0.5) - xbuggy, paint);
                // Draw the foreground parallax
    
                for (int n = 0; n < numberOfshots; n++)
                    i[n] = i[n] + 20;
    
                j = j + 10;
                o = o + 7;
                if (!down)
                    k=k+2;
                else
                    k=k-2;
    
                if (!down2)
                    l++;
                else
                    l--;
    
                // Unlock and draw the scene
                ourHolder.unlockCanvasAndPost(canvas);
            }
        }
    
        // Clean up our thread if the game is stopped
        public void pause() {
            running = false;
            try {
                gameThread.join();
            } catch (InterruptedException e) {
                // Error
            }
        }
    
        // Make a new thread and start it
        // Execution moves to our run method
        public void resume() {
            running = true;
            gameThread = new Thread(this);
            gameThread.start();
        }
    
        private void drawBackground(int position) {
    
            // Make a copy of the relevant background
            Background bg = backgrounds.get(position);
    
            // define what portion of images to capture and
            // what coordinates of screen to draw them at
    
            // For the regular bitmap
            Rect fromRect1 = new Rect(0, 0, bg.width - bg.xClip, bg.height);
            Rect toRect1 = new Rect(bg.xClip, bg.startY, bg.width, bg.endY);
    
            // For the reversed background
            Rect fromRect2 = new Rect(bg.width - bg.xClip, 0, bg.width, bg.height);
            Rect toRect2 = new Rect(0, bg.startY, bg.xClip, bg.endY);
    
            //draw the two background bitmaps
            if (!bg.reversedFirst) {
                canvas.drawBitmap(bg.bitmap, fromRect1, toRect1, paint);
                canvas.drawBitmap(bg.bitmapReversed, fromRect2, toRect2, paint);
            } else {
                canvas.drawBitmap(bg.bitmap, fromRect2, toRect2, paint);
                canvas.drawBitmap(bg.bitmapReversed, fromRect1, toRect1, paint);
            }
        }
    
        // Because we call this from onTouchEvent, this code will be executed for both
        // normal touch events and for when the system calls this using Accessibility
        @Override
        public boolean performClick() {
            super.performClick();
            launchMissile();
            return true;
        }
    
        private void launchMissile() {
            i[ind] = 350; // what does it do?
            ind++;
            xbuggy2 = 0;
            shoot = true;
        }
    
        // event listener for when the user touches the screen
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            boolean gameOver = false;
            //if (paused) {
            //   paused = false;
            //}
            int action = MotionEventCompat.getActionMasked(event);
            int coordX = (int) event.getX();
            int coordY = (int) event.getY();
            Log.d("coordY", "coordY " + coordY);
            if (coordX < 220 && xbuggy == 0 && action == MotionEvent.ACTION_MOVE) {
                jump = true;
                shoot = false;
                lastTurn3 = System.currentTimeMillis();
                return true; // do nothing
            }
    
            if (coordX > 219 && action == MotionEvent.ACTION_DOWN) {
                numberOfshots++;
                performClick();
                return true;
            }
            return true;
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Tatarize    6 年前

    您最大的错误似乎是在绘图例程中分配了4个位图。在oncreate中分配这些位图,并简单地调用您初始化的oncreate()全局位图。那会解决你的问题。你可以在它们的位置画它们。

        private void draw() {
            Alien alien1 = new AttackingAlien(context, "right_side_hdpi");
            Alien alien2 = new AttackingAlien(context, "object2_hdpi");
            Alien alien3 = new AttackingAlien(context, "object1_hdpi");
    

    您分配了一堆内存对象来调用上下文并扩展drawable和一堆其他工作。你可以用上一次的外星人。

            int alienResID = context.getResources().getIdentifier("right_side_hdpi",
                    "drawable", context.getPackageName());
    
            int alienResID2 = context.getResources().getIdentifier("right_side_hdpi",
                    "drawable", context.getPackageName());
    
            int alienResID3 = context.getResources().getIdentifier("right_side_hdpi",
                    "drawable", context.getPackageName());
    

    外星人的身份和之前的一样。

            // Load the bitmap using the id
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
            Bitmap alienbitmap = BitmapFactory.decodeResource(context.getResources(), alienResID);
            Bitmap alienbitmap2 = BitmapFactory.decodeResource(context.getResources(), alienResID2);
            Bitmap alienbitmap3 = BitmapFactory.decodeResource(context.getResources(), alienResID3);
    

    这些是上一个tick中的相同位图,位图是巨大的,从资源中获取它们很慢,您正在执行此操作 每个 滴答声。

        }
    }
    

    大多数其他的东西都会在这里和那里剃掉半毫秒,但这可能会让你达到正确的fps球场。


    别担心,尽管你还有别人。

    你的大部分问题都是通过加快程序来解决的。现在是时候说,你做错了。典型且正确的方法是每隔17ms左右进行一次循环计时。剩下的时间是暂停的。有些错误是显而易见的。

    您最大的错误似乎是在绘图例程中分配了4个位图。

    但是,抽签程序 只有 绘制。你在画布上画出需要发生的事情就这样。你不分配任何东西,你不膨胀任何东西,你把你拥有的数字放在那里,画出你已经在那个位置的内存中加载的东西。

    在绘图程序中,您正在滴答并执行碰撞检测,并为此分配一堆对象,但它们必须在瞬间被扔进削木机。

    您不应该在初始化之外创建任何对象,或者在存在新外星体的特殊情况下创建任何对象。你不应该在绘图程序中的任何地方使用“new”。曾经。

    你正在使用蛮力进行碰撞检测,最终没有。找到一个你喜欢的好的加速结构并使用它。不过,对于一个对象来说,这并不重要。

    不要调用某个外星人类,虽然它看起来更漂亮,但您需要外星人的边界框的原始数字。然后,您需要将它们保存在某种结构中,以便能够非常快速地引用它们(您需要的帧小于17ms)。调用一组width命令并不是很有帮助,即使它们改变了大小,也只需更改hitbox的编号。这些方法允许您对数据使用一些很好的结构,例如有一个已排序的Hitbox数组,您可以对它们进行二进制搜索,并在结构的Log(n)更新的情况下查找移动的对象是否在Log(n)时间内命中对象,或者使用一些不同的方法遍历轴对齐的绑定框树。这是你最终需要的东西,但如果不这样做,只要你保持简单就行了。不过,老实说只是你的位图 慢下来的。

    还有很多其他基本问题,比如将边界框放在if语句中,而不是生成两个额外的矩形。但是还有其他的问题,比如做长方形!你用实际的位置来调用绘图,而不是一些分配一个大屁股的对象来调用一个函数。用数字调用函数就行了。


    你应该有一个程序,为你绘制基于位置的东西。它应该能够在17毫秒内画出它需要画的所有东西。如果画不出来,你就永远不会达到你需要达到的每秒60帧。所以在这种情况下,少画一些东西,做得更好。空间背景需要是位图吗?你能为天空画一堆点,然后相应地调整图形吗?你的绘图程序从不分配任何内容。周期。如果你需要分配一些东西,它应该在初始化期间。分配是你生存的祸根。

    你的触摸更新了东西的位置。人工智能/物理勾号也会更新事物的位置,并检查是否存在串通。画图只根据记忆中的位置和内容绘制内容。

    在自己的线程中运行更新位置标记。您只需要处理读写相同数据的并发位。它只需要同步绘图数据的变化数据读取,因此将这些部分(触摸位置更新、勾选位置更新和获取绘图例程本身的位置)放入具有相同对象的同步块中。