我正在制作一个2D游戏并且我在地图上停留。我希望它存储在像这样的文本文件中
[block name] [x] [y]
wall 20 20
wall 20 40
wall 20 60
wall 20 80
wall 20 100
wall 20 120
wall 20 140
wall 20 160
wall 20 180
wall 20 200
我有一个读取该代码的代码
public void TileLayer() throws IOException{
InputStream inp = (InputStream) getClass().getResourceAsStream("/image/msdf.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(inp));
String str;
while ((str = in.readLine()) != null) {
if (str.startsWith("wall ")) {
float x = Float.valueOf(str.split(" ")[1]);
float y = Float.valueOf(str.split(" ")[2]);
}
}
}
但比如何展示呢?如何缓冲它?
感谢您的帮助。 如果您有任何问题,我会问,我会回答。
完整代码
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.Timer;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import org.omg.CORBA.portable.InputStream;
import java.io.Reader;
public class NewTest extends JFrame implements KeyListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private Image TestImage;
private BufferedImage bf;
private int cordX = 100;
private int cordY = 100;
private boolean down, up, left, right;
public NewTest() throws IOException {
setTitle("Testing....");
setSize(200, 200);
imageLoader();
setVisible(true);
}
public void imageLoader() throws IOException {
TestImage = Toolkit.getDefaultToolkit().getImage("image/Koala.jpg");
addKeyListener(this);
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
bf = new BufferedImage( this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_RGB);
try{
animation(bf.getGraphics());
g.drawImage(bf,0,0,null);
}catch(Exception ex){
}
}
public void animation(Graphics g) {
super.paint(g);
g.drawImage(TestImage, cordX, cordY,20 , 20, this);
}
public void TileLayer() throws IOException{
InputStream inp = (InputStream) getClass().getResourceAsStream("/image/msdf.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(inp));
String str;
while ((str = in.readLine()) != null) {
if (str.startsWith("wall ")) {
float x = Float.valueOf(str.split(" ")[1]);
float y = Float.valueOf(str.split(" ")[2]);
}
}
}
public static void main(String[] args) {
try {
new NewTest();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_RIGHT:
right = true;
break;
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_DOWN:
down = true;
break;
case KeyEvent.VK_UP:
up = true;
break;
}
updateState();
}
public void keyReleased(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_DOWN:
down = false;
break;
case KeyEvent.VK_UP:
up = false;
break;
}
updateState();
}
protected void updateState() {
if (right) {
cordX += 5;
} else if (left) {
cordX -= 5;
}
if (down) {
cordY += 5;
} else if (up) {
cordY -= 3;
}
if (down && up) {
cordY += 0;
cordX += 0;
}
if (right && left) {
cordX += 0;
cordY += 0;
}
System.out.printf("X:");System.out.println(cordX);
System.out.printf("Y:");System.out.println(cordY);
repaint();
}
public void keyTyped(KeyEvent ke) {
}
}