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

问题描述 投票:0回答:1

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

但是因为循环受到计算机性能的影响,我使用ScheduledExecutorService类来执行敌人的移动和产生但由于某种原因它不起作用,敌人不移动或有时甚至不产生和弹丸不动。程序没有出错,它只是不起作用。我查了一下,没有语法错误,我至少在我的知识中找不到任何逻辑问题。

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

代码:(控制是S,D /向上,向下移动和射击空间)

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();

    }

}

如果有人能告诉我是否有一种方法可以优化这款游戏而不消耗25%-30%的cpu,我也会非常高兴。

java arrays 2d-games scheduledexecutorservice
1个回答
1
投票

我已经设法让你的应用程序运行一些更改和错误修复。这不是一个完成的解决方案,但可以帮助您前进。

从一开始

我改变了延迟值,这使得游戏可玩,因为它似乎是100短时间的方式。你可能不喜欢我的可玩性值,但是我的价值观至少可以测试游戏。

long delay = 1000;
long delay2 = 3000;

没有必要拥有两个ScheduledExecutorService对象,所以删除第二个。

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

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

并在run()结束时手动调用start()方法,所以使用它和单个执行程序start()is的后半部分

    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来使阵列变得更小,这再次使游戏更具可玩性(尽管它使图形搞砸了一些)使得更容易测试和修复错误。

© www.soinside.com 2019 - 2024. All rights reserved.