Java控制台弹球小游戏(面向对象版本)

在模拟弹球小游戏中设计的对象:弹棒(Stick),小球(Ball),击中播放声音(Explode),游戏的组织进行(Game)。各个类的构造方法以及相关代码如下:

/**
*模拟弹棒
*/

public class  Stick
{
    private int x;        //棒子左端x坐标
    private int y;        //棒子右端的y坐标
    private int len;    //棒子的长度

    public Stick()
    {
    }

    public Stick(int x, int y, int len)
    {
        this.x = x;
        this.y = y;
        this.len = len;
    }

    public int getX()
    {
        return this.x;
    }

    public int getY()
    {
        return this.y;
    }

    public void setX(int x)
    {
        this.x = x;
    }

    public int getLen()
    {
        return this.len;
    }

    public void setLen(int len)
    {
        this.len = len;
    }

    //根据玩家的方向键移动棒子:1向右 -1向左
    public void move(int dir)
    {
        x = x + dir * 4;
        if(x < 4)
        {
            x = 0;
        }

        if(x >= 68)
        {
            x = 71;
        }
    }

}

/**
*模拟小球
*/

public class Ball 
{
    private int x;        //小球的x坐标
    private int y;        //小球的y坐标
    private int xdir;    //小球的运动方向:1向右 -1向左
    private int ydir;    //小球的运动方向:1向下 -1向上

    public Ball()
    {
    }

    public Ball(int x, int y, int xdir, int ydir)
    {
        this.x = x;
        this.y = y;
        this.xdir = xdir;
        this.ydir = ydir;
    }

    public int getX()
    {
        return this.x;
    }

    public int getY()
    {
        return this.y;
    }

    public void setX(int x)
    {
        this.x = x;
    }

    public void setY(int y)
    {
        this. y = y;
    }

    public int getXdir()
    {
        return this.xdir;
    }

    public int getYdir()
    {
        return this.ydir;    
    }

    public void setXdir(int xdir)
    {
        this.xdir = xdir;
    }

    public void setYdir(int ydir)
    {
        this.ydir = ydir;
    }

    //小球移动,返回是否到底了
    public boolean move()
    {
        //移动修改坐标
        x = x + xdir;
        y = y + ydir;

        //边界判断,小球变向
        if(x <= 0 || x >= 79)    //左右边界判断
        {
            xdir = -xdir;
        }

        if(y <= 0 || y >= 23)    //上下边界判断
        {
            ydir = -ydir;
        }

            return (y >= 23);        //是否到底
    }

}

/**
*棒子弹中小球时播放声音
*/
import java.applet.Applet;
import java.applet.AudioClip;

public class  Explode
{
    private AudioClip explode;

    public Explode()
    {
        explode = Applet.newAudioClip(Explode.class.getResource("Boing.wav"));
    }

    //播放击中的声音
    public void play()
    {
        explode.play();
    }
    
}
 

/**
*弹小球游戏2023版
*/
import static javaconsole.JavaConsole.*;


public class Game 
{
    public static final int KEY_LEFT = 75;        //向左的光标键
    public static final int KEY_RIGHT = 77;        //向右的光标键

    private Ball ball;            //小球
    private Stick stick;        //棒子
    private Explode sound;    //弹中时的声音
    private int score;            //玩家的得分,即棒子弹中小球的次数
    private boolean isHit;        //是否弹中

    public Game()
    {
        score = 0;
        isHit = false;
        ball = new Ball(0, 0, 1, 1);
        stick = new Stick(40, 23, 8);
        sound = new Explode();

        set_size(80,25);
        set_title("Java控制台弹球小游戏");
        hide_cursor();
        set_color(0, 0xa);

        //绘制初始的全部砖块
        draw_init_blocks();

    }

    public static void main(String[] args) 
    {
        Game game = new Game();

        game.start();
    }

    //游戏主控方法
    public void start()
    {
        while(true)
        {
            //更新内部状态数据
            update();

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

    
    //更新内部状态数据
    public void update()
    {
        int dir = -1;        //棒子的运动方向
        //移动小球
        if(ball.move())        //到底了吗?
        {
            check_hit();    //弹中小球了吗?
        }

        //根据玩家的方向键移动棒子
        if(kbhit())
        {
            int ch = getkey();

            switch(ch)
            {
                case KEY_LEFT:    //向左的光标键
                dir = -1;
                break;

                case KEY_RIGHT:    //向右的光标键
                dir = 1;
            }

            stick.move(dir);    //移动棒子
        }
    }

    //判断是否弹中小球
    public void check_hit()
    {
        if((ball.getX() < ball.getX()) || (ball.getX() > stick.getX() + stick.getLen()))    //没弹中
        {
            //游戏结束
            end_game();
        }
        else    //弹中小球
        {
            score++;
            isHit = true;
        }
    }

    //游戏结束
    public void end_game()
    {
        gotoxy(30, 10);
        System.out.printf("游戏结束,总共得分%d分!", score);

        set_color(0,7);        //恢复黑底白字
        getkey();            //按任意键退出游戏
        System.exit(0);
    }

    //初始化小方块
    public void draw_init_blocks()
    {
        for(int x = 5; x < 75; x += 2)
        {
            gotoxy(x, 0);
            set_color(0, (int)(Math.random() * 15) + 1);
            System.out.print("■");
        }
    }

    //根据内部状态渲染
    public void render()
    {
        //如果弹中播放声音
        if(isHit)
        {
            sound.play();    //播放声音
            isHit = false;
        }

        //显示玩家得分
        gotoxy(0, 0);
        set_color(0, 0xa);
        System.out.printf("得分:%d", score);

        //绘制小球
        int x = ball.getX();
        int y = ball.getY();

        gotoxy(x,y);
        System.out.print("o");

        delay(60);

        gotoxy(x, y);
        System.out.print(" ");

        //绘制棒子
        gotoxy(0, 23);
        for(int i = 0; i < 80; i++)
        {
            System.out.print(" ");
        }

        gotoxy(stick.getX(), stick.getY());
        for(int i = 0; i < stick.getLen(); i++)
        {
            System.out.print("==");
        }
    }
}


 

 

 


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Java 编程语言来实现一个简单的弹球游戏。以下是一个示例代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class BallGame extends JPanel implements ActionListener { private int ballX = 0; private int ballY = 0; private int ballSpeedX = 1; private int ballSpeedY = 1; public BallGame() { Timer timer = new Timer(5, this); timer.start(); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.fillOval(ballX, ballY, 30, 30); } public void actionPerformed(ActionEvent e) { if (ballX < 0 || ballX > getWidth() - 30) { ballSpeedX = -ballSpeedX; } if (ballY < 0 || ballY > getHeight() - 30) { ballSpeedY = -ballSpeedY; } ballX += ballSpeedX; ballY += ballSpeedY; repaint(); } public static void main(String[] args) { JFrame frame = new JFrame("Ball Game"); frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BallGame game = new BallGame(); frame.add(game); frame.setVisible(true); } } ``` 以上代码创建了一个 `BallGame` 类,继承自 `JPanel` 并实现了 `ActionListener` 接口。在 `paint` 方法中,我们绘制了一个红色的圆球。在 `actionPerformed` 方法中,我们更新球的位置,并重新绘制。 在 `main` 方法中,我们创建了一个 `JFrame` 并将 `BallGame` 对象添加到其中,然后显示窗口。 这个简单的例子实现了一个可以在窗口中上下左右移动的圆球。你可以根据自己的需求添加更多的功能,比如碰撞检测、得分等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值