[java]代码库
package 坦克大战;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class wegame extends JFrame implements ActionListener {
public static void main(String[] args) {
wegame game1=new wegame();
}
mypanel mb=null;
gkpanel gkmb=null;
JMenuBar cd=null;
JMenu cd1,cd2=null;
JMenuItem cdx1,cdx2,cdx3,cdx4=null;
public wegame() {
cd=new JMenuBar();
cd1=new JMenu("游戏(G)");
cd1.setMnemonic('G');
cd2=new JMenu("帮助(H)");
cd2.setMnemonic('H');
cdx1=new JMenuItem("新游戏(N)");
cdx2=new JMenuItem("退出游戏(E)");
cdx3=new JMenuItem("存盘退出(C)");
cdx4=new JMenuItem("继续游戏(S)");
cd.add(cd1);
cd.add(cd2);
cd1.add(cdx1);
cd1.add(cdx2);
cd1.add(cdx3);
cd1.add(cdx4);
cdx1.addActionListener(this);
cdx1.setActionCommand("newGame");
cdx2.addActionListener(this);
cdx2.setActionCommand("exit");
cdx3.addActionListener(this);
cdx3.setActionCommand("saveEixt");
cdx4.addActionListener(this);
cdx4.setActionCommand("goonGame");
gkmb=new gkpanel();
this.setJMenuBar(cd);
this.add(gkmb);
Thread t=new Thread(gkmb);
t.start();
this.setTitle("坦克大战");
this.setSize(600,500);
this.setLocation(270,170);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(e.getActionCommand().equals("newGame")) {
mb=new mypanel("newGame");
this.remove(gkmb );
this.add(mb);
this.addKeyListener(mb);
Thread t=new Thread(mb);
t.start();
this.setVisible(true);
}else if(e.getActionCommand().equals("goonGame")) {
mb=new mypanel("goonGame");
Thread t=new Thread(mb);
t.start();
this.remove(gkmb );
this.add(mb);
this.addKeyListener(mb);
this.setVisible(true);
}else if(e.getActionCommand().equals("saveEixt")) {
jilu jl=new jilu();
jilu.setDtk(mb.dtk);
jl.cunpan();
System.exit(0);
}else if(e.getActionCommand().equals("exit")) {
jilu.bcjl();
System.exit(0);
}
}
}
class gkpanel extends JPanel implements Runnable{
int times=0;
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0,400,300);
if(times%2==0) {
g.setColor(Color.YELLOW );
Font myfont=new Font("华文行楷",Font.BOLD,38);
g.setFont(myfont );
g.drawString("第一关", 140, 140);
}
}
@Override
public void run() {
// TODO 自动生成的方法存根
while(true) {
try{Thread.sleep(500);}
catch(Exception e){
}
times++;
this.repaint();
}
}
}
class mypanel extends JPanel implements KeyListener,Runnable{
mytank mt=null;
// mytank mt2=null;
Vector<ditank> dtk=new Vector<ditank>();
Vector<weizhi> wzjh=new Vector<weizhi>();
Vector <baozha> bzjh=new Vector<baozha>();
int tksl=3;
// Image tp0=null;
// Image tp1=null;
// Image tp2=null;
public mypanel(String ss) {
jilu.dqjl();
mt=new mytank(200,232);
// mt2=new mytank(300,100);
if(ss.equals("newGame")) {
for(int i=0;i<tksl;i++) {
ditank dt =new ditank((i)*181+5,0);
dt.setFangxing(2);
Thread t2=new Thread(dt);
t2.start();
zidan zd=new zidan(dt.x+10,dt.y+30,2);
dt.dzd.add(zd);
Thread t3=new Thread(zd);
t3.start();
dtk.add(dt);
}
}else if(ss.equals("goonGame")){
wzjh=jilu.dupan();
for(int i=0;i<wzjh.size();i++) {
weizhi wz=wzjh.get(i);
ditank dt=new ditank(wz.x,wz.y);
dt.setFangxing(wz.fangxing );
dt.dtkxl(dtk);
Thread t2=new Thread(dt);
t2.start();
zidan zd=new zidan(dt.x+10,dt.y+30,2);
dt.dzd.add(zd);
Thread t3=new Thread(zd);
t3.start();
dtk.add(dt);
}
}
// tp0=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/爆炸0.jpg"));
// tp1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/爆炸.jpg"));
// tp2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/爆炸2.jpg"));
try{}catch(Exception e) {miusc yy=new miusc("./日本ACG - L'Amour et la Liberté (Darwin & DJ Silver remix).mp3 ");
yy.start();}
}
public void tjsj(Graphics g) {
this.drawTank(80, 330, g , 0, 0);
g.setColor(Color.black );
g.drawString(jilu.getMtsl()+"",116,350 );
this.drawTank(150, 330, g, 0, 1);
g.setColor(Color.black);
g.drawString(jilu.getDtsl()+"",186,350);
this.drawTank(450, 86, g, 0, 1);
g.setColor(Color.black );
g.drawString(jilu.getSdjl()+"",486,107);
g.setColor(Color.black);
Font f=new Font("华文彩云",Font.BOLD ,20);
g.setFont(f);
g.drawString("消灭的坦克数量", 410, 40);
}
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0,400,275);
this.tjsj(g);
if(mt.shengming) {
this.drawTank(mt.getX(),mt.getY(),g,mt.fangxing,0);}
for(int i=0;i<dtk.size();i++) {
ditank dt=dtk.get(i);
if(dt.shengming) {
this.drawTank(dt.getX(),dt.getY(), g, dt.fangxing, 1);
}
}
for(int i=0;i<mt.aa.size();i++) {
zidan zd =mt.aa.get(i);
if(zd!=null&&zd.shengming==true){
g.setColor(Color.red);
g.fill3DRect(zd.x,zd.y, 3, 3, false);
}
if(zd.shengming==false) {
mt.aa.remove(zd);
}
}
for(int i=0;i<dtk.size();i++) {
ditank dt=dtk.get(i);
for(int j=0;j<dt.dzd.size();j++) {
zidan zd=dt.dzd.get(j);
if(zd!=null&&zd.shengming ==true) {
g.setColor(Color.white);
g.fill3DRect(zd.x,zd.y, 3, 3, false);
}
if(zd.shengming==false) {
dt.dzd.remove(zd);
}
}
}
this.repaint();
// for(int i=0;i<bzjh.size();i++) {
// baozha bz=bzjh.get(i);
// if(bz.shengcunq>6) {
// g.drawImage(tp1,bz.x,bz.y,30,30,this);
// }else if(bz.shengcunq>3) {
// g.drawImage(tp2,bz.x,bz.y,30,30,this);
// }else {
// g.drawImage(tp0,bz.x,bz.y,30,30,this);
//
// }
// bz.suqsd();
// if(bz.shengcunq==0) {
// bzjh.remove(bz);
// }
// }
}
public void jzwf(zidan zd,mytank mt) {
for(int i=0;i<dtk.size();i++) {
ditank dt=dtk.get(i);
for(int j=0;j<dt.dzd.size();j++) {
zd=dt.dzd.get(j);
if(zd.shengming&&mt.shengming) {
this.jzdf(zd,mt);}
}
}
}
public void jzdf1(zidan zd,ditank dt) {
for(int i=0;i<mt.aa.size();i++) {
zd=mt.aa.get(i);
if(zd.shengming) {
for(int j=0;j<dtk.size();j++) {
dt=dtk.get(j);
if(dt.shengming) {
this.jzdf(zd,dt);
}
}
}
}
}
public void jzdf(zidan zd,tank dt) {
switch(dt.fangxing) {
case 0:
case 2:
if(zd.x>dt.x && zd.x<dt.x+20 && zd.y>dt.y && zd.y<dt.y+30 ) {
zd.shengming=false;
dt.shengming=false;
jilu.dtjs();
jilu.sdjl();
baozha bz=new baozha(dt.x,dt.y);
bzjh.add(bz);
}
break;
case 1:
case 3:
if(zd.x>dt.x && zd.x<dt.x+30 && zd.y>dt.y && zd.y<dt.y+20) {
zd.shengming=false;
dt.shengming=false;
jilu.dtjs();
jilu.sdjl();
baozha bz=new baozha(dt.x,dt.y);
bzjh.add(bz);
}
}
this.repaint();
}
//this.drawTank(mt2.getX(),mt.getY(),g,0,0);//双人坦克
// g.setColor(Color.YELLOW );
// g.fill3DRect(mt.getX(), mt.getY() , 5, 30, false);
// g.fill3DRect(mt.getX()+15, mt.getY() , 5, 30, false);
// g.fill3DRect(mt.getX()+5, mt.getY()+5 , 10, 20, false);
// g.fillOval(mt.getX()+5, mt.getY()+10, 10, 10);
// g.drawLine(mt.getX()+10 , mt.getY()+15,mt.getX()+10 , mt.getY()-3);
public void drawTank(int x,int y,Graphics g,int fangxing ,int leixing) {
switch(leixing) {
case 0://我的坦克
g.setColor(Color.yellow );break;
case 1:
g.setColor(Color.green);break;}
switch(fangxing) {
case 0://上
g.fill3DRect( x, y, 5, 30, false);
g.fill3DRect(x+15, y, 5, 30, false);
g.fill3DRect(x+5, y+5, 10, 20, false);
g.fillOval(x+5, y+10, 10, 10);
g.drawLine(x+10, y+15, x+10, y-3);
break;
case 1://左
g.fill3DRect( x, y, 30, 5, false);
g.fill3DRect(x, y+15, 30, 5, false);
g.fill3DRect(x+5, y+5, 20, 10, false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x-3, y+10);
break;
case 2://下
g.fill3DRect( x, y, 5, 30, false);
g.fill3DRect(x+15, y, 5, 30, false);
g.fill3DRect(x+5, y+5, 10, 20, false);
g.fillOval(x+5, y+10, 10, 10);
g.drawLine(x+10, y+15, x+10, y+33);
break;
case 3://右
g.fill3DRect( x, y, 30, 5, false);
g.fill3DRect(x, y+15, 30, 5, false);
g.fill3DRect(x+5, y+5, 20, 10, false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x+33, y+10);
break;
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_W) {
this.mt.setFangxing(0);
this.mt.xangshang();
}else if(e.getKeyCode()==KeyEvent.VK_A) {
this.mt.setFangxing(1);
this.mt.xangzuo();
}else if(e.getKeyCode()==KeyEvent.VK_S) {
this.mt.setFangxing(2);
this.mt.xangxia();
}else if(e.getKeyCode()==KeyEvent.VK_D) {
this.mt.setFangxing(3);
this.mt.xangyou();
}
if(e.getKeyCode()==KeyEvent.VK_J) {
if(mt.shengming&&this.mt.aa.size()<10) {
this.mt.fszd();
}
}
this.repaint();
}
public void run() {
while(true) {
try{Thread.sleep(100);}
catch(Exception e) {
}
for(int i=0;i<mt.aa.size();i++) {
zidan zd =mt.aa.get(i);
if(zd.shengming)
{
for(int j=0;j<dtk.size();j++) {
ditank dt=dtk.get(j);
if(dt.shengming&&mt.shengming){
this.jzdf1(zd,dt);
this.jzwf(zd, mt);
}
}
}
this.repaint();
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO 自动生成的方法存根
}
}
class tank {
int x=0;
int y=0;
int fangxing;
int sudu=5 ;
boolean shengming=true;
public int getFangxing() {
return fangxing;
}
public void setFangxing(int fangxing) {
this.fangxing = fangxing;
}
public int getSudu() {
return sudu;
}
public void setSudu(int sudu) {
this.sudu = sudu;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public tank(int x,int y) {
this.x=x;
this.y=y;
}
}
class mytank extends tank{
Vector <zidan> aa=new Vector<zidan>();
public mytank(int x,int y) {
super(x, y);
}
zidan zd=null;
public void fszd() {
switch(this.fangxing) {
case 0:
zd=new zidan(x+10,y,0);
aa.add(zd);
break;
case 1:
zd=new zidan(x,y+10,1);
aa.add(zd);
break;
case 2:
zd=new zidan(x+10,y+30,2);
aa.add(zd);
break;
case 3:
zd=new zidan(x+30,y+10,3);
aa.add(zd);
break;
}
Thread t1=new Thread(zd);
t1.start();
}
public void xangshang() {
if(y>0) {
y-=sudu;}
}
public void xangzuo() {
if(x>0) {
x-=sudu;}
}
public void xangxia() {
if(y<240) {
y+=sudu;}
}
public void xangyou() {
if(x<360) {
x+=sudu;}
}
}
class ditank extends tank implements Runnable{
Vector<zidan> dzd=new Vector<zidan>();
Vector<ditank> dtk=new Vector<ditank>();
int sudu=1;
int time=0;
public ditank(int x,int y) {
super(x,y);
}
public void dtkxl(Vector <ditank> dxl) {
this.dtk=dxl;
}
public void run() {
while(true) {
switch(this.fangxing) {
case 0:
for(int i=0;i<30;i++) {
if(y>0)
{y-=sudu;}
try {Thread.sleep(50);
}catch(Exception e) {
}}
break;
case 1:for(int i=0;i<30;i++) {
if(x>0) {
x-=sudu;}
try {Thread.sleep(50);
}catch(Exception e) {
}}
break;
case 2:for(int i=0;i<30;i++) {
if(y<260) {
y+=sudu;}
try {Thread.sleep(50);
}catch(Exception e) {
}}
break;
case 3:for(int i=0;i<30;i++) {
if(x<360) {
x+=sudu;}
try {Thread.sleep(50);
}catch(Exception e) {
}}
break;
}
this.fangxing=(int)(Math.random()*4);
if(this.shengming==false) {
break;
}
this.time++;
if(time%2==0) {
if(shengming) {
if(dzd.size()<5){
zidan zd=null;
switch(fangxing) {
case 0:
zd=new zidan(x+10,y,0);
dzd.add(zd);
break;
case 1:
zd=new zidan(x,y+10,1);
dzd.add(zd);
break;
case 2:
zd=new zidan(x+10,y+30,2);
dzd.add(zd);
break;
case 3:
zd=new zidan(x+30,y+10,3);
dzd.add(zd);
break;
}
Thread t5=new Thread(zd);
t5.start();
}
}
}
}
}
}
class zidan implements Runnable {
int x;
int y;
int fangxing;
int sudu=2;
boolean shengming=true;
public zidan(int x,int y,int fangxing) {
this.x=x;
this.y=y;
this.fangxing=fangxing;
}
public void run(){
while(true) {
try{Thread.sleep(50);}
catch(Exception e) {}
switch(fangxing) {
case 0:
y-=sudu;
break;
case 1:
x-=sudu;
break;
case 2:
y+=sudu;
break;
case 3:
x+=sudu;
break;
}
if (x<0||x>400||y<0||y>300) {
this.shengming=false;
break;
}
}
}
}
class baozha{
int x,y;
int shengcunq=9;
boolean shengming=true;
public baozha(int x,int y) {
this.x=x;
this.y=y;
}
public void suqsd() {
if(shengcunq>0) {
shengcunq--;
}else {
this.shengming=false;
}
}
}
class jilu{
private static Vector<ditank> dtk=new Vector<ditank>();
public static Vector<ditank> getDtk() {
return dtk;
}
public static void setDtk(Vector<ditank> dtk) {
jilu.dtk = dtk;
}
static Vector<weizhi> wzjh=new Vector<weizhi>();
private static int dtsl=10;
private static int mtsl=2;
private static int sdjl=0;
private static FileWriter fw=null;
private static BufferedWriter bw=null;
private static FileReader fr=null;
private static BufferedReader br=null;
public static void bcjl() {
try {
fw=new FileWriter("e:/eclipse/youxi/bin/坦克大战/cpjl.txt");
bw=new BufferedWriter(fw);
bw.write(sdjl+"\r\n");
}catch(Exception e) {
}
finally {
try {
bw.close();
fw.close();
}catch(Exception e) {}
}
}
public static void dqjl() {
try {
fr=new FileReader("e:/eclipse/youxi/bin/坦克大战/cpjl.txt");
br=new BufferedReader(fr);
String s=br.readLine();
sdjl=Integer.parseInt(s);
}catch(Exception e) {
}
finally {
try {
br.close();
fr.close();
}catch(Exception e) {}
}
}
public void cunpan() {
try {
fw=new FileWriter("e:/eclipse/youxi/bin/坦克大战/cpjl.txt");
bw=new BufferedWriter(fw);
bw.write(sdjl+"\r\n");
for(int i=0;i<dtk.size();i++) {
ditank dt=dtk.get(i);
if(dt.shengming) {
String zb=dt.x+" "+dt.y+" "+dt.fangxing+" " ;
bw.write(zb+"\r\n");
}
}
}catch(Exception e) {
}
finally {
try {
bw.close();
fw.close();
}catch(Exception e) {}
}
}
public static Vector<weizhi> dupan() {
try {
fr=new FileReader("e:/eclipse/youxi/bin/坦克大战/cpjl.txt");
br=new BufferedReader(fr);
String s=" ";
s=br.readLine();
sdjl=Integer.parseInt(s);
while((s=br.readLine())!=null) {
String[] sz=s.split(" ");
weizhi wz=new weizhi(Integer.parseInt(sz[0]),Integer.parseInt(sz[1]),Integer.parseInt(sz[2]));
wzjh.add(wz);}
}catch(Exception e) {
}
finally {
try {
br.close();
fr.close();
}catch(Exception e) {}
}
return wzjh;
}
public static int getSdjl() {
return sdjl;
}
public static void setSdjl(int sdjl) {
jilu.sdjl = sdjl;
}
public static int getDtsl() {
return dtsl;
}
public static void setDtsl(int dtsl) {
jilu.dtsl = dtsl;
}
public static int getMtsl() {
return mtsl;
}
public static void setMtsl(int mtsl) {
jilu.mtsl = mtsl;
}
public static void dtjs() {
dtsl--;
}
public static void mtjs() {
mtsl--;
if(mtsl<=0) {
System.exit(0);
}
}
public static void sdjl() {
sdjl++;
}
}
class weizhi{
int x;
int y;
int fangxing;
public weizhi(int x,int y,int fangxing) {
this.x=x;
this.y=y;
this.fangxing=fangxing;
}
}
class miusc extends Thread{
private String wjm;
public miusc(String ypwj) {
wjm=ypwj;
}
public void run() {
File wjl=new File(wjm);
AudioInputStream ypsrl=null;
try {
ypsrl=AudioSystem.getAudioInputStream(wjl);
}catch(Exception e) {}
AudioFormat format=ypsrl.getFormat();
SourceDataLine aqsj= null;
DataLine.Info info=new DataLine.Info(SourceDataLine.class, format);
try {
aqsj = (SourceDataLine)AudioSystem.getLine(info);
aqsj.open(format);
}catch(Exception e) {}
aqsj.start();
int zjtj=0;
byte[] hczj=new byte[1024];
try {
while(zjtj!=-1) {
zjtj=ypsrl.read(hczj,0,hczj.length);
if(zjtj>=0) {
aqsj.write(hczj,0,zjtj);
}
}
}catch(Exception e) {}
finally{
aqsj.drain();
aqsj.close();
}
}
}
[源代码打包下载]
初级程序员
by: 云代码会员 发表于:2020-06-06 00:16:08 顶(0) | 踩(0) 回复
希望能从中学到好的知识
回复评论