java rpg游戏代码(移动保存读取)
package ggg;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Tank1 extends JFrame
{
//用来存储对应的图片的二维数组 (这里的icon数组,只是用来将我们设置好的数组在界面上显示出来,不用保存进文件)
MyJPanel mp=null;
public static void main(String[] args)
{
Tank1 t1=new Tank1();
// t1.BuildWorld();
}
public Tank1 ()
{
mp=new MyJPanel();
this.add(mp);
String v="";//
System.out.println();
Scanner ini=new Scanner(System.in);
v= ini.next();
if(v.equals("x"))
{
System.out.print("载入");
mp.BuildWorld();
}
if(v.equals("d"))
{
mp.BuildWorld();
mp.duqu();
System.out.print("读取");
}
Thread t=new Thread(mp);//启动面板线程
t.start();
this.addKeyListener(mp);
this.setSize(1600,1000);
this.setLocation(1,1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class MyJPanel extends JPanel implements KeyListener,Runnable// Perception感知主界面
{
ImageIcon lawn = new ImageIcon("000草地.png");
ImageIcon person = new ImageIcon("ren.png");
ImageIcon green_trees = new ImageIcon("101绿树.png");
ImageIcon [][] plane = new ImageIcon[100][100];
int [][]map=new int [100][100];
int x=8;
int y=8;
MyJPanel()
{
}
private void baocun()//游戏保存
{
// TODO 自动生成的方法存根
File f=null;
FileOutputStream fx=null;
//DataOutputStream fx=null;
try
{
f=new File("D:\\map999999999999.txt");
fx=new FileOutputStream(f);
//fx=new DataOutputStream (f);
for(int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
if(mp.plane[i][j]==mp.person)
{
mp.map[i][j]=0;
//System.out.print(p.map[i][j]);
}
if(mp.plane[i][j]==mp.green_trees)
{
mp.map[i][j]=1;
System.out.print(mp.map[i][j]+" ");
}
fx.write(mp.map[i][j]);
//强制流中的数据完全输出完
}
}
System.out.print("保存X Y"+this.x+" "+this.y);
fx.write(this.x);
fx.write(this.y);
fx.flush();
//关闭输出流
fx.close();
}
catch(Exception e)
{
}
}
public void duqu()游戏读取
{
FileInputStream in=null;
DataInputStream inn=null;
try
{
in=new FileInputStream("D:\\map999999999999.txt");
inn=new DataInputStream (in);
for(int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
mp.map[i][j]=inn.read();
if(map[i][j]==0)
{
mp.plane[i][j]=this.lawn;
}
if(map[i][j]==1)
{
mp.plane[i][j]=mp.green_trees;
}
//强制流中的数据完全输出完
}
}
mp.x=inn.read();
mp.y=inn.read();
System.out.print("读取"+this.x+" "+this.y);
//关闭输出流
inn.close();
}
catch(Exception e)
{
}
}
public void keyTyped(KeyEvent e){}//键盘监听
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_S)
{
this.xia();
System.out.print("xxxxx"+this.x+this.y+" ");
}
if(e.getKeyCode()==KeyEvent.VK_W)
{
this.shang();
System.out.print("xxxxx"+this.x+this.y+" ");
}
if(e.getKeyCode()==KeyEvent.VK_A)
{
mp.zuo();
System.out.print("xxxxx"+this.x+this.y+" ");
}
if(e.getKeyCode()==KeyEvent.VK_D)
{
mp.you();
System.out.print("xxxxx"+this.x+this.y+" ");
}
if(e.getKeyCode()==KeyEvent.VK_B)
{
mp.baocun();
System.out.print("保存");
}
}
public void run()
{
while(true)
{
try {
Thread.sleep(100);}
catch (Exception e) {}
this.repaint();
}
}
public void zuo()
{
this.x=this.x-1;
}
public void you()
{
this.x=this.x+1;
}
public void xia()
{
this.y=this.y+1;
}
public void shang()
{
this.y=this.y-1;
}
public void paint(Graphics g)
{ super.paint(g);
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
g.drawImage(this.plane[i+this.x][j+y].getImage(), i*55, j*55, 50,50, null);
//g.drawImage(icon1.getImage(), this.x,this.y , 50,50, null);
g.drawImage(this.person.getImage(),this.x*55 ,this.y*55, 50,50, null);
}
}
}
public void BuildWorld()//生成地图的方法
{
for(int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
plane[i][j]=lawn;
}
}
this.plane[5][5]=green_trees;
this.plane[6][6]=green_trees;
this.plane[8][8]=green_trees;
}
}
}