弹跳的字符串

/**
*弹跳的字符串
*/

import static javaconsole.JavaConsole.*;
import java.util.Random;

public class  BouncingString
{
    public static void main(String[] args) 
    {
        String msg = "记得每天要微笑面对生活哈!";
        int x;                        //广告字符串首字母x坐标(初始时是随机产生的)
        int y;                        //广告字符串首字母y坐标(初始时是随机产生的)
        int xdir;                    //x轴运动方向:1向右运动    -1向左运动
        int ydir;                    //y轴运动方向:1向下运动    -1向上运动
        int delaytime;                //延迟时间:单位毫秒
        int len = msg.length();        //字符串长度
        final int UP = 72;            //向上的光标键
        final int DOWN = 80;        //向下的光标键

        //做一些初始化工作
        set_size(80,25);
        set_title("弹跳的字符串广告!延时010毫秒,加速,减速!");
        hide_cursor();    //隐藏光标
        Random r = new Random();      
        x = r.nextInt(79);
        y = r.nextInt(25);
        xdir = ((r.nextInt(2) == 1) ? 1 : -1);
        ydir = ((r.nextInt(2) == 1) ? 1 : -1);
        delaytime = 100;

        while(true)
        {


            //输出广告字符串(难点:如何根据首字符的坐标和运动方向计算每个字符的坐标?)
            int tx = x;            //(tx,ty)是当前要输出的字符坐标
            int ty = y;
            int dx = xdir;        //计算当前字符的增量
            int dy = ydir;

            for(int i = 0; i < len; i++)
            {
                tx = tx - dx;
                ty = ty - dy;
                if(tx <= 0 || tx >= 78)
                {
                    dx = -dx;
                }

                if(ty <= 0 || ty >= 24)
                {
                    dy = -dy;
                }

                gotoxy(tx,ty);
                set_color(0,r.nextInt(15) + 1);    //避免产生背景色
                System.out.print(msg.charAt(i));
            
            }

                
                //延迟一段时间
                delay(delaytime);

                //擦除原来的广告字符串(只需清除尾字符即可)
                gotoxy(tx,ty);
                System.out.print(" ");


            //移动广告字符串(更新它的首字母坐标和运动方向)
            if(x <= 0 || x >= 78)
            {
                xdir = -xdir;
            }

            if(y <= 0 || y >= 24)
            {
                ydir = -ydir;
            }

            x = x + xdir;
            y = y + ydir;


            //判断是否有键按下,有,则根据上下光标键修改延迟时间,然后修改标题
            if(kbhit())
            {
                int key = getkey();
                switch(key)
                {
                    case UP:
                        if(delaytime > 10)
                    {
                        delaytime = delaytime - 10;
                    }
                    break;

                    case DOWN:
                        delaytime = delaytime + 10;
                }

                set_title("弹跳的字符串广告!延时" + String.format("%03d",delaytime) + "毫秒,加速,减速!");
            }
        
        }
    }
}


重构后的代码程序:

main方法的内容简单化了,开发的思路清晰了许多,分成多了小模块。

/**
*弹跳的字符串
*/

import static javaconsole.JavaConsole.*;
import java.util.Random;

public class  BouncingString2023
{
        String msg = "记得每天要微笑面对生活哈!";
        int x;                        //广告字符串首字母x坐标(初始时是随机产生的)
        int y;                        //广告字符串首字母y坐标(初始时是随机产生的)
        int xdir;                    //x轴运动方向:1向右运动    -1向左运动
        int ydir;                    //y轴运动方向:1向下运动    -1向上运动

        int delaytime;                //延迟时间:单位毫秒
        int len = msg.length();        //字符串长度
        final int UP = 72;            //向上的光标键
        final int DOWN = 80;        //向下的光标键
        Random r;

    public static void main(String[] args) 
    {
        BouncingString2023 bs = new BouncingString2023();
        bs.start();
    }

    
    //启动程序
    public void start()
    {
        //初始化
        init();

        while(true)
        {
            //更新内部状态
            update();

            //根据内部状态进行渲染
            render();
        
        }
    }

    //初始化
    public void init()
    {
        //做一些初始化工作
        set_size(80,25);
        set_title("弹跳的字符串广告!延时010毫秒,加速,减速!");
        hide_cursor();    //隐藏光标
        r = new Random();      
        x = r.nextInt(79);
        y = r.nextInt(25);

        xdir = ((r.nextInt(2) == 1) ? 1 : -1);
        ydir = ((r.nextInt(2) == 1) ? 1 : -1);
        delaytime = 10;
        
    }

    //更新内部状态
    public void update()
    {
        //移动广告字符串,修改首字符的坐标及运动方向
        movingString();

        //检查是否输入按键
        checkKeyInput();
    
    }


    //移动广告字符串,修改首字符的坐标及运动方向
    public void movingString()
    {
        //移动广告字符串(更新它的首字母坐标和运动方向)
            if(x <= 0 || x >= 78)
            {
                xdir = -xdir;
            }

            if(y <= 0 || y >= 24)
            {
                ydir = -ydir;
            }

            x = x + xdir;
            y = y + ydir;
    }

    //检查是否输入按键
    public void checkKeyInput()
    {
        //判断是否有键按下,有,则根据上下光标键修改延迟时间,然后修改标题
            if(kbhit())
            {
                int key = getkey();
                switch(key)
                {
                    case UP:
                        if(delaytime > 10)
                    {
                        delaytime = delaytime - 10;
                    }
                    break;

                    case DOWN:
                        delaytime = delaytime + 10;
                }

                set_title("弹跳的字符串广告!延时" + String.format("%03d",delaytime) + "毫秒,加速,减速!");
            }
    }

    //根据内部状态进行渲染
    public void render()
    {
        //输出广告字符串(难点:如何根据首字符的坐标和运动方向计算每个字符的坐标?)
            int tx = x;            //(tx,ty)是当前要输出的字符坐标
            int ty = y;
            int dx = xdir;        //计算当前字符的增量
            int dy = ydir;

            for(int i = 0; i < len; i++)
            {
                tx = tx - dx;
                ty = ty - dy;
                if(tx <= 0 || tx >= 78)
                {
                    dx = -dx;
                }

                if(ty <= 0 || ty >= 24)
                {
                    dy = -dy;
                }

                gotoxy(tx,ty);
                set_color(0,r.nextInt(15) + 1);    //避免产生背景色
                System.out.print(msg.charAt(i));
            
            }

                
                //延迟一段时间
                delay(delaytime);

                //擦除原来的广告字符串(只需清除尾字符即可)
                gotoxy(tx,ty);
                System.out.print(" ");
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值