gpt4 book ai didi

java - 如何制作 Java Canvas 游戏循环

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

我一直在尝试在 Canvas 中创建一个运行 render(Graphics g) 和logic() 的 java 游戏循环,但它不起作用。

我已经尝试制作一个运行这两个函数的脚本,然后再次调用自身来创建循环。

提前致谢。

我尝试过这个:

public class Canvas extends JPanel {
static GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
static int width = gd.getDisplayMode().getWidth();
static int height = gd.getDisplayMode().getHeight();
//Get the system time
long lastTime = System.nanoTime();
//Specify how many seconds there are in a minute as a double
//store as a double cause 60 sec in nanosec is big and store as final so it can't be changed
final double ticks = 60D;
//Set definition of how many ticks per 1000000000 ns or 1 sec
double ns = 1000000000 / ticks;
double delta = 0;

public static void main(String[] args) {
JFrame frame = new JFrame("Zombie Run");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Canvas());
frame.setSize(width, height);
frame.setVisible(true);
loop();
}

public static void loop() {
logic();
render(g);
loop();
}
public static void render(Graphics g) {
System.out.println("Running Render");
g.setColor(Color.BLUE);
g.fillRect(0, 0, 800, 500);

}
public static void logic() {
System.out.println("Logic");

}

}

我从 eclipse 得到的错误是:

Logic
Running Render
Exception in thread "main" java.lang.NullPointerException
at ZombieGame.Canvas.render(Canvas.java:41)
at ZombieGame.Canvas.loop(Canvas.java:36)
at ZombieGame.Canvas.main(Canvas.java:31)

最佳答案

您可以使用Swing Timer 。 while 循环和 Thread.sleep 只会导致 GUI 卡住。

我使用 Timer 类制作了一个示例。它创建一个延迟 42 毫秒的计时器。每次延迟之后,它都会执行 ActionListener 内的代码。该示例在面板上绘制随机矩形,有点像 Linux 屏幕保护程序 :P 我认为您不必太担心我具体是如何做到这一点的,这只是为了说明 Timer可以使用。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Foo {

public static void main(String[] args) throws IOException {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Foo().createAndShowGUI();
}
});
}

public void createAndShowGUI() {
DrawingPanel panel = new DrawingPanel();

Timer timer = new Timer(42, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.addNewRectangle();
panel.repaint();
}
});
timer.start();

JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.setSize(800, 600);
frame.setVisible(true);
}

private class GameRectangle extends Rectangle {
private Color color;

public GameRectangle(int x, int y, int width, int height, Color color) {
setBounds(x, y, width, height);
setColor(color);
}

public void draw(Graphics2D context) {
context.setColor(getColor());
context.fill(this);
}

public Color getColor() {
return color;
}

public void setColor(Color color) {
this.color = color;
}
}

private class DrawingPanel extends JPanel {
private final Random RANDOM = new Random();
private ArrayList<GameRectangle> rectangles;

public DrawingPanel() {
setRectangles(new ArrayList<>());
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
for (GameRectangle rectangle : getRectangles()) {
rectangle.draw(g2);
}
g2.dispose();
}

public void addNewRectangle() {
getRectangles().add(createRandomRectangle());
}

private Color createRandomColor() {
return new Color(RANDOM.nextInt(255), RANDOM.nextInt(255), RANDOM.nextInt(255));
}

private GameRectangle createRandomRectangle() {
return new GameRectangle(RANDOM.nextInt(getWidth()), RANDOM.nextInt(getHeight()), RANDOM.nextInt(420),
RANDOM.nextInt(420), createRandomColor());
}

public ArrayList<GameRectangle> getRectangles() {
return rectangles;
}

public void setRectangles(ArrayList<GameRectangle> rectangles) {
this.rectangles = rectangles;
}
}

}

关于java - 如何制作 Java Canvas 游戏循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39245804/

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