千家信息网

Java如何实现接月饼小游戏

发表于:2024-11-23 作者:千家信息网编辑
千家信息网最后更新 2024年11月23日,这篇文章给大家分享的是有关Java如何实现接月饼小游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。主要设计设计游戏界面,用swing实现设计背景设计得分物体-月饼,碰到加
千家信息网最后更新 2024年11月23日Java如何实现接月饼小游戏

这篇文章给大家分享的是有关Java如何实现接月饼小游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

    主要设计

    • 设计游戏界面,用swing实现

    • 设计背景

    • 设计得分物体-月饼,碰到加一分

    • 设计障碍物-月亮,碰到会死亡

    • 监听鼠标的左右键,用来控制篮子左右移动

    • 设计积分系统

    • 将resource文件夹设为resource(Project Manage中可以设置),因为要用里面的图片

    功能截图

    游戏开始界面:

    代码实现

    游戏启动类

    public class Start {    public static void main(String[] args) throws URISyntaxException {        new EatGame();    }}

    核心类

    public class EatGame extends JFrame implements ActionListener, MouseListener{    private final int WIDTH = 400;    private final int HEIGHT = 800;    private int score;    private int ticks;     private boolean gameOver = false;    private GameControl control;     private List moons;    private List cakes;    private Timer timer;    private ChangeE changeE;    /**     * Constructor for objects of class FallingGame     */    public EatGame() throws URISyntaxException {        changeE = new ChangeE(150, 650, "player.jpg", WIDTH);        moons = new ArrayList<>();        cakes = new ArrayList<>();        addMoonAndCake();                control = new GameControl(changeE, moons, cakes);        timer = new Timer(20, this);                 //add keybinds        control.addAction("Left", -20, KeyEvent.VK_LEFT);        control.addAction("Right", 20, KeyEvent.VK_RIGHT);                //add components        add(control);        addMouseListener(this);        control.addMouseListener(this);        setTitle("吃月饼");        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setSize(WIDTH, HEIGHT);        setResizable(false);        setVisible(true);               timer.start();    }    /**     * 添加月亮和月饼     */    public void addMoonAndCake() {        Random r = new Random();        int x, y;        x = 60 + (r.nextInt(35) * 2);        y = r.nextInt(20) + moons.size() * 30;        Moon moon1 = new Moon(x, y, "moon.png");        moons.add(moon1);        x = 190 + r.nextInt(35) * 2;        y = r.nextInt(20) + moons.size() * 30;        Moon moon2 = new Moon(x, y, "moon.png");        moons.add(moon2);        x = 80 + (r.nextInt(35) * 2);        y = r.nextInt(20) + moons.size() * 50;        Cake cake = new Cake(x, y, "cake.png");        cakes.add(cake);    }    /**     * 更新窗口     */    private void updateFrame() {        ticks++;        for(int i = 0; i < moons.size(); i++)        {            Moon moon = moons.get(i);            if(ticks % 25 == 0 && moon.getSpeed() < 10)            {                moon.setSpeed(moon.getSpeed() + 2);            }        }        Iterator moonIterator = moons.iterator();        while (moonIterator.hasNext()) {            Moon moon = moonIterator.next();            // 超出屏幕            if(moon.getY() > HEIGHT) {                moonIterator.remove();            } else                moon.move();        }        Iterator cakeIterator = cakes.iterator();        while (cakeIterator.hasNext()) {            Cake cake = cakeIterator.next();            // 超出屏幕            if(cake.getY() > HEIGHT) {                cakeIterator.remove();            } else                cake.move();        }        if(moons.size() == 0) {            addMoonAndCake();        }    }    private boolean checkCollision() {        Rectangle rectangle = (Rectangle) changeE.getShape();        for(Moon moon : moons) {            Ellipse2D circle = (Ellipse2D) moon.getShape();            // 判断是否与圆形相撞            if (circle.intersects(rectangle)) {                gameOver = true;            }        }        Iterator cakeIterator = cakes.iterator();        while (cakeIterator.hasNext()) {            Cake cake = cakeIterator.next();            Ellipse2D circle = (Ellipse2D) cake.getShape();            if (circle.intersects(rectangle)) {                score ++;   // 得分                cakeIterator.remove();            }        }        return gameOver;    }    public void actionPerformed(ActionEvent e)    {        if(gameOver) {            timer.stop();             control.drawEnd(control.getGraphics(), score);        } else {            //continue with game            updateFrame();            checkCollision();            control.repaint();         }    }    public void mouseClicked(MouseEvent e) {    }    public void mousePressed(MouseEvent e)    {        if(gameOver)        {            //reset game            moons.clear();            control.removeAll();             control.updateUI();             score = 0;            changeE.setX(150);            changeE.setY(650);            addMoonAndCake();            timer.start();            repaint();            gameOver = false;         }    }    public void mouseReleased(MouseEvent e)    {            }    public void mouseEntered(MouseEvent e)    {            }    public void mouseExited(MouseEvent e)    {            }}

    画面绘制

    /** * 画面绘制 */public class GameControl extends JPanel{    private InputMap inputMap;     private ChangeE myChangeE;    private List moons;    private List cakes;    public GameControl(ChangeE changeE, List moons, List cakes)    {        this.myChangeE = changeE;        this.moons = moons;        this.cakes = cakes;        setBackground(Color.white);        inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);      }    @Override    protected void paintComponent(Graphics g)    {        super.paintComponent(g);        // 绘制背景        ImageIcon icon=new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("background.png"));        Image img=icon.getImage();        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);        // 绘制玩家        g.drawImage(myChangeE.getImage(), myChangeE.getX(), myChangeE.getY(), this);        // 绘制月亮        for(Moon moon : moons) {            g.drawImage(moon.getImage(), moon.getX(), moon.getY(), this);        }        // 绘制月饼        for (Cake cake : cakes) {            g.drawImage(cake.getImage(), cake.getX(), cake.getY(), this);        }            }    public void drawEnd(Graphics g, int score)    {        g.setColor(Color.WHITE);        g.fillRect(50, 200, 300, 300);        g.setColor(Color.RED);        g.drawString("祝你中秋快乐,阖家欢乐!", 100, 300);        g.setColor(Color.BLUE);        g.drawString("您吃的到的月饼个数为:" + score, 100, 350);        g.setColor(Color.BLACK);        g.drawString("鼠标点击重玩", 100, 400);    }    public void addAction(String name, int deltaX, int keyCode) {        MoveAction moveAction = new MoveAction(name, deltaX);        inputMap.put(KeyStroke.getKeyStroke(keyCode, 0), name);        getActionMap().put(name, moveAction);    }     private class MoveAction extends AbstractAction implements ActionListener {        private int myDeltaX;        public MoveAction(String name, int deltaX) {            super(name);            myDeltaX = deltaX;        }        public void actionPerformed(ActionEvent e) {            myChangeE.move(myDeltaX);        }    }}

    感谢各位的阅读!关于"Java如何实现接月饼小游戏"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

    0