为什么这个基于数组的 Java 游戏不起作用?

Why doesn't this array based Java game work?

我制作了一个非常基本的 2D 游戏,你必须击落来自 5 个不同轨道的敌人。我创建了一个二维数组(轨道)来存储敌人、射弹的位置。该阵列为 790 宽,因为轨道为 790 像素长。我正在使用游戏循环来更新和渲染,效果很好。

但由于循环受计算机性能的影响,我使用 ScheduledExecutorService class 来执行敌人的移动和生成,但由于某种原因它不起作用,敌人没有移动,有时甚至不产卵,射弹也不会移动。该程序不会给出错误,它只是不起作用。我检查了一下,没有语法错误,至少据我所知,我也没有发现任何逻辑问题。

因为我还是初学者,所以请给出一个简短但不要太复杂的答案。

代码:(控制为 S、D / 向上、向下移动和 space 射击)

package com.bacskai.peashooter;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable, KeyListener {

    private static final long serialVersionUID = -4227990863874935837L;

    JFrame frame;
    static Dimension d;
    Thread thread;

    public static int width = 300;
    public static int height = width / 16 * 9;
    int scale = 3;

    boolean running;
    boolean alive = true;

    int[][] track = new int[5][790]; // the array

    int trackY = 2; // the track which the player is on

    private int playerPos = 250;
    private int playerPos1 = 220; // the 3 starting points for the triangle
    private int playerPos2 = 280;

    int health = 3;
    int score = 0;

    long delay = 100;
    long delay2 = 5000;

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    ScheduledExecutorService executor2 = Executors.newScheduledThreadPool(1);



    Runnable task = new Runnable() {

        public void run() {
        move();
        }
    };

    Runnable task2 = new Runnable() {

        public void run() {
            spawnEnemy();
        }
    };

    public Game() {

        d = new Dimension(width * scale, height * scale);
        setPreferredSize(d);
        frame = new JFrame();

    }

    private void start() {

        thread = new Thread(this, "thread");
        running = true;
        thread.start();

        for (int i = 0; i == 5; i++) {
            for (int j = 0; j == 790; j++) { // initializing the array
                track[i][j] = 0;
            }
        }       

        executor.scheduleAtFixedRate(task, delay, delay, 
        TimeUnit.MILLISECONDS); // moveing
        executor2.scheduleAtFixedRate(task2, delay2, delay2, 
        TimeUnit.MILLISECONDS); // spawning new enemies

        System.out.println("Game started, window width: " + getWidth() + ", 
        height: " + 
        getHeight());

    }

    public void run() {

        while (running) { // game loop
            update();
            if (alive) {
            render();
            }
        }

    }

    private void stop() {

        try {

            frame.dispose();
            thread.join();
            executor.shutdownNow();
            executor2.shutdownNow();

        } catch (Exception e) {
            System.out.println("Error while closing: " + e);
        }
        System.out.println("Program closed, processes halted");
    }

    public void update() {

        if (health == 0) {
            alive = false;
            executor.shutdownNow();
            executor2.shutdownNow();
            System.out.println("Game over");

        }

    }

    private void render() {

    BufferStrategy bs = getBufferStrategy();

    if (bs == null) {createBufferStrategy(3); return;}

    Graphics g = bs.getDrawGraphics();

    //  Map

    g.setColor(Color.black);
    g.fillRect(0, 0, getWidth(), getHeight());

    g.setColor(Color.cyan);
    g.fillRect(100, 0, 10, 490);
    g.fillRect(0, 98, 900, 10);
    g.fillRect(0, 196, 900, 10);
    g.fillRect(0, 294, 900, 10);
    g.fillRect(0, 392, 900, 10);

    //  Score / health

    Font font = new Font("Default", Font.PLAIN, 30);
    g.setFont(font);

    g.setColor(Color.red);
    g.drawString("Score: " + score, 740, 30);

    g.setColor(Color.yellow);
    g.drawString("Health: " + health, 600, 30);

    //  Player

    g.setColor(Color.green);
    int[] xPoints = {10, 10, 60};
    int[] yPoints = {playerPos1, playerPos2, playerPos};
    g.fillPolygon(xPoints, yPoints, 3);

    //  Enemies

    g.setColor(Color.red);
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 790; j++) {
            if (track[i][j] == 1) {
                g.fillRect(100 + j, i * 97 + 44, 30, 30);
            }
        }
    }

    //  Projectiles
    g.setColor(Color.green);

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 790; j++) {
            if (track[i][j] == 2) {
                g.fillOval(110 + j, i * 97 + 44, 27, 27);
            }
        }
    }

    bs.show();
    g.dispose();    
    } // End of render

    public int randInt(int min, int max) {

        Random rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + 1;
        return randomNum;

        }

    public void keyTyped(KeyEvent e) {}

    public void keyPressed(KeyEvent e) {

        if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            stop();
        }

        if(e.getKeyCode() == KeyEvent.VK_UP && trackY > 0) {
            trackY--;
            playerPos -= 98;
            playerPos1 -= 98;
            playerPos2 -= 98;
            System.out.println("Key pressed: up");
        }

        if(e.getKeyCode() == KeyEvent.VK_DOWN && trackY < 4) {
            trackY++;
            playerPos += 98;
            playerPos1 += 98;
            playerPos2 += 98;
            System.out.println("Key pressed: down");
        }

        if(e.getKeyCode() == KeyEvent.VK_SPACE) {
            shoot();
        }


    }

    public void keyReleased(KeyEvent e) {}

    public void shoot() {

            System.out.println("Player shot projectile from: " + (trackY + 1));
            track[trackY][0] = 2;   

    }

    public void move() {

        System.out.print("asd");
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 790; j++) {

                if (track[i][j] == 2 && track[i][j + 2] == 1) {
                    track[i][j] = 0;
                    track[i][j + 2] = 0;
                    break;
                }

                    switch (track[i][j]) {

                        case 0: // 0 ==> empty position

                            break;

                        case 1: // 1 ==> enemy

                            if (j != 0) {

                            track[i][j - 1] = 1;
                            track[i][j] = 0;


                            } else {
                                track[i][j] = 0;
                                enemyArrived();
                            }
                            System.out.print("");
                            break;

                        case 2: // 2 ==> projectile

                            if (j == 789) {
                                track[i][j] = 0;
                                break;
                            }

                            track[i][j + 1] = 2;
                            track[i][j] = 0;
                            System.out.print("");
                            break;

                        default:

                            System.out.println("Unable to identify object type at: track[" + i + "][" + j + "]");
                            break;

                }
            }
        }

    }

    public void spawnEnemy() {

        int trakk = randInt(1, 5);
        track[trakk][789] = 1;

        System.out.println("New enemy at: " + trakk);
    }

    public void enemyArrived() {

        health--;
        System.out.println("Player lost a health point, current health: " + health);

    }


    public static void main(String[] args) {

        Game game = new Game();

        game.frame.setResizable(false);
        game.frame.setTitle("Peasooter");
        game.frame.add(game);
        game.frame.pack();
        game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.frame.setLocationRelativeTo(null);
        game.frame.setVisible(true);
        game.frame.addKeyListener(game);

        game.start();

    }

}

此外,如果有人能告诉我是否有优化此游戏的方法,使其不消耗 cpu 的 25% - 30%,我将非常高兴。

通过一些更改和错误修复,我已经设法使您的应用程序 运行。这不是一个完整的解决方案,而是可以帮助您前进的东西。

自上而下

我更改了延迟值,这使游戏可以玩了,因为 100 似乎是很短的时间。你可能不喜欢我的可玩性价值观,但至少可以通过我的价值观来测试游戏。

long delay = 1000;
long delay2 = 3000;

有两个 ScheduledExecutorService 对象没有意义,所以删除第二个。

我不知道你为什么要从那里生成一个线程 运行 你的代码所以我删除了它。

//thread = new Thread(this, "thread");
running = true;
//thread.start();

并在 start() 末尾手动调用 run() 方法,因此 start() 的后半部分是

    executor.scheduleAtFixedRate(task, delay, delay,
            TimeUnit.MILLISECONDS); // moveing
    executor.scheduleAtFixedRate(task2, delay2, delay2,
            TimeUnit.MILLISECONDS); // spawning new enemies

    System.out.println("Game started, window width: " + getWidth() + ", height: " + getHeight());

    run();
}

你到处都是你的for循环,在一个地方你有i < 6作为限制,而在另一个地方你有i == 5(这总是错误的)。遍历所有循环并确保它们被定义为

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 790; j++) {

同时正确调用 randInt 以适合您的数组大小

int trakk = randInt(0, 4);

游戏仍然表现得很奇怪,但大多数时候我会遇到一些红色敌人,我可以将它们击落。


更新

我又玩了一些,又做了两处改动。

您的随机生成器方法每次调用时都会创建一个新的 Random 对象,这是不必要的,而且我没有看到使用 randInt 方法的意义,所以我删除了该方法并在顶部

Random rand = new Random();

然后在 randInt 的调用位置使用它

int trakk = rand.nextInt(NUMBER_OF_ROWS);

为了看看我是否可以提高性能,我引入了 3 个常量,然后将它们用于数组和 for 循环(用它们替换所有出现的 5 和 790)

private static final int NUMBER_OF_ROWS = 5;
private static final int NUMBER_OF_PIXELS = 790;
private static final int LAST_PIXEL = NUMBER_OF_PIXELS - 1;

然后我通过将两个第一个更改为 3 和 100 使数组更小,这再次使游戏更具可玩性(尽管它弄乱了一些图形)使测试和修复错误更容易。