code

编程记录 · 2018-10-10

import java.awt.*;
import java.awt.event.*;

public class Tanke {
    public static void main(String[] args) {
        Frame w = new Frame();
        w.setSize(1024, 768);
//        w.setBackground(Color.BLACK);
        MyPanel mp = new MyPanel();
        w.add(mp);
        w.addKeyListener(mp);
        mp.addKeyListener(mp);

        w.show();
    }

}
class MyPanel extends Panel implements KeyListener{
    int x1,x,y,fx;
    public void pd(int fx,int x,int y,Graphics g){
        if(fx==1){
            g.fillRect(x+20, y+0, 20, 20);
        }
        if(fx==2){
            g.fillRect(x+20, y+40, 20, 20);
        }
        if(fx==3){
            g.fillRect(x+40, y+20, 20, 20);
        }
        if(fx==4){
            g.fillRect(x+0, y+20, 20, 20);
        }
    }
    public void tk(int fx,int x,int y,Graphics g){
    if(fx==1){
        //上
        g.fillRect(x+20, y+0, 20, 20);
        //炮筒
        g.fillRect(x+0,y+20 , 20, 20);
        g.fillRect(x+20,y+20 , 20, 20);
        g.fillRect(x+40,y+20 , 20, 20);
        //身体
        g.fillRect(x+0,y+40, 20, 20);
        //左腿
        g.fillRect(x+40, y+40, 20, 20);
    }
    
    if(fx==2){
        //下
        g.fillRect(x+20, y+40, 20, 20);
        //炮筒
        g.fillRect(x+0,y+20 , 20, 20);
        g.fillRect(x+20,y+20 , 20, 20);
        g.fillRect(x+40,y+20 , 20, 20);
        //身体
        g.fillRect(x+0,y+0, 20, 20);
        //左腿
        g.fillRect(x+40, y+0, 20, 20);
    }
    if(fx==3){
        //右
        g.fillRect(x+40, y+20, 20, 20);
        //炮筒
        g.fillRect(x+20,y+0 , 20, 20);
        g.fillRect(x+20,y+20 , 20, 20);
        g.fillRect(x+20,y+40 , 20, 20);
        //身体
        g.fillRect(x+0,y+0, 20, 20);
        //左腿
        g.fillRect(x+0, y+40, 20, 20);
        }
    if(fx==4){
        //左
        g.fillRect(x+0, y+20, 20, 20);
        //炮筒
        g.fillRect(x+20,y+0 , 20, 20);
        g.fillRect(x+20,y+20 , 20, 20);
        g.fillRect(x+20,y+40 , 20, 20);
        //身体
        g.fillRect(x+40,y+0, 20, 20);
        //左腿
        g.fillRect(x+40, y+40, 20, 20);
        }
    }

    public void paint(Graphics g){
        tk(fx,x,y,g);
        pd(fx,x,y,g);
    }
    @Override
    public void keyPressed(KeyEvent e){

        if(e.getKeyCode()==37){
            x-=20;
            fx=4;
            
        }
        if(e.getKeyCode()==38){
            y-=20;
            fx=1;
        }
        if(e.getKeyCode()==39){
            x+=20;
            fx=3;
        }
        if(e.getKeyCode()==40){
            y+=20;
            fx=2;
        }
        repaint();
    }
    @Override
    public void keyReleased(KeyEvent arg0){
        
    }
    @Override
    public void keyTyped(KeyEvent arg0){
        
    }
    public void run(){
        while(true){
            try{
                if(fx==1){
                for(int i=0; i<10;i++){
                    y[i]--;
                    if(y[i]<0){
                        y[i]=510;
                    }
                }
                Thread.sleep(300);
            }catch(Exception e){
                
            }
            repaint();
        }
    }
}
Theme Jasmine by Kent Liao
辽ICP备2021009421号-2