zoukankan      html  css  js  c++  java
  • 项目实战之天天酷跑(五):结束界面【完结篇】

    本文摘自:https://blog.csdn.net/qq_45909299/article/details/110881446

    项目源码及相关图片资源,已上传至GitHub:https://github.com/HueyM/Projects

    接上文,本文将实现天天酷跑游戏的结束界面,功能如下:

    跑酷距离、获取玩家的得分。
    再来一次、返回主菜单、直接退出。

    具体啥样子,先睹为快!

    在这里插入图片描述

    点击再来一次按钮,进入加载状态,加载结束,直接进入游戏。

    在这里插入图片描述

    点击主菜单按钮,进入主菜单界面:

    在这里插入图片描述

    一、跑酷距离

    我是在Person类的玩家移动方法中,添加了一个自增的diatance,只要玩家的图片还在切换,也就是游戏还没有结束,这个distance都在自增,也算是一种间接的实现计算跑酷距离的方法。
    在这里插入图片描述
    通过在Person类中添加get、set方法,获取数据。
    在这里插入图片描述

    二、获取玩家的得分

    玩家与金币碰撞的得分即为图中的表现分,在GamePanel 获取。
    在这里插入图片描述
    而总分,我在Person类中,设定了一个简单的计分规则:
    在这里插入图片描述

    三、再来一次

    在鼠标点击事件内,new一个新的加载界面,加载完成后自动进入游戏。
    在这里插入图片描述

    四、返回主界面

    同理。
    在这里插入图片描述

    五、直接退出

    同理。
    在这里插入图片描述

    上代码

    EndFrame.java

    package cn.sqc.runday.view;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    import cn.sqc.runday.controller.GamePanel;
    import cn.sqc.runday.model.Person;
    
    
    public class EndFrame extends JFrame implements MouseListener {
        //创建继续游戏按钮、返回主菜单按钮、退出按钮 组件
            JLabel again,back,exit;
            
        public EndFrame(Person person) { 
            again = new JLabel(new ImageIcon("Image/hh5.png"));
            again.setBounds(520, 622, 60, 25);
            again.addMouseListener(this);
            this.add(again);    
            back = new JLabel(new ImageIcon("Image/hh6.png"));
            back.setBounds(520, 722, 60, 25);
            back.addMouseListener(this);
            this.add(back);
            exit = new JLabel(new ImageIcon("Image/hh3.png"));
            exit.setBounds(520, 822, 60, 25);
            exit.addMouseListener(this);
            this.add(exit);
            
            EndPanel end = new EndPanel(person);
            this.add(end);//将结束面板组件添加到结束窗口上
            
            this.setSize(1500, 900);
            this.setLocationRelativeTo(null);
            this.setUndecorated(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setIconImage(new ImageIcon("Image/115.png").getImage());
            this.setVisible(true);
        }
        
        public static void main(String[] args) { 
            //new EndFrame();
        }
        class EndPanel extends JPanel{
            Image background;
            Person p;
            public EndPanel(Person person) {//类比int a
                this.p = person;//创建对象、传值
                try {
                    background = ImageIO.read(new File("Image/chou.png"));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            @Override
            public void paint(Graphics g) {
                // TODO Auto-generated method stub
                super.paint(g);
                g.drawImage(background, 0, 0,1500,900 ,null);
                g.setColor(Color.CYAN);
                g.setFont(new Font("宋体",Font.BOLD,30));
                g.drawString(p.getScore()+"",1110,705);// + ” “ 属实妙
                g.drawString(p.getDistance() + " ", 1110, 622);
                
                g.setFont(new Font("宋体",Font.BOLD,50));
                g.setColor(Color.ORANGE);
                g.drawString(p.getTotalScore() + "", 1075, 500);
            }
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            if(e.getSource().equals(again)){
                //跳转到下一界面    
                 new WindowFrame().Start();
                //关闭当前界面
                 dispose();
            }    else if(e.getSource().equals(back)){
                new MainFrame();
                dispose();
            }else if(e.getSource().equals(exit)){
                System.exit(0);
            }
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
        
    }
    View Code
    I have a dream : Sandy beach B-J-N.
  • 相关阅读:
    2013年工作中遇到的20个问题:81-100
    2013年工作中遇到的20个问题:81-100
    码农:客户是恶魔
    码农:客户是恶魔
    C# DataGridView 使用
    Java实现 LeetCode 203 移除链表元素
    Java实现 LeetCode 203 移除链表元素
    Java实现 LeetCode 202 快乐数
    Java实现 LeetCode 202 快乐数
    Java实现 LeetCode 202 快乐数
  • 原文地址:https://www.cnblogs.com/mjtabu/p/14380850.html
Copyright © 2011-2022 走看看