当前位置:文档之家› 韩顺平java坦克大战1.0版本_源代码

韩顺平java坦克大战1.0版本_源代码

/**画坦克1.0*/import java.awt.Color;import java.awt.Graphics;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import javax.swing.JFrame;import javax.swing.JPanel;public class MyTankGame extends JFrame{MyPanel mp=null;public static void main(String[] args){MyTankGame mtk=new MyTankGame();}public MyTankGame(){mp=new MyPanel();this.add(mp);//把面板加入窗体//注册监听this.addMouseListener(mp);this.addKeyListener(mp);this.addMouseMotionListener(mp);this.addWindowListener(mp);this.setTitle("坦克大战");//窗体标题this.setSize(600,400);//大小,宽,高(像素)this.setLocation(300,300);//显示位置。

左边距,上边距//禁止用户改变窗口大小this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);//显示}}//我的面板class MyPanel extends JPanel implementsWindowListener,MouseListener,MouseMotionListener,KeyListener {//定义一个我的坦克Hero hero=null;Diren diren=null;public MyPanel(){hero=new Hero(300,200);diren=new Diren(100,0);//diren.start();}public void paint(Graphics g){super.paint(g);//画坦克(到时候封装到一个函数)//画左边矩形//g.setColor(Color.black);g.fillRect(0, 0, 600, 400);this.drawTank(hero.getX(),hero.getY(), g,hero.dstct,1);this.drawTank(diren.getX(),diren.getY(), g,1,0);this.drawTank(diren.getX()+50,diren.getY(), g,1,0);this.drawTank(diren.getX()+100,diren.getY(), g,1,0);}//画坦克的函数public void drawTank(int x,int y,Graphics g,int direct,int type) {switch (type){//0我的坦克case 0:g.setColor(Color.blue);break;//1敌人坦克case 1:g.setColor(Color.GREEN);break;}// 判断方向switch (direct){//0向上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,true);//中g.setColor(Color.RED);g.drawLine(x+10, y+15, x+10,y);//线g.setColor(Color.YELLOW);g.fillOval(x+5, y+10, 10, 10);//圆break;case 1://下g.fill3DRect(x, y, 5, 30,false);g.fill3DRect(x+15, y, 5, 30,false);g.fill3DRect(x+5, y+5, 10, 20,true);g.setColor(Color.RED);g.drawLine(x+10, y+15, x+10,y+30);g.setColor(Color.YELLOW);g.fillOval(x+5, y+10, 10, 10);break;case 2://左g.fill3DRect(x, y, 30, 5,false);g.fill3DRect(x, y+15, 30, 5,false);g.fill3DRect(x+5, y+5, 20, 10,true);g.setColor(Color.RED);g.drawLine(x+15, y+10, x,y+10);g.setColor(Color.YELLOW);g.fillOval(x+10, y+5, 10, 10);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,true);g.setColor(Color.RED);g.drawLine(x+15, y+10, x+30,y+10);g.setColor(Color.YELLOW);g.fillOval(x+10, y+5, 10, 10);break;}}public void keyPressed(KeyEvent e){// 键按下监听System.out.println("按下:"+e.getKeyChar());if(e.getKeyCode()==KeyEvent.VK_DOWN){hero.setY(hero.getY()+hero.getSpeed());hero.dstct=1;}else if(e.getKeyCode()==KeyEvent.VK_UP){hero.setY(hero.getY()-hero.getSpeed());hero.dstct=0;}else if(e.getKeyCode()==KeyEvent.VK_LEFT){hero.setX(hero.getX()-hero.getSpeed());hero.dstct=2;}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){hero.setX(hero.getX()+hero.getSpeed());hero.dstct=3;}this.repaint();}public void keyReleased(KeyEvent e) {// 键松开监听}public void keyTyped(KeyEvent e) {// 键的一个值被输出}public void mouseClicked(MouseEvent e) {//点击System.out.println("鼠标被点击了x="+e.getX()+"y="+e.getY()); }public void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub//进入System.out.println("鼠标进入x="+e.getX()+"y="+e.getY()); }public void mouseExited(MouseEvent e) {// TODO Auto-generated method stub//离开System.out.println("鼠标离开x="+e.getX()+"y="+e.getY()); }public void mousePressed(MouseEvent e) {// TODO Auto-generated method stub//压下System.out.println("鼠标被按下x="+e.getX()+"y="+e.getY()); }public void mouseReleased(MouseEvent e) {// TODO Auto-generated method stubSystem.out.println("鼠标被松开x="+e.getX()+"y="+e.getY());//松开}public void mouseDragged(MouseEvent e) {// 拖拽System.out.println("鼠标被拖拽x="+e.getX()+"y="+e.getY()); }public void mouseMoved(MouseEvent e) {// 移动System.out.println("鼠标被移动x="+e.getX()+"y="+e.getY()); }public void windowActivated(WindowEvent e) {// 激活TODO Auto-generated method stub}public void windowClosed(WindowEvent e) {// 窗口关闭}public void windowClosing(WindowEvent e) {//正在关闭TODO Auto-generated method stub}public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub}public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub}public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub}public void windowOpened(WindowEvent e) { // 打开TODO Auto-generated method stub}}//坦克父类class Tank{//坦克横坐标int x=0;//坦克纵坐标int y=0;int speed=5;//速度static int dstct=0;public Tank(int x, int y){this.x=x;this.y=y;}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 static int getDstct() {return dstct;}public static void setDstct(int dstct) {Tank.dstct = dstct;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}}//我的坦克class Hero extends Tank{public Hero(int x ,int y ){super(x, y);}// public int tankup// {// y=y-getSpeed();// }// public int tankdown// {// y=y+speed;// }// public int tankleft// {// x=x-speed;// }// public int tankright// {// x=x+speed;// }//}//敌人坦克class Diren extends Tank implements Runnable {public Diren(int x ,int y ){super(x, y);}public void run(){while(true){ //休眠一秒//1000秒=1秒.进入堵塞。

相关主题