项目一之绘制小王八爬行

我用了两个类来实现此项目,一个tortoiseObject类用来存放王八的相关数据和画王八的方法,另一个Run
MyTortoise类用来运行实现这个项目。

tortoiseObject类

这个类的构造器属实太长了,如有大神,评论区可以给点建议吗?R

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class tortoiseObject extends Frame {
    //确定乌龟的一些部位以及相关数据
    //速度
    int speed;
    //头,圆形
    int HeadX,HeadY;int HeadWidth,HeadHeight;
    //壳,椭圆
    int bodyX,bodyY;int bodyWidth,bodyHeight;
    //脚,圆形,四个脚
    int foot1X,foot1Y,foot2X,foot2Y,foot3X,foot3Y,foot4X,foot4Y;
    int foot1Width,foot1Height,foot2Width,foot2Height,foot3Width,foot3Height,foot4Width,foot4Height;
    //眼睛,圆形
    int eye1X,eye1Y,eye2X,eye2Y;
    int eye1Width,eye1Height,eye2Width,eye2Height;
    //舌头
    int toneX,toneY;int toneWidth,toneHeight;
    //尾巴
    int wbX,wbY;int wbWidth,wbHeight;
    //创建一个构造器,用来创建乌龟对象
    public tortoiseObject(int speed,int HeadX,int HeadY,int HeadWidth,int HeadHeight,
     int bodyX,int bodyY,int bodyWidth,int bodyHeight,
     int foot1X,int foot1Y,int foot1Width,int foot1Height, int foot2X,int foot2Y,int foot2Width,int foot2Height,
     int foot3X,int foot3Y,int foot3Width,int foot3Height,int foot4X,int foot4Y,int foot4Width,int foot4Height,
     int eye1X, int eye1Y, int eye1Width,int eye1Height,int eye2X, int eye2Y, int eye2Width,int eye2Height,
      int toneX,int toneY,int toneWidth,int toneHeight,int wbX,int wbY,int wbWidth,int wbHeight){
        this.speed=speed;
this.HeadX=HeadX;this.HeadY=HeadY;this.HeadWidth=HeadWidth;this.HeadHeight=HeadHeight;
this.bodyX =bodyX;this.bodyY=bodyY;this.bodyWidth=bodyWidth;this.bodyHeight=bodyHeight;
this.foot1X=foot1X;this.foot1Y=foot1Y;this.foot2X=foot2X;this.foot2Y=foot2Y;this.foot3X=foot3X;this.foot3Y=foot3Y;this.foot4X=foot4X;this.foot4Y=foot4Y;
this.foot1Width=foot1Width;this.foot1Height=foot1Height;this.foot2Width=foot2Width;this.foot2Height=foot2Height;this.foot3Width=foot3Width;this.foot3Height=foot3Height;this.foot4Width=foot4Width;this.foot4Height=foot4Height;
this.eye1X=eye1X;this.eye1Y=eye1Y;this.eye2X=eye2X;this.eye2Y=eye2Y;
this.eye1Width=eye1Width;this.eye1Height=eye1Height;this.eye2Width=eye2Width;this.eye2Height=eye2Height;
this.toneX=toneX;this.toneY=toneY;this.toneWidth=toneWidth;this.toneHeight=toneHeight;
this.wbX=wbX;this.wbY=wbY;this.wbWidth=wbWidth;this.wbHeight=wbHeight;
    }


    //定义画乌龟的方法
//画法一
    public void drawTortoiseA(Graphics g){
        //画尾巴
        g.setColor(new Color(0, 100, 0));
        g.fillOval(wbX, wbY+290, wbWidth, wbHeight);
        //设置画笔颜色和背景颜色相同
        g.setColor(Color.white);
        g.fillOval(wbX+20, wbY+300, wbWidth, wbHeight);
        //画舌头
        g.setColor(new Color(212,100,89));
        g.fillOval(toneX,toneY,toneWidth,toneHeight);
        //画头
        g.setColor(new Color(132,224,77));
        g.fillOval(HeadX,HeadY,HeadWidth,HeadHeight);

        //画脚
        g.setColor(new Color(150,212,178));
        g.fillOval(foot1X,foot1Y,foot1Width,foot1Height);
        g.fillOval(foot2X,foot2Y,foot2Width,foot2Height);
        g.fillOval(foot3X,foot3Y,foot3Width,foot3Height);
        g.fillOval(foot4X,foot4Y,foot4Width,foot4Height);
        //画壳
        g.setColor(new Color(29,102,5));
        g.fillOval(bodyX,bodyY,bodyWidth,bodyHeight);
        //画眼睛
        g.setColor(Color.black);
        g.fillOval(eye1X,eye1Y,eye1Width,eye1Height);
        g.fillOval(eye2X,eye2Y,eye2Width,eye2Height);

    }
//画法二
    public void drawTortoiseB(Graphics g){
        //画尾巴
        g.setColor(Color.black);
        g.fillOval(wbX, wbY, wbWidth, wbHeight);
        //设置画笔颜色和背景颜色相同
        g.setColor(new Color(0, 100, 0));
        g.fillOval(wbX+10, wbY+5, wbWidth, wbHeight);

        //画舌头
        g.setColor(new Color(212,100,89));
        g.fillOval(toneX,toneY,toneWidth,toneHeight);
        //画头
        g.setColor(new Color(132,224,77));
        g.fillOval(HeadX,HeadY,HeadWidth,HeadHeight);

        //画脚
        g.setColor(Color.yellow);
        g.fillOval(foot1X,foot1Y,foot1Width,foot1Height);
        g.fillOval(foot2X,foot2Y,foot2Width,foot2Height);
        g.fillOval(foot3X,foot3Y,foot3Width,foot3Height);
        g.fillOval(foot4X,foot4Y,foot4Width,foot4Height);
        //画壳
        g.setColor(Color.blue);
        g.fillOval(bodyX,bodyY,bodyWidth,bodyHeight);
        //画眼睛
        g.setColor(Color.black);
        g.fillOval(eye1X,eye1Y,eye1Width,eye1Height);
        g.fillOval(eye2X,eye2Y,eye2Width,eye2Height);

    }


}

RunMyTortoise 类

public class RunMyTortoise extends Frame{
    //窗口的相关信息
    public static final int WindowWidth=2000;//游戏窗口宽度
    public static final int WindowHeight=1300;//游戏窗口高度
    boolean left,right,up,down;//爬行方向控
    int speed=1;//更新的速度
    int f1=0,f2=0;
    //Image sky=GameUtil.getImage("imges01/sky.jpg");
   //创建乌龟对象
   tortoiseObject t2=new tortoiseObject(3,450,540,60,100,380,590,200,250,350,620,75,55,350,740,75,55,530,620,75,55,530,740,75,55,465,564,5,5,495,564,5,5,470,530,25,25,480,820,50,50);
    tortoiseObject t1=new tortoiseObject(3,425,390,120,200,280,480,400,500,220,590,150,110,230,810,150,110,600,590,150,110,590,810,150,110,460,420,10,10,505,420,10,10,462,370,50,50,450,650,100,100);
    //绘制窗口内容,用paint方法
   @Override
   public void paint (Graphics g){
       //g.drawImage(sky, 0, 0, 2000, 1300, null);

        if(left){
            f1+=1;
            t1.HeadX-=t1.speed;
            t1.bodyX-=t1.speed;
            t1.eye1X-=t1.speed;
            t1.eye2X-=t1.speed;
            t1.toneX-=t1.speed;
            t1.wbX-=t1.speed;
            if(f1%6==5){
                t1.wbY+=7;
            }
            if(f1%6==0){
                t1.foot2X-=5*t1.speed;
                t1.foot3X-=5*t1.speed;
                t1.foot1X-=5*t1.speed;
                t1.foot4X-=5*t1.speed;

                t1.HeadX+=t1.speed;
                t1.bodyX+=t1.speed;
                t1.eye1X+=t1.speed;
                t1.eye2X+=t1.speed;
                t1.toneX+=t1.speed;
                t1.wbY-=7;
                t1.wbX+=t1.speed;
            }

            f2+=1;
            t2.HeadX-=t2.speed;
            t2.bodyX-=t2.speed;
            t2.eye1X-=t2.speed;
            t2.eye2X-=t2.speed;
            t2.toneX-=t2.speed;
            t2.wbX-=t2.speed;
            if(f2%6==5){
                t2.wbY+=7;
            }
            if(f2%6==0){
                t2.foot2X-=5*t2.speed;
                t2.foot3X-=5*t2.speed;
                t2.foot1X-=5*t2.speed;
                t2.foot4X-=5*t2.speed;

                t2.HeadX+=t2.speed;
                t2.bodyX+=t2.speed;
                t2.eye1X+=t2.speed;
                t2.eye2X+=t2.speed;
                t2.toneX+=t2.speed;
                t2.wbY-=7;
                t2.wbX+=t2.speed;
            }


        }

       if(right){
           f1+=1;
           t1.HeadX+=t1.speed;
           t1.bodyX+=t1.speed;
           t1.eye1X+=t1.speed;
           t1.eye2X+=t1.speed;
           t1.toneX+=t1.speed;
           t1.wbX+=t1.speed;
           if(f1%6==5){
               t1.wbY-=7;
           }
           if(f1%6==0){
               t1.foot2X+=5*t1.speed;
               t1.foot3X+=5*t1.speed;
               t1.foot1X+=5*t1.speed;
               t1.foot4X+=5*t1.speed;

               t1.HeadX-=t1.speed;
               t1.bodyX-=t1.speed;
               t1.eye1X-=t1.speed;
               t1.eye2X-=t1.speed;
               t1.toneX-=t1.speed;
               t1.wbY+=7;
               t1.wbX-=t1.speed;
           }
           f2+=1;
           t2.HeadX+=t2.speed;
           t2.bodyX+=t2.speed;
           t2.eye1X+=t2.speed;
           t2.eye2X+=t2.speed;
           t2.toneX+=t2.speed;
           t2.wbX+=t2.speed;
           if(f2%6==5){
               t2.wbY-=7;
           }
           if(f2%6==0){
               t2.foot2X+=5*t2.speed;
               t2.foot3X+=5*t2.speed;
               t2.foot1X+=5*t2.speed;
               t2.foot4X+=5*t2.speed;

               t2.HeadX-=t2.speed;
               t2.bodyX-=t2.speed;
               t2.eye1X-=t2.speed;
               t2.eye2X-=t2.speed;
               t2.toneX-=t2.speed;
               t2.wbY+=7;
               t2.wbX-=t2.speed;
           }


       }
       if(up){
           f1+=1;
           t1.HeadY-=t1.speed;
           t1.bodyY-=t1.speed;
           t1.eye1Y-=t1.speed;
           t1.eye2Y-=t1.speed;
           t1.toneY-=t1.speed;
           t1.wbY-=t1.speed;
           if(f1%6==5){
               t1.wbX+=7*t1.speed;
           }
           if(f1%6==0){
               t1.foot2Y-=5*t1.speed;
               t1.foot3Y-=5*t1.speed;
               t1.foot1Y-=5*t1.speed;
               t1.foot4Y-=5*t1.speed;

               t1.HeadY+=t1.speed;
               t1.bodyY+=t1.speed;
               t1.eye1Y+=t1.speed;
               t1.eye2Y+=t1.speed;
               t1.toneY+=t1.speed;
               t1.wbX-=7*t1.speed;
               t1.wbY+=t1.speed;
           }

           f2+=1;
           t2.HeadY-=t2.speed;
           t2.bodyY-=t2.speed;
           t2.eye1Y-=t2.speed;
           t2.eye2Y-=t2.speed;
           t2.toneY-=t2.speed;
           t2.wbY-=t2.speed;
           if(f2%6==5){
               t2.wbX+=7;
           }
           if(f2%6==0){
               t2.foot2Y-=5*t2.speed;
               t2.foot3Y-=5*t2.speed;
               t2.foot1Y-=5*t2.speed;
               t2.foot4Y-=5*t2.speed;

               t2.HeadY+=t2.speed;
               t2.bodyY+=t2.speed;
               t2.eye1Y+=t2.speed;
               t2.eye2Y+=t2.speed;
               t2.toneY+=t2.speed;
               t2.wbX-=7;
               t2.wbY+=t2.speed;
           }




       }
       if(down){

           f1+=1;
           t1.HeadY+=t1.speed;
           t1.bodyY+=t1.speed;
           t1.eye1Y+=t1.speed;
           t1.eye2Y+=t1.speed;
           t1.toneY+=t1.speed;
           t1.wbY+=t1.speed;
           if(f1%6==5){
               t1.wbX-=7;
           }
           if(f1%6==0){
               t1.foot2Y+=5*t1.speed;
               t1.foot3Y+=5*t1.speed;
               t1.foot1Y+=5*t1.speed;
               t1.foot4Y+=5*t1.speed;

               t1.HeadY-=t1.speed;
               t1.bodyY-=t1.speed;
               t1.eye1Y-=t1.speed;
               t1.eye2Y-=t1.speed;
               t1.toneY-=t1.speed;
               t1.wbX+=7;
               t1.wbY-=t1.speed;
           }

           f2+=1;
           t2.HeadY+=t2.speed;
           t2.bodyY+=t2.speed;
           t2.eye1Y+=t2.speed;
           t2.eye2Y+=t2.speed;
           t2.toneY+=t2.speed;
           t2.wbY+=t2.speed;
           if(f2%6==5){
               t2.wbX-=7;
           }
           if(f2%6==0){
               t2.foot2Y+=5*t2.speed;
               t2.foot3Y+=5*t2.speed;
               t2.foot1Y+=5*t2.speed;
               t2.foot4Y+=5*t2.speed;

               t2.HeadY-=t2.speed;
               t2.bodyY-=t2.speed;
               t2.eye1Y-=t2.speed;
               t2.eye2Y-=t2.speed;
               t2.toneY-=t2.speed;
               t2.wbX+=7;
               t2.wbY-=t2.speed;
           }
       }
       t1.drawTortoiseA(g);
       t2.drawTortoiseB(g);

   }

    //初始化窗口
   public void launchFrame(){
       this.setTitle("乌龟上天了!");
       setVisible(true);//窗口是否可见
       setSize(WindowWidth,WindowHeight);//窗口大小
       setLocation(0,0);//窗口打开的位置

       //增加关闭窗口的动作
       this.addWindowListener(new WindowAdapter() {
           @Override
           public void windowClosing(WindowEvent e) {
               System.exit(0);
               //正常退出程序
           }
       });
       new RunMyTortoise.PaintThread().start();//启动重画窗口的线程,用类名调用
        this.addKeyListener(new KeyMonitor());//启动键盘监听
   }

    /*定义了一个重画窗口的线程类。
    定义成内部类是为了方便直接使用窗口类的相关方法*/
    class PaintThread extends Thread{
        public void run(){
            while(true){
                repaint();//内部类可以直接使用外部类的成员
                try {
                    Thread.sleep(speed);//1s=1000ms 1s画20次
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //内部类,实现键盘的监听处理
    class KeyMonitor extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println("按下"+e.getKeyCode());
            if(e.getKeyCode()==KeyEvent.VK_LEFT){
                left=true;
            }
            if(e.getKeyCode()==KeyEvent.VK_RIGHT){
                right=true;
            }
            if(e.getKeyCode()==KeyEvent.VK_UP){
                up=true;
            }
            if(e.getKeyCode()==KeyEvent.VK_DOWN){
                down=true;
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            System.out.println("抬起"+e.getKeyCode());
            if(e.getKeyCode()==KeyEvent.VK_LEFT){
                left=false;
            }
            if(e.getKeyCode()==KeyEvent.VK_RIGHT){
                right=false;
            }
            if(e.getKeyCode()==KeyEvent.VK_UP){
                up=false;
            }
            if(e.getKeyCode()==KeyEvent.VK_DOWN){
                down=false;
            }
        }
    }

    //解决屏幕闪的问题
    private Image offScreenImage=null;
    public void update(Graphics g){
        if (offScreenImage==null);
        offScreenImage=this.createImage(WindowWidth,WindowHeight);//这是游戏窗口的宽度和高度
        Graphics gOff=offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage,0,0,null);
    }
    public static void main(String[] args) {
        RunMyTortoise f1=new RunMyTortoise();
        f1.launchFrame();
    }
}

 这次的代码有了一些意外的效果,我起初是没有想要实现解体和组装这个功能的,但是意外实现了这个效果,同时按上下键或者左右键即可在上下方向或者左右方向实行解体或者组装功能。

不知道控制坐标的代码是怎么实现的?有知道的可以在评论区发表见解。

乌龟爬行-解体与组装

乌龟爬行-上下左右

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笃岩_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值