java实现弹球小游戏

GUI实现弹球小游戏,供大家参考,具体内容如下

先看一下游戏效果图。

java实现弹球小游戏_第1张图片

java实现弹球小游戏_第2张图片

一个简单的Demo。也比较简单,新手试着做一做完善改进。

源代码

import Com.Style.FontStyle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 * @Author: 冀十三
 * @DescIption: 弹球小游戏
 * @Date:2021--06--10--17:08
 */
public class Demo  extends JFrame {
    //创建游戏窗口
    public Demo() {
        init();
    }

    //桌面大小
    private final int TABLE_WIDTH = 450;
    private final int TABLE_HEIGHT = 600;
    //球大小
    private final int BALL_SIZE = 15;

    //球拍大小
    private final int RACKET_WIDTH = 120;
    private final int RACKET_HEIGHT = 20;
    //记录小球坐标
    private double BallX = 100;
    private double BallY = 200;
    //记录球拍坐标
    private double RACKET_X = 150;
    private final double RACKET_Y = 380;
    //记录小球速度
    private double speedX = 10;
    private double speedY = 20;
    //记录游戏是否结束
    private boolean isOver = false;
    //声明一个积分器
    int score= 0;


    //声明一个计时器。

    ActionListener run = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //更新小球坐标让小球动起来
            BallY+=speedY;
            BallX+=speedX;
            //重绘界面
            mycanvas.repaint();
            //碰到两边的时候
            if (BallX<=0||BallX>=TABLE_WIDTH-BALL_SIZE){
                speedX=-speedX;
            }
            //碰到上边界的时候
            if(BallY<=0){
                speedY=-speedY;
            }
            //接住小球的时候
            if ( BallY>=(RACKET_Y-BALL_SIZE)&&BallX>RACKET_X&&BallX<(RACKET_X+RACKET_WIDTH)){
                //得10分
                score+=10;
                speedY=-speedY;

            }
            if ((BallY>(RACKET_Y-BALL_SIZE)&&(BallXRACKET_X+RACKET_WIDTH))){
                timer.stop();
                isOver=true;
                mycanvas.repaint();
            }
        }
    }

        ;
        private Timer timer = new Timer(60, run);

        //内部类继承Canvas充当画布
        private class myCanvas extends Canvas {
            public void paint(Graphics g) {
                //绘制内容
                //如果游戏结束
                if (isOver) {
                    //给画笔颜色
                    g.setColor(new Color(0xFF9A1B44, true));
                    //给画笔字体
                    g.setFont(new Font("宋体", FontStyle.BOLD, 25));
                    g.drawString("Game Over!", 120, TABLE_HEIGHT / 4);
                    g.drawString("成绩"+score+"分", 120, TABLE_HEIGHT / 3);
                    g.drawString("按空格再来一次", 120, TABLE_HEIGHT / 2);
                } else {
                    //否则游戏中
                    //绘制小球

                    g.setColor(new Color(0xFF09FAA6, true));
                    g.fillOval((int) BallX, (int) BallY, BALL_SIZE, BALL_SIZE);
                    //画一个积分系统
                    //上色
                    g.setColor(new Color(0xE8DC0C77,true));
                    //给画笔字体
                    g.setFont(new Font("宋体", FontStyle.BOLD, 25));
                    g.drawString("得分:"+score, 25, 25);

                    //绘制球拍
                    g.setColor(new Color(0xFF0E51D7, true));
                    g.fillRect((int) RACKET_X, (int) RACKET_Y, RACKET_WIDTH, RACKET_HEIGHT);
                    timer.start();

                }
            }
        }

        //创建绘画区
        myCanvas mycanvas = new myCanvas();


        //窗口初始化
        public void init() {

            JFrame jFrame = new JFrame();
            jFrame.setTitle("弹球小游戏");//窗口名称
            jFrame.pack();//自适应布局
            jFrame.setVisible(true);//可见
            jFrame.add(mycanvas);
            jFrame.setBounds(500, 200, TABLE_WIDTH, TABLE_HEIGHT);
            jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//关闭
            mycanvas.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));//画布大小
            jFrame.setResizable(false);//大小不可变


            KeyListener keyListener = new KeyAdapter() {
                //重写键盘被按压的方法
                @Override
                public void keyPressed(KeyEvent e) {
                    //接收按下的是哪个键
                    int key = e.getKeyCode();
                    if (key == KeyEvent.VK_LEFT) {
                        if (RACKET_X >0) {
                            RACKET_X -= 30;
                        }
                    }
                    if (key == KeyEvent.VK_RIGHT) {
                        if (RACKET_X < (TABLE_WIDTH - RACKET_WIDTH )) {
                            RACKET_X += 30;
                        }
                    }
                    if (key == KeyEvent.VK_SPACE) {
                        if (isOver=true) {
                            Demo.main(null);

                        }
                    }
                }

            };

            //窗口获取键盘监听
            jFrame.addKeyListener(keyListener);
            //键盘获取键盘监听
            mycanvas.addKeyListener(keyListener);


        }

        public static void main(String[] args) {
            new Demo();

        }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(java实现弹球小游戏)