大二的时候无聊写的吧,上下左右,右面的两个按键是可以跳过一面墙的。小时候特别喜欢这个游戏,可惜没时间只能大概做一下了。那时候代码写的还不是很规范了。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Container;
class beijing extends JFrame implements ActionListener
{
private ImageIcon kaishi=new ImageIcon("picture/kaishi.png");
JButton kaishib;
beijing()
{
setLayout(null);
kaishib=new JButton(kaishi);
add(kaishib);
kaishib.setBounds(170,200,200,80);
ImageIcon beijing= new ImageIcon("picture/beijing.png");//这是背景图片
JLabel beijingb=new JLabel(beijing);
kaishib.addActionListener(this);
add(beijingb);
beijingb.setBounds(0,0,574,387);
beijingb.setOpaque(false);
setBounds(300,100,549,405);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==kaishib)
{
this.dispose();
picture a1=new picture("死神");
}
}
}
class picture extends JFrame implements Runnable,ActionListener
{
int i,j,kq=0,kup=0,kdi=0,kmenf=0;
int xiancheng=1,jump_fangxiang=0,jishii=1,kaimenzhi=1;//为了避免线程重复start的数值
Thread jump,yanshi,jishi,kaimen;
String cishus="剩余次数:",shijians="剩余时间:";
int cishui=10,shijiani=30;
int kmen=0,menshui=0;
public static int hp=100,fang=10,gong=20,yaoshi=1,gold=10;
public static int[][] ditu={{2,2,2,2,1,2,1,2,2,1,2,2,1},
{2,1,1,2,2,2,1,2,1,2,1,2,2},
{2,1,2,1,1,1,4,1,1,2,1,1,2},
{2,1,2,2,2,2,1,2,2,2,1,2,1},
{1,1,1,1,1,1,2,2,1,2,2,2,2},
{1,2,2,2,2,1,2,2,1,2,1,2,1},
{1,2,1,2,2,2,1,2,1,1,2,2,1},
{1,2,1,2,1,2,2,2,1,2,2,1,1},
{2,1,2,1,1,2,1,2,1,2,1,2,1},
{1,1,1,2,1,2,2,1,2,1,2,1,1},
{2,2,1,2,2,2,2,2,2,1,2,1,2},
{1,2,2,2,1,2,1,1,1,1,2,2,1},
{1,1,1,2,2,1,3,1,2,1,1,1,1}};
public static int[][] ditu1={{2,2,2,2,1,2,1,2,2,1,2,2,1},
{2,1,1,2,2,2,1,2,1,2,1,2,2},
{2,1,2,1,1,1,4,1,1,2,1,1,2},
{2,1,2,2,2,2,1,2,2,2,1,2,1},
{1,1,1,1,1,1,2,2,1,2,2,2,2},
{1,2,2,2,2,1,2,2,1,2,1,2,1},
{1,2,1,2,2,2,1,2,1,1,2,2,1},
{1,2,1,2,1,2,2,2,1,2,2,1,1},
{2,1,2,1,1,2,1,2,1,2,1,2,1},
{1,1,1,2,1,2,2,1,2,1,2,1,1},
{2,2,1,2,2,2,2,2,2,1,2,1,2},
{1,2,2,2,1,2,1,1,1,1,2,2,1},
{1,1,1,2,2,1,3,1,2,1,1,1,1}};
private ImageIcon xiaowu=new ImageIcon("picture/xiaowu.png");
private ImageIcon shang=new ImageIcon("picture/shang.png");
private ImageIcon xia=new ImageIcon("picture/xia.png");
private ImageIcon zuo=new ImageIcon("picture/zuo.png");
private ImageIcon you=new ImageIcon("picture/you.png");
private ImageIcon qiang=new ImageIcon("picture/qiang.png");
private ImageIcon ying=new ImageIcon("picture/ying.png");
private ImageIcon shu=new ImageIcon("picture/shu.png");
private ImageIcon upstairs=new ImageIcon("picture/upstairs.png");
private ImageIcon di=new ImageIcon("picture/di.png");
private ImageIcon men=new ImageIcon("picture/men.png");
JLabel xiaowub;
JButton shangb;
JButton xiab;
JButton zuob;
JButton youb;
JButton jump1;
JButton jump2;
JLabel[] qiangb=new JLabel[100];
JLabel[] upstairsl=new JLabel[100];
JLabel cishu=new JLabel(cishus+cishui);
JLabel shijian=new JLabel(shijians+shijiani);
JLabel yingl=new JLabel(ying);
JLabel shul=new JLabel(shu);
JLabel[] dil=new JLabel[100];
JLabel[] menl=new JLabel[100];
int menshu(int x,int y)
{
int kmena=0;
for(int i=0;i<13;i++)
{
for(int j=0;j<13;j++)
{
if(ditu1[i][j]==4)
kmena++;
if((j*41==x)&&(i*41)==y)
break;
}
if((i*41==x)&&(j*41)==y)
break;
}
return(kmena);
}
int qingkuang(int x,int y)
{
int heng,lie;
heng=x/41;
lie=y/41;
if((lie<0)||(lie>13))
{
return(2);
}
if(ditu[heng][lie]==2)
return(2);
if(ditu[heng][lie]==3)
return(3);
if(ditu[heng][lie]==1)
return(1);
if(ditu[heng][lie]==4)
return(4);
return(0);
}
void xianshi()
{
for(i=0;i<13;i++)
{
for(j=0;j<13;j++)
{
if(ditu[i][j]==2)
{
kq++;
qiangb[kq]=new JLabel(qiang);
add(qiangb[kq]);
qiangb[kq].setBounds(41*j,41*i,41,41);
}
if(ditu[i][j]==3)
{
kup++;
upstairsl[kup]=new JLabel(upstairs);
add(upstairsl[kup]);
upstairsl[kup].setBounds(41*j,41*i,41,41);
}
if(ditu[i][j]==1)
{
kdi++;
dil[kdi]=new JLabel(di);
add(dil[kdi]);
dil[kdi].setBounds(41*j,41*i,41,41);
}
if(ditu[i][j]==4)
{
kmenf++;
menl[kmenf]=new JLabel(men);
add(menl[kmenf]);
menl[kmenf].setBounds(41*j,41*i,41,41);
}
}
}
}
picture(String s)
{
super();
setLayout(null);
jump=new Thread(this);
yanshi=new Thread(this);
jishi=new Thread(this);
kaimen=new Thread(this);
add(yingl);
add(shul);
xiaowub=new JLabel(xiaowu);
shangb=new JButton(shang);
xiab=new JButton(xia);
zuob=new JButton(zuo);
youb=new JButton(you);
jump1=new JButton(you);
jump2=new JButton(zuo);
shangb.addActionListener(this);
xiab.addActionListener(this);
zuob.addActionListener(this);
youb.addActionListener(this);
jump1.addActionListener(this);
jump2.addActionListener(this);
add(xiaowub);
add(shangb);
add(xiab);
add(zuob);
add(youb);
add(jump1);
add(jump2);
add(cishu);
add(shijian);
xianshi();
yingl.setBounds(180,205,170,70);
yingl.setVisible(false);
shul.setBounds(180,205,170,70);
shul.setVisible(false);
xiaowub.setBounds(246,0,41,41);
shangb.setBounds(246,533,41,41);
xiab.setBounds(246,574,41,41);
zuob.setBounds(205,574,41,41);
youb.setBounds(287,574,41,41);
jump1.setBounds(451,574,41,41);
jump2.setBounds(410,574,41,41);
cishu.setBounds(5,574,100,15);
shijian.setBounds(5,590,100,15);
setBounds(300,0,549,700);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
jishii=jishii+1;
if(jishii==2)
{
jishi.start();
}
Rectangle rect=xiaowub.getBounds();
int x=(int)rect.getX();
int y=(int)rect.getY();
int qingkuangzhi=0;
qingkuangzhi=qingkuang(y,x);
//if(qingkuangzhi==1)
//{
//dijige=dijige+1;
//System.out.println("qingkuangzhi="+dijige+" "+qingkuangzhi);
//}
//if(qingkuangzhi==4)
//{
//System.out.println("qingkuangzhi="+qingkuangzhi);
//}
if(e.getSource()==youb)
{
x=x+41;
if(x>492)
x=492;
qingkuangzhi=qingkuang(y,x);
if(qingkuangzhi==2)
x=x-41;
if(qingkuangzhi==4)
{
menshui=menshu(x,y);
x=x-41;
}
xiaowub.setLocation(x,y);
}
if(e.getSource()==zuob)
{
x=x-41;
if(x<0)
x=0;
qingkuangzhi=qingkuang(y,x);
if(qingkuangzhi==2)
x=x+41;
if(qingkuangzhi==4)
{
menshui=menshu(x,y);
x=x+41;
}
xiaowub.setLocation(x,y);
}
if(e.getSource()==shangb)
{
y=y-41;
if(y<0)
y=0;
qingkuangzhi=qingkuang(y,x);
if(qingkuangzhi==2)
y=y+41;
if(qingkuangzhi==4)
{
menshui=menshu(x,y);
y=y+41;
}
xiaowub.setLocation(x,y);
}
if(e.getSource()==xiab)
{
y=y+41;
if(y>492)
y=492;
qingkuangzhi=qingkuang(y,x);
if(qingkuangzhi==2)
y=y-41;
if(qingkuangzhi==4)
{
menshui=menshu(x,y);
y=y-41;
}
xiaowub.setLocation(x,y);
}
if(e.getSource()==jump1)
{
jump_fangxiang=1;
xiancheng=xiancheng+1;
if(xiancheng==2)
{
jump.start();
}
else
huifu();
}
if(e.getSource()==jump2)
{
jump_fangxiang=2;
xiancheng=xiancheng+1;
if(xiancheng==2)
{
jump.start();
}
else
huifu();
}
if(qingkuangzhi==3)
{
yingl.setVisible(true);
yanshi.start();
}
if(qingkuangzhi==4)
{
System.out.println("qingkuangzhi="+qingkuangzhi);
if(kaimenzhi==1)
{
kaimen.start();
}
else
{
huifu();
System.out.println("huifu");
}
kaimenzhi=2;
}
}
public void run()
{
if(Thread.currentThread()==kaimen)
{
while(true)
{
System.out.println("kmena="+(menshui+kmen));
Rectangle rect=menl[menshui].getBounds();
int x=(int)rect.getX();
int y=(int)rect.getY();
int kmena=0;
kmena=menshui;
try//原来的门变成了地,所以少了一个,把原来的门隐藏,新建一个门,每少一个门,那个值就加一。
{
/*for(int i=0;i<13;i++)
{
for(int j=0;j<13;j++)
{
if(ditu[i][j]==4)
kmena++;
if((j*41==x)&&(i*41)==y)
break;
}
if((i*41==x)&&(j*41)==y)
break;
}*/
int x1=x+41;
ditu[y/41][x/41]=2;
kdi++;
dil[kdi]=new JLabel(di);
add(dil[kdi]);
dil[kdi].setBounds(x,y,41,41);
for(;x!=x1;)
{
x=x+1;
kaimen.sleep(20);
menl[kmena].setLocation(x,y);
}
ditu[y/41][(x-41)/41]=1;
kaimen.sleep(800);
for(int i=0;i<13;i++)
{
for(int j=0;j<13;j++)
{
System.out.print(ditu[i][j]+",");
}
System.out.println(" ");
}
menl[kmena].setVisible(false);
if(x==x1)
guaqi();
}
catch (InterruptedException e)
{
}
}
}
if(Thread.currentThread()==jishi)
{
while(true)
{
Rectangle rect=xiaowub.getBounds();
int x=(int)rect.getX();
int y=(int)rect.getY();
int qingkuangzhi=0;
qingkuangzhi=qingkuang(y,x);
try
{
jishi.sleep(1000);
shijiani=shijiani-1;
shijian.setText(shijians+shijiani);
if(shijiani==0)
{
shul.setVisible(true);
jishi.sleep(2000);
this.dispose();
beijing a1=new beijing();
guaqi();
}
if(qingkuangzhi==3)
{
guaqi();
}
}
catch (InterruptedException e)
{
}
}
}
if(Thread.currentThread()==yanshi)
{
while(true)
{
try
{
yanshi.sleep(2000);
this.dispose();
beijing a1=new beijing();
}
catch (InterruptedException e)
{
}
guaqi();
}
}
if(Thread.currentThread()==jump){
while(true)
{
double t=0;
int jiaocuox=0,jiaocuoy=0,qingkuangzhi=0;
Rectangle rect=xiaowub.getBounds();
int x=(int)rect.getX();
int y=(int)rect.getY();
qingkuangzhi=qingkuang(x,y);
if(jump_fangxiang==1)//l
{
qingkuangzhi=qingkuang(y,x+82);
if(qingkuangzhi==2)
{
}
else
try
{
double y2=0;
int x1=-41,y1=0;
for(t=0.0;t<=82;t=t+1)
{
jump.sleep(5);
x=x+1;
x1=x1+1;
y2=x1*x1*2/41+y-85;
y1=(int)y2;
xiaowub.setLocation(x,y1);
}
}
catch (InterruptedException e)
{
System.out.println("zhuchengxu");
}
}
if(jump_fangxiang==2)//r
{
qingkuangzhi=qingkuang(y,x-82);
if(qingkuangzhi==2)
{
}
else
try
{
double y2=0;
int x1=-41,y1=0;
for(t=0.0;t<=82;t=t+1)
{
jump.sleep(5);
x=x-1;
x1=x1+1;
y2=x1*x1*2/41+y-85;
y1=(int)y2;
xiaowub.setLocation(x,y1);
}
}
catch (InterruptedException e)
{
System.out.println("zhuchengxu");
}
}
jiaocuox=x%41;
jiaocuoy=y%41;
if(jiaocuox<=21)
{
x=x-jiaocuox;
}
if(jiaocuox>=21)
{
x=x-jiaocuox+41;
}
if(jiaocuoy<=21)
{
y=y-jiaocuoy;
}
if(jiaocuoy>=21)
{
y=y-jiaocuox+41;
}
xiaowub.setLocation(x,y);
try
{
jump.sleep(1);
cishui=cishui-1;
cishu.setText(cishus+cishui);
if(cishui==0)
{
shul.setVisible(true);
jump.sleep(2000);
this.dispose();
beijing a1=new beijing();
guaqi();
}
}
catch (InterruptedException e)
{
}
guaqi();
}}
}
public synchronized void guaqi()// throws InterruptedException
{
try
{
wait();
}
catch (InterruptedException e3)
{
System.out.println("guqi");
}
}
public synchronized void huifu()
{
notifyAll();
}
}
public class xinjian
{
public static void main(String arsg[])
{
beijing a1=new beijing();
}
}