地图未正确加载
温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
Hi I am a student learning Java and I am trying to make my first ever java game but the problem is that my maps are not loading right I made a map loading system that reads the map which is a txt file in 0 (grass), 1(asphalt), 2(sand) these get rode and putted into the tiles array which tile[1] will spawn asphalt and I have 16 maps but my code does not read it so the map does not open and I want to switch maps when player location is either at the right bottom or the right side of the screen. Please Help!
I tried to change map file positions, called it differently and tried debugging it but none worked
package main;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import entity.Player;
import tile.Tile;
public class MapLoader {
GamePanel gp;
Tile[] tile;
int mapTileNum[][] = new int[18][18];
//GamePanel gamePanel = new GamePanel();
//MapLoader mapLoader = new MapLoader(gamePanel);
private List<String> mapPaths;
private int currentMapIndex;
public MapLoader(GamePanel gp) {
this.gp = gp;
// Initialize the list and add map file paths in the desired order
mapPaths = new ArrayList<>();
mapPaths.add("/maps/map01.txt");
mapPaths.add("/maps/map02.txt");
mapPaths.add("/maps/map03.txt");
mapPaths.add("/maps/map04.txt");
mapPaths.add("/maps/map05.txt");
mapPaths.add("/maps/map06.txt");
mapPaths.add("/maps/map07.txt");
mapPaths.add("/maps/map08.txt");
mapPaths.add("/maps/map09.txt");
mapPaths.add("/maps/map10.txt");
mapPaths.add("/maps/map11.txt");
mapPaths.add("/maps/map12.txt");
mapPaths.add("/maps/map13.txt");
mapPaths.add("/maps/map14.txt");
mapPaths.add("/maps/map15.txt");
mapPaths.add("/maps/map16.txt");
setCurrentMapIndex(0);
}
public void switchToNextMap() {
if (currentMapIndex < mapPaths.size() - 1) {
currentMapIndex++;
System.out.println("Not Working no new map");
loadMap(mapPaths.get(currentMapIndex));
}
}
public void switchToPreviousMap() {
if (currentMapIndex > 0) {
currentMapIndex--;
System.out.println("Not Working no old map");
loadMap(mapPaths.get(currentMapIndex));
}
}
public void loadMaps() {
// Iterate through the list of map file paths and load each map
for (String mapPath : mapPaths) {
loadMap(mapPath);
}
}
public void loadMap(String filePath) {
try {
InputStream is = getClass().getResourceAsStream(filePath);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
int col = 0;
int row = 0;
while(col < gp.maxScreenCol && row < gp.maxScreenRow) {
String line = br.readLine();
while(col < gp.maxScreenCol) {
String numbers[] = line.split(" ");
int num = Integer.parseInt(numbers[col]);
mapTileNum[col][row] = num;
col++;
}
if(col == gp.maxScreenCol) {
col = 0;
row++;
}
}
br.close();
}catch(Exception e) {
e.printStackTrace();
//System.out.println(e);
}
System.out.println("Loading map from file: " + filePath);
}
public void switchMap(GamePanel gp, Player player2, String filePathc) {
loadMap(filePathc); // Load the new map
}
public void checkMap(GamePanel gamePanel, Player player, String previousMapFilePath, String nextMapFilePath, String upperMapFilePath) {
int playerX = player.getX();
int playerY = player.getY();
int tileSize = gamePanel.tileSize; // Accessing tileSize from GamePanel instance
if (playerX <= 0) {
// Player reached left edge of map, switch to the previous map
switchToPreviousMap();
// Adjust player position if needed
player.setX(gamePanel.screenWidth - tileSize);
} else if (playerX >= gamePanel.screenWidth - tileSize) {
// Player reached right edge of map, switch to the next map
switchToNextMap();
// Adjust player position if needed
player.setX(0);
} else if (playerY <= 0) {
// Player reached top edge of map, switch to the upper map
switchMap(gamePanel, player, upperMapFilePath); //maploader
// Adjust player position if needed
player.setY(gamePanel.screenHeight - tileSize);
}
}
public int getCurrentMapIndex() {
return currentMapIndex;
}
public void setCurrentMapIndex(int currentMapIndex) {
this.currentMapIndex = currentMapIndex;
}
public static void main(String[] args) {
GamePanel gamePanel = new GamePanel();
MapLoader mapLoader = new MapLoader(gamePanel);
mapLoader.loadMaps();
}
}
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import entity.Player;
import tile.TileManager;
import main.MapLoader;
public class GamePanel extends JPanel implements Runnable {
// Screen Settings
final int originalTileSize = 16; // 16x16 cars
final int scale = 3; // Size Scale 32*3
public final int tileSize = originalTileSize * scale; // 48x48 cars
public final int maxScreenCol = 18;
public final int maxScreenRow = 18;
public final int screenWidth = tileSize * maxScreenCol; // 20 height 960 pixels
public final int screenHeight = tileSize * maxScreenRow; // 16 width 768 pixels
Font fpsFont = new Font("Arial", Font.PLAIN, 12);
//FPS
int FPS = 70;
TileManager tileM = new TileManager(this);
KeyHandler keyH = new KeyHandler();
Thread gameThread; // Start and Stop Repeating
Player player = new Player(this,keyH);
MapLoader mapLoader = new MapLoader(this);
GamePanel gp;
public GamePanel() {
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
this.setBackground(Color.gray); //Color.Black put it gray for testing
this.setDoubleBuffered(true);
this.addKeyListener(keyH);
this.setFocusable(true);
mapLoader.loadMap("/maps/map01.txt");
//dashboard = new Dashboard(this, keyH);
}
public void startGameThread() {
gameThread = new Thread(this); // this GamePanel
gameThread.start();
}
@Override
public void run() {
mapLoader.loadMaps();
double drawInterval = 1000000000/FPS;
double delta = 0;
long lastTime = System.nanoTime();
long currentTime;
long timer = 0;
int drawCount = 0;
while(gameThread != null) {
currentTime = System.nanoTime();
delta += (currentTime - lastTime) / drawInterval;
timer += (currentTime - lastTime);
lastTime = currentTime;
if(delta >=1) {
update();
repaint();
delta--;
drawCount++;
}
if(timer >= 1000000000) {
System.out.println("FPS: " + drawCount);
drawCount = 0;
timer = 0;
}
}
}
public void update() {
player.update();
int playerX = player.getX();
int playerY = player.getY();
int tileSize = this.tileSize;
// Check if the player is at the right edge of the screen
if (playerX >= screenWidth - tileSize) {
mapLoader.switchToNextMap();
// Adjust player position if needed
player.setX(0);
}
// Check if the player is at the bottom edge of the screen
else if (playerY >= screenHeight - tileSize) {
mapLoader.switchToNextMap();
// Adjust player position if needed
player.setY(0);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
tileM.draw(g2);
player.draw(g2);
player.draw(g2);
g2.dispose(); // to save ram
}
public static void main(String[] args) {
GamePanel gamePanel = new GamePanel();
JFrame frame = new JFrame("F1 Monaco");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(gamePanel);
frame.pack();
frame.setLocationRelativeTo(null); // Center the frame on the screen
frame.setVisible(true);
MapLoader mapLoader = new MapLoader(gamePanel);
mapLoader.loadMaps();
}
}
正文到此结束
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2361
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权