gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:05:33 25 4
gpt4 key购买 nike

我制作了一个非常基本的 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,我会非常高兴。

最佳答案

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

从上往下

我更改了延迟值,这使得游戏可以玩,因为看起来 100 时间太短了。你可能不喜欢我的可玩性值(value)观,但按照我的值(value)观,至少可以测试游戏。

long delay = 1000;
long delay2 = 3000;

拥有两个 ScheduledExecutorService 没有意义对象,因此删除第二个。

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

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

并手动调用run() start()末尾的方法所以有了这个和单一执行者 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,使数组变得更小,这再次使游戏更具可玩性(尽管它弄乱了一些图形),从而更容易测试和修复错误。

关于java - 为什么这个基于数组的 Java 游戏不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51003988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com