Pelaksanaan tuangan sinar di Java untuk "maze tingkap" yang membenarkan saiz ketinggian yang berbeza.
import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; import javax.swing.*; public class RayCastingWithDifferentHeightSize extends JPanel implements MouseListener, MouseMotionListener, MouseWheelListener { private static final int SCREEN_WIDTH = 800; private static final int SCREEN_HEIGHT = 600; private BufferedImage screen; private int[][] map; private List<Wall> walls; private Player player; private double fov = 90 * Math.PI / 180; private double viewDistance = 300; private double mouseAngle; private boolean[] keys; public RayCastingWithDifferentHeightSize() { setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT)); screen = new BufferedImage(SCREEN_WIDTH, SCREEN_HEIGHT, BufferedImage.TYPE_INT_RGB); map = new int[][]{ {1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 3, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; walls = new ArrayList<>(); for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { if (map[i][j] == 1) { walls.add(new Wall(i * 50, j * 50, 50, 50)); // 50 is the wall size } } } player = new Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 0); keys = new boolean[10]; addMouseListener(this); addMouseMotionListener(this); addMouseWheelListener(this); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; // Clear the screen g2.setColor(Color.BLACK); g2.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); // Draw the walls for (Wall wall : walls) { g2.setColor(Color.WHITE); g2.fillRect(wall.getX(), wall.getY(), wall.getWidth(), wall.getHeight()); // Draw the different heights on the walls int height = wall.getHeight(); for (int i = 0; i < height; i++) { if (height - i < 15) { g2.setColor(Color.RED); } else if (height - i < 30) { g2.setColor(Color.YELLOW); } else {
Atas ialah kandungan terperinci Bagaimanakah saya boleh mencipta "maze tingkap" menggunakan Ray Casting di Java yang membenarkan saiz ketinggian yang berbeza untuk dinding?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!