java - java eclipse 错误的 Minecraft 克隆

标签 java minecraft

我观看了有人制作《我的世界》克隆版的 Youtube 视频,该视频编译并运行没有错误。但是当我完全复制它时,出现错误。

错误出现在 Render 类中:int Pixels = new int(width * height);

渲染:

package com.mads.minecraft.graphics;

public class Render {
    public final int width;
    public final int height;
    public int[] pixels;

    public Render(int width, int height){
        this.width = width;
        this.height = height;
        int pixels = new int(width * height);

    }

    public void draw(Render render, int xOffset, int yOffset){
        for (int y = 0; y < render.height; y++){
            int yPix = y + yOffset;
            if(yPix < 0 || yPix >= height) continue;

            for (int x = 0; y < render.width; x++){
                int xPix = x + xOffset;
                if(xPix < 0 || xPix >= width) continue;

                pixels[xPix + yPix + width] = render.pixels[x + y * render.width];
            }
        }
    }
}

显示:

package com.mads.minecraft;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

import com.mads.minecraft.graphics.Screen;

public class Display extends Canvas implements Runnable{ 

    static final long serialVersionUID = 1L;
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    public static final String TITLE = "Minecraft Alpha";

    private Thread thread;
    private boolean running = false;
    private BufferedImage img;
    private Screen screen;
    private int[] pixels;

    public Display(){
        screen = new Screen(WIDTH, HEIGHT);
        img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
    }

    public void start(){
        if(running) return;
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public void stop(){
        if(!running) return;
        running = false;
        try {
            thread.join();
        } catch (Exception e){
            e.printStackTrace();
            System.exit(0);

        }
    }

    public void run(){
        while(running){
            tick();
            render();
        }
    }

    public void tick(){}

    public void render(){
        BufferStrategy bs = getBufferStrategy();
        if(bs == null){
            createBufferStrategy(3);
            return;
        }
        screen.render();
        for (int i = 0; i < WIDTH * HEIGHT; i++){
            pixels[i] = screen.pixels[i];
        }
        Graphics g = bs.getDrawGraphics();
        g.drawImage(img, WIDTH, HEIGHT, null);
        g.dispose();
        bs.show();
    }

    public static void main(String []args){
        Display game = new Display();
        JFrame frame = new JFrame();
        frame.add(game);
        frame.pack();
        frame.setTitle(TITLE);
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);

        game.start();
    }
}

屏幕:

package com.mads.minecraft.graphics;

import java.util.Random;

public class Screen extends Render{
    private Render test;

    public Screen(int width, int height) {
        super(width, height);
        Random random = new Random();
        test = new Render(256, 256);
        for(int i = 0; i < 256 * 256; i++) {
            test.pixels[i] = random.nextInt();
        }
    }

    public void render(){
        draw(test, 0, 0);
    }
}

最佳答案

以下是无效的语句,您需要替换:

int Pixels = new int(width * height);(int是原始数据类型而不是引用类型,因此不能创建对象)

像素 = new int[宽度 * 高度]; (您可以创建一个int数组。您已经在实例级别将变量pixels声明为int[],因此无需再次声明)

希望这会有所帮助。

关于java - java eclipse 错误的 Minecraft 克隆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37624505/

相关文章:

java - 我想从 Eclipse 连接到 SSMS 数据库,但它抛出 SQLserverException

java - 在 Java 中使用 for 循环,是否必须指定 not null?

内部类中的 Java KeyListener 由于某种原因未获取事件

java - 如何在多项目项目中拥有两个不同的 Gradle 版本?

java - 获取一个新字符串,除非某个值等于 false java

java - 在 bukkit 插件中找不到主类

java - 让苹果给你体验? | Minecraft Forge 模组 1.7.10

java - 使用 Jersey 默认实现 : MOXy 反序列化多态类型

java - 如何更改列表的值并在 hibernate 中更新列表?

java - Bukkit 从库存中移除元素