我正在尝试创建一个程序,它将打开一个新窗口,显示50个方块,并使它们在屏幕上随机移动。这会渲染它们,但它们不会移动。
package game;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1550691097823471818L;
public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
private Random r;
private Handler handler;
public Game(){
new Window(WIDTH, HEIGHT, "Test", this);
handler = new Handler();
r = new Random();
for(int i = 0; i < 50; i++){
handler.addObject(new Player(r.nextInt(WIDTH), r.nextInt(HEIGHT), ID.Player));
}
}
public synchronized void start(){
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop(){
try{
thread.join();
running = false;
}catch(Exception e){
e.printStackTrace();
}
}
public void run(){
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1){
tick();
delta--;
}
if(running)
render();
frames++;
if(System.currentTimeMillis() - timer > 10000){
timer += 10000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
private void tick(){
handler.tick();
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
while(1==1){
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
handler.render(g);
g.dispose();
bs.show();
}
}
public static void main(String args[]) {
new Game();
}
}
这是玩家类
package game;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Player extends GameObject {
Random r = new Random();
public Player(int x, int y, ID id){
super(x, y, id);
velX = r.nextInt(5) + 1;
velY = r.nextInt(5) + 1;
}
public void tick(){
x += velX;
y += velY;
}
public void render(Graphics g){
g.setColor(Color.white);
g.fillRect(x, y, 32, 32);
}
}
处理程序类
package game;
import java.awt.Graphics;
import java.util.LinkedList;
public class Handler {
LinkedList<GameObject> object = new LinkedList<GameObject>();
public void tick(){
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.tick();
}
}
public void render(Graphics g){
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.render(g);
}
}
public void addObject(GameObject object){
this.object.add(object);
}
public void removeObject(GameObject object){
this.object.remove(object);
}
}
答案 0 :(得分:2)
好的,我想通了,在处理程序渲染方法中,你永远不会调用tick方法。所以将方法更改为:
public void render(Graphics g){
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.tick();
tempObject.render(g);
}
}
我对它进行了测试,并且它有效,它实际上是一个非常好的程序。
答案 1 :(得分:0)
在Player类中的tick方法稍有错误,它应该是
public void tick() {
x = x + velX;
y = y + velY;
}
+ =或 - =正在加/减字节。