Guest
Gibt es eine Möglichkeit, meine Tilemap einmal statt jedes Bilds zu zeichnen?
Post
by Guest » 31 Dec 2024, 13:36
Mein Spiel läuft mit Sekunden pro Frame, wenn ich meine Tilemap auf das Gamepanel zeichne, da es dies bei jedem Frame tut und einfach verstopft. Ich werde etwas Code der Kachel-, Karten- und Panel-Klassen hinzufügen. Hoffentlich kann jemand helfen.
Ich würde mir eine Möglichkeit wünschen, die Karte zu erstellen und einmal anzuzeigen, während alles andere darüber aktualisiert wird. Ich bin Java-Neuling und komme aus dem Schreiben von Spielen in Pygame (was seltsam ist, da ich dachte, dass ich aufgrund von GIL mehr solche Probleme mit Python haben würde)
Zuerst die GamePanel-Klasse.< /p>
Code: Select all
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import entity.Player;
import tile.TileMap;
public class GamePanel extends JPanel implements Runnable {
// Screen Settings
public final int tileSize = 25;
public final int maxScreenColumn = 26;
public final int maxScreenRow = 18;
final int screenWidth = tileSize * maxScreenColumn;
final int screenHeight = tileSize * maxScreenRow;
final int FPS = 60;
Thread gameThread;
KeyHandler keyH = new KeyHandler();
public int playerX = tileSize * 13;
public int playerY = tileSize * 10;
public int playerSpeed = 3;
TileMap tileM = new TileMap(this);
Player player = new Player(this, keyH);
// set player's default pos
public GamePanel() {
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
this.setBackground(Color.black);
this.setDoubleBuffered(true);
this.addKeyListener(keyH);
this.setFocusable(true);
}
public void startGameThread() {
gameThread = new Thread(this);
gameThread.start();
}
public void run() {
double drawInterval = 1000000000/FPS;
double nextDrawTime = System.nanoTime() + drawInterval;
while (gameThread != null) {
// 1. Update info such as character pos etc...
update();
// 2. Draw to the gamePanel object...
repaint();
try {
double remainingTime = nextDrawTime - System.nanoTime();
if (remainingTime < 0) {
remainingTime = 0;
}
Thread.sleep((long)remainingTime / 1000000);
nextDrawTime += drawInterval;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void update() {
player.update();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
tileM.drawTileMap(g2);
// tile.draw(g2);
player.draw(g2);
g2.dispose();
}
}
Als nächstes kommt die TileMap-Klasse
Code: Select all
package tile;
import main.GamePanel;
import java.awt.Graphics2D;
public class TileMap {
GamePanel gp;
public String tileMap[][] =
{
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" },
{"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }
};
public TileMap(GamePanel gp) {
this.gp = gp;
}
public void drawTileMap (Graphics2D g2) {
for (int col = 0; col < gp.maxScreenColumn-1; col++) {
for (int row = 0; row < gp.maxScreenRow-1; row++) {
if (tileMap[row][col].equalsIgnoreCase("g")) {
Tile tile = new Tile(gp, col*gp.tileSize, row*gp.tileSize, "/res/tiles/grass1.png");
tile.draw(g2);
}
}
}
}
}
Endlich die Tile-Klasse.
Code: Select all
package tile;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import main.GamePanel;
public class Tile {
private BufferedImage image;
public boolean collision = false;
GamePanel gp;
int x;
int y;
public String path;
public Tile (GamePanel gp, int x, int y, String path) {
this.gp = gp;
this.x = x;
this.y = y;
this.setImage(path);
}
public BufferedImage getImage () {
return image;
}
public void setImage (String path) {
try {
this.image = ImageIO.read(getClass().getResourceAsStream(path));
// this.image = ImageIO.read(new FileInputStream(path));
} catch (IOException e) {
e.printStackTrace();
System.out.println(path);
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
System.out.println(path);
}
}
public void draw (Graphics2D g2) {
g2.drawImage(image, x, y, gp.tileSize, gp.tileSize, null);
}
}
1735648587
Guest
Mein Spiel läuft mit Sekunden pro Frame, wenn ich meine Tilemap auf das Gamepanel zeichne, da es dies bei jedem Frame tut und einfach verstopft. Ich werde etwas Code der Kachel-, Karten- und Panel-Klassen hinzufügen. Hoffentlich kann jemand helfen. Ich würde mir eine Möglichkeit wünschen, die Karte zu erstellen und einmal anzuzeigen, während alles andere darüber aktualisiert wird. Ich bin Java-Neuling und komme aus dem Schreiben von Spielen in Pygame (was seltsam ist, da ich dachte, dass ich aufgrund von GIL mehr solche Probleme mit Python haben würde) Zuerst die GamePanel-Klasse.< /p> [code]package main; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JPanel; import entity.Player; import tile.TileMap; public class GamePanel extends JPanel implements Runnable { // Screen Settings public final int tileSize = 25; public final int maxScreenColumn = 26; public final int maxScreenRow = 18; final int screenWidth = tileSize * maxScreenColumn; final int screenHeight = tileSize * maxScreenRow; final int FPS = 60; Thread gameThread; KeyHandler keyH = new KeyHandler(); public int playerX = tileSize * 13; public int playerY = tileSize * 10; public int playerSpeed = 3; TileMap tileM = new TileMap(this); Player player = new Player(this, keyH); // set player's default pos public GamePanel() { this.setPreferredSize(new Dimension(screenWidth, screenHeight)); this.setBackground(Color.black); this.setDoubleBuffered(true); this.addKeyListener(keyH); this.setFocusable(true); } public void startGameThread() { gameThread = new Thread(this); gameThread.start(); } public void run() { double drawInterval = 1000000000/FPS; double nextDrawTime = System.nanoTime() + drawInterval; while (gameThread != null) { // 1. Update info such as character pos etc... update(); // 2. Draw to the gamePanel object... repaint(); try { double remainingTime = nextDrawTime - System.nanoTime(); if (remainingTime < 0) { remainingTime = 0; } Thread.sleep((long)remainingTime / 1000000); nextDrawTime += drawInterval; } catch (InterruptedException e) { e.printStackTrace(); } } } public void update() { player.update(); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; tileM.drawTileMap(g2); // tile.draw(g2); player.draw(g2); g2.dispose(); } } [/code] Als nächstes kommt die TileMap-Klasse [code]package tile; import main.GamePanel; import java.awt.Graphics2D; public class TileMap { GamePanel gp; public String tileMap[][] = { {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" }, {"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G" } }; public TileMap(GamePanel gp) { this.gp = gp; } public void drawTileMap (Graphics2D g2) { for (int col = 0; col < gp.maxScreenColumn-1; col++) { for (int row = 0; row < gp.maxScreenRow-1; row++) { if (tileMap[row][col].equalsIgnoreCase("g")) { Tile tile = new Tile(gp, col*gp.tileSize, row*gp.tileSize, "/res/tiles/grass1.png"); tile.draw(g2); } } } } } [/code] Endlich die Tile-Klasse. [code]package tile; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; import main.GamePanel; public class Tile { private BufferedImage image; public boolean collision = false; GamePanel gp; int x; int y; public String path; public Tile (GamePanel gp, int x, int y, String path) { this.gp = gp; this.x = x; this.y = y; this.setImage(path); } public BufferedImage getImage () { return image; } public void setImage (String path) { try { this.image = ImageIO.read(getClass().getResourceAsStream(path)); // this.image = ImageIO.read(new FileInputStream(path)); } catch (IOException e) { e.printStackTrace(); System.out.println(path); } catch (IllegalArgumentException e1) { e1.printStackTrace(); System.out.println(path); } } public void draw (Graphics2D g2) { g2.drawImage(image, x, y, gp.tileSize, gp.tileSize, null); } } [/code]