我的播放器出现问题。我创建了Player类并在游戏循环的渲染功能中实现了它的draw()
方法,但是没有玩家。也没有错误。有人可以帮我找出玩家没有出现的原因吗?
窗口左侧和底部也有一个边框,我无法摆脱它。有人知道吗?
这是游戏类
@SuppressWarnings("serial")
public class Game extends Canvas implements Runnable {
private static Game _instance;
private static final String TITLE = "ProjectG";
public static final int WIDTH = 240;
public static final int HEIGHT = WIDTH * 3 / 4;
private static final int SCALE = 4;
public static final Dimension SIZE = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
private static final int UPDATE_RATE = 60;
private static final int RENDER_RATE = 60;
private JFrame _frame;
private Thread _thread;
private boolean _running = false;
private int _tps = 0;
private int _fps = 0;
private int _totalTicks = 0;
private BufferedImage image;
private Graphics2D g;
private Player player;
public Game() {
_instance = this;
setPreferredSize(SIZE);
setMinimumSize(SIZE);
setMaximumSize(SIZE);
_frame = new JFrame(TITLE);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_frame.setLayout(new BorderLayout());
_frame.add(_instance, BorderLayout.CENTER);
_frame.pack();
_frame.setResizable(false);
_frame.setLocationRelativeTo(null);
_frame.setVisible(true);
player = new Player();
}
public synchronized void start() {
_running = true;
_thread = new Thread(this, TITLE + "_main");
_thread.start();
}
public synchronized void stop() {
_running = false;
if (_thread != null) {
try {
_thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void run() {
double lastUpdateTime = System.nanoTime();
double lastRenderTime = System.nanoTime();
final int ns = 1000000000;
final double nsPerUpdate = (double) ns / UPDATE_RATE;
final double nsPerRender = (double) ns / RENDER_RATE;
final int maxUpdatesBeforeRender = 1;
int lastSecond = (int) lastUpdateTime / ns;
int tickCount = 0;
int renderCount = 0;
image = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
while (_running) {
long currTime = System.nanoTime();
int tps = 0;
while ((currTime - lastUpdateTime) > nsPerUpdate && tps < maxUpdatesBeforeRender) {
update();
tickCount++;
_totalTicks++;
tps++;
lastUpdateTime += nsPerUpdate;
}
if (currTime - lastUpdateTime > nsPerUpdate) {
lastUpdateTime = currTime - nsPerUpdate;
}
float interpolation = Math.min(1.0F, (float) ((currTime - lastUpdateTime) / nsPerUpdate));
render(interpolation);
draw();
renderCount++;
lastRenderTime = currTime;
int currSecond = (int) (lastUpdateTime / ns);
if (currSecond > lastSecond) {
_tps = tickCount;
_fps = renderCount;
tickCount = 0;
renderCount = 0;
lastSecond = currSecond;
System.out.println(_tps + " TPS " + _fps + " FPS");
}
while (currTime - lastRenderTime < nsPerRender && currTime - lastUpdateTime < nsPerUpdate) {
Thread.yield();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
currTime = System.nanoTime();
}
}
}
public void update() {
player.update();
}
public void render(float interpolation) {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
}
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH * SCALE, HEIGHT * SCALE);
g.setColor(Color.BLACK);
g.drawString("TPS: " + _fps + " FPS: " + _fps, 10, 20);
player.draw(g);
}
public void draw() {
Graphics g2 = this.getGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
}
}
这是播放器类
public class Player {
private int x;
private int y;
private int r;
private int dx;
private int dy;
private int speed;
private boolean left;
private boolean right;
private boolean up;
private boolean down;
private int lives;
public Player() {
x = Game.WIDTH / 2;
y = Game.HEIGHT / 2;
dx = 0;
dy = 0;
speed = 5;
lives = 3;
}
public void update() {
if (left) {
dx = -speed;
} else if (right) {
dx = speed;
} else if (up) {
dy = -speed;
} else if (down) {
dy = speed;
}
x += dx;
y += dy;
if (x < r)
x = r;
if (y < r)
y = r;
if (x > Game.WIDTH - r)
x = Game.WIDTH - r;
if (y > Game.HEIGHT - r)
y = Game.HEIGHT - r;
dx = 0;
dy = 0;
}
public void draw(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillOval(x - r, y - r, 2 * r, 2 * r);
g.setStroke(new BasicStroke(3));
g.setColor(Color.BLACK.brighter());
g.drawOval(x - r, y - r, 2 * r, 2 * r);
g.setStroke(new BasicStroke(1));
}
public int getLives() {
return lives;
}
}
如果需要,这里是Main类
public class Main {
private static Game _game;
public static void main(String[] args) {
_game = new Game();
_game.start();
}
}
答案 0 :(得分:0)
如果您从未初始化r
,则默认为0
,以及如下所示的行。
g.drawOval(x - r, y - r, 2 * r, 2 * r);
实际上不会画任何东西。