Java游戏,颜色不出现

阿林·穆兰

我有一个名为Wave的游戏的代码,通常在运行它时,它应该是一个黑色的窗口,上面有白色的正方形。但是窗户是白色的,窗户左侧有很细的黑色条纹。我几乎看不到它。

有谁知道为什么会这样吗?

package wave.myFirstGame;

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 = 3580879553502102315L;
    public static final int WITDH = 640, HEIGHT = WITDH / 12 * 9;

    private Thread thread;
    private boolean running = false;

    private Random r;
    public Handler handler;

    public Game() {
        new Window(WITDH, HEIGHT, "Wave", 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));
        }

        handler.addObject(new Player(200, 200, ID.Player));
    }

    public synchronized void start() {// initializing the thread
        thread = new Thread(this);
        thread.start();
        running = true;
    }

    public synchronized void stop() {
        try {
            thread.join();
            running = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // GAME LOOP
    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 > 1000) {
                timer += 1000;
                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();
        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();
    }
}
北极领主

看一下Canvas的API 在那里,您会发现:

从接口java.awt.image.ImageObserver继承的字段
[...] HEIGHT,[...],WIDTH

因此,由于从Canvas类中扩展了类,因此您已经具有WIDTH和HEIGHT常量,并且由于某种原因WIDTH接缝具有值1
因此,只需重命名常量,它就会按预期显示。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章