我做了一个非常基本的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的方式,我也会很高兴。
答案 0 :(得分:1)
通过一些更改和错误修复,我设法使您的应用程序运行起来。这不是最终的解决方案,而是可以帮助您前进的一些方法。
从顶部
我更改了延迟值,这使游戏变得可玩,因为看起来100可以缩短时间。您可能不喜欢我的可玩性价值观,但至少可以通过我的价值观对游戏进行测试。
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
方法的意义,因此我删除了该方法并在顶部声明了一个新成员< / p>
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来使数组更小,这又使游戏更具可玩性(尽管它使图形有些混乱),从而更易于测试和修复错误。