目录

• 游戏规则
• 主要实现方法
• 游戏流程展示
• 1. 开始游戏页面
• 2. 加载中页面
• 3. 选择地图页面
• 4. 自定义昵称页面
• 5. 运行页面
• 死亡页面
• 发展方向
• (前面都不感兴趣，快进到)原码
• 最后

Java课大作业消息出来的时候我脑子一片空白，连Hello World都不会输出，让用Java写个小游戏，七八个星期以来老师上课给我们讲的内容我一点也没印象，莫名有种给你一盒擦炮去炸碉堡的感觉哦。

ddl最后那三天，着实难受>_<…。

主要实现方法

`public class Ball {  private double x;     //绘制横坐标  private double y;     //绘制纵坐标  private double d;     //直径  private double real_x;    //中心横坐标  private double real_y;    //中心纵坐标  private double speed;   //速度  private double degree;    //角度  private double m;     //质量  private String name;    //昵称  private boolean alive;    //存活  private Color owncolor;   //颜色  private BufferedImage flag; //国家国旗  public Ball(double x, double y, double d) {    this.setX(x);    this.setY(y);    this.setD(d);    this.setOwncolor(randomcolor());    this.real_x = x + d / 2;    this.real_y = y + d / 2;    this.alive = true;  }  public Ball(double x, double y, double speed, double degree, double m) {    this.setX(x);    this.setY(y);    this.speed = speed;    this.degree = degree;    this.m = m;    FreshD();    this.alive = true;  }  public Ball(double x, double y, double speed, double degree, double m, String name) {    super();    this.x = x;    this.y = y;    this.speed = speed;    this.degree = degree;    this.m = m;    this.name = name;    this.setOwncolor(randomcolor());    FreshD();    this.alive = true;  }    public BufferedImage getFlag() {    return flag;  }  public void setFlag(BufferedImage flag) {    this.flag = flag;  }  public boolean isAlive() {    return alive;  }  public void setAlive(boolean alive) {    this.alive = alive;  }  public double getSpeed() {    return speed;  }  public void setSpeed(double speed) {    this.speed = speed;  }  public double getDegree() {    return degree;  }  public void setDegree(double degree) {    this.degree = degree;  }  public double getM() {    return m;  }  public void setM(double m) {    this.m = m;  }  public String getName() {    return name;  }  public void setName(String name) {    this.name = name;  }  public double getX() {    return x;  }  public void setX(double x) {    this.x = x;  }  public double getY() {    return y;  }  public void setY(double y) {    this.y = y;  }  public double getD() {    return d;  }  public void setD(double d) {    this.d = d;  }  public double getReal_x() {    return real_x;  }  public void setReal_x(double real_x) {    this.real_x = real_x;  }  public double getReal_y() {    return real_y;  }  public void setReal_y(double real_y) {    this.real_y = real_y;  }  public Color getOwncolor() {    return owncolor;  }  public void setOwncolor(Color owncolor) {    this.owncolor = owncolor;  }  public void draw(Graphics g) {    Color c = g.getColor();    g.setColor(Color.cyan);    g.fillOval((int) x, (int) y, (int) d, (int) d);    g.setColor(c);  }  public void draw(Graphics g, int windowx, int windowy) {  //绘制自身    Color c = g.getColor();    g.setColor(Color.cyan);    g.fillOval((int) (x - windowx), (int) (y - windowy), (int) d, (int) d);    g.setColor(c);  }  public void move() {          //移动小球    if (x > ballfight.Width || x < 0)      degree = Math.PI - degree;    if (y > ballfight.Height || y < 0)      degree = -degree;    while (degree < 0)      degree += 2 * Math.PI;    while (degree > 2 * Math.PI)      degree -= 2 * Math.PI;    if (speed > 0) {      x += speed * Math.cos(degree);      y += speed * Math.sin(degree);    }  }  public void eat(Ball b) {       //吃掉别的Ball类    m += b.m;    FreshD();  }  public void FreshD() {          //刷新小球中心位置    d = 30 + Math.sqrt(m) * 4;    real_x = x + d / 2;    real_y = y + d / 2;  }  public Color randomcolor() {          Random rand = new Random();    float r = rand.nextFloat();    float s = rand.nextFloat();    float t = rand.nextFloat();    return (new Color(r, s, t));  }}`

Player（玩家）类继承Ball类，但是需要重写draw()方法、move()方法，并添加spore()方法和my_spore_move()方法，具体如下。

`public void move(double mx, double my) {    //移动的边界处理和普通Ball不同    double dis = Math.sqrt((mx - screenx) * (mx - screenx) + (my - screeny) * (my - screeny));    double Degree;    Degree = Math.acos((mx - screenx) / dis);    if (my - screeny < 0) {      Degree = -Degree;    }    setDegree(Degree);    if (dis > 2) {      if (getSpeed() > 0) {        if ((getX() + getSpeed() * Math.cos(getDegree()) < 0            || getX() + getSpeed() * Math.cos(getDegree()) > ballfight.Width)            && (getY() + getSpeed() * Math.sin(getDegree()) < 0                || getY() + getSpeed() * Math.sin(getDegree()) > ballfight.Height)) {        } else if (getX() + getSpeed() * Math.cos(getDegree()) < 0            || getX() + getSpeed() * Math.cos(getDegree()) > ballfight.Width) {          setY(getY() + getSpeed() * Math.sin(getDegree()));          FreshD();        } else if (getY() + getSpeed() * Math.sin(getDegree()) < 0            || getY() + getSpeed() * Math.sin(getDegree()) > ballfight.Height) {          setX(getX() + getSpeed() * Math.cos(getDegree()));          FreshD();        } else {          setX(getX() + getSpeed() * Math.cos(getDegree()));          setY(getY() + getSpeed() * Math.sin(getDegree()));          FreshD();        }      }    }  }  public double getsize(double weight) {    return 10 + Math.sqrt(weight) * 4;  }  public void spore(double speed) {   //吐出孢子    System.out.println(getM());    if (resttime < ballfight.timeperspore / ballfight.breaktime)      resttime++;    else {      resttime = 0;      if (getM() > 2 * ballfight.sporeweight) {        double rx, ry;        Spore s = new Spore(            getReal_x() + getD() * 0.5 * Math.cos(getDegree()) - 0.5 * getsize(ballfight.sporeweight),            getReal_y() + getD() * 0.5 * Math.sin(getDegree()) - 0.5 * getsize(ballfight.sporeweight),            speed, this.getDegree(), ballfight.sporeweight);        s.FreshD();        spores.add(s);        rx = getReal_x();        ry = getReal_y();        double weight = getM();        weight -= s.getM();        setM(weight);        FreshD();        setX(rx - getD() * 0.5);        setY(ry - getD() * 0.5);        FreshD();      }    }  }  public void my_spore_move() {   //所有已吐出孢子运动    for (int i = 0; i < spores.size(); i++) {      Spore s = spores.get(i);      if (s.isAlive()) {        s.move();        s.FreshD();      }    }  }`

`public class ballfight extends JPanel`
`public ballfight(String name) {    LoadImgs();    setBackground(Color.black);    InitialImages();    frame = new JFrame(name);    frame.setVisible(true);    frame.setResizable(false);    frame.add(this);    frame.setBackground(Color.black);    frame.setBounds(this.X, this.Y, ballfight.windowWidth, ballfight.windowHeight);    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);`

`keylistener = new KeyListener() {      /**************/      @Override      public void keyTyped(KeyEvent e) {        // TODO Auto-generated method stub        /*********/      }      @Override      public void keyReleased(KeyEvent e) {        // TODO Auto-generated method stub        /*********/      }      @Override      public void keyPressed(KeyEvent e) {        // TODO Auto-generated method stub        /*********/      }    };frame.addKeyListener(keylistener);`

`game.mouse_adapter = new MouseAdapter() {      @Override      public void mouseClicked(MouseEvent e) {        // TODO Auto-generated method stub      }      @Override      public void mousePressed(MouseEvent e) {        // TODO Auto-generated method stub        /*********/      }    };    game.addMouseListener(mouse_adapter);    game.addMouseMotionListener(new MouseMotionListener() {      @Override      public void mouseMoved(MouseEvent e) {        // TODO Auto-generated method stub        mx = e.getX();        my = e.getY();      }      @Override      public void mouseDragged(MouseEvent e) {        // TODO Auto-generated method stub      }    });`

`public class ParticleMaker implements Runnable {    @Override    public void run() {      // TODO Auto-generated method stub      while (player.isAlive()) {        synchronized (this) {          for (int i = 0; i < particlepertime; i++) {            MakeParticle();          }        }        try {          Thread.sleep(particletime);        } catch (Exception e) {          // TODO: handle exception          e.printStackTrace();        }      }    }  }`

`public class ranking {    Ball[] M = new Ball[10000];    Color Gold = new Color(255, 215, 0);    Color Silver = new Color(192, 192, 192);    Color Copper = new Color(244, 164, 96);    Color MediumOrchid = new Color(186, 85, 211);    Color Lavender = new Color(230, 230, 250);    Color CornflowerBlue = new Color(100, 149, 237);    public Ball[] getM() {      return M;    }    public void setM(Ball[] m) {      M = m;    }    public ranking() {      int j = 0;      for (int i = 0; i < enemy.size(); i++) {        Enemy e = enemy.get(i);        if (e != null && e.isAlive()) {          M[j] = e;          j++;        }      }      M[j] = player;      for (int i = 0; i < ranknum; i++) {        int x = i;        for (int ii = i + 1; ii <= j; ii++) {          if (M[ii].getM() > M[x].getM()) {            x = ii;          }        }        Ball y = M[i];        M[i] = M[x];        M[x] = y;      }    }    public void UpdateRankingList() {      int j = 0;      for (int i = 0; i < enemy.size(); i++) {        Enemy e = enemy.get(i);        if (e != null && e.isAlive()) {          M[j] = e;          j++;        }      }      M[j] = player;      for (int i = 0; i < ranknum; i++) {        int x = i;        for (int ii = i + 1; ii <= j; ii++) {          if (M[ii].getM() > M[x].getM()) {            x = ii;          }        }        Ball y = M[i];        M[i] = M[x];        M[x] = y;      }    }    public void DrawRankingList(Graphics g) {      UpdateRankingList();      g.setColor(MediumOrchid);      g.setFont(new Font("Comic Sans MS", Font.LAYOUT_LEFT_TO_RIGHT, 21));      g.drawString("---Ranking---", windowWidth - 200, 20);      g.setFont(new Font("楷体", Font.LAYOUT_LEFT_TO_RIGHT, 18));      g.drawString("排名", windowWidth - 200, 45);      g.drawString("昵称", windowWidth - 140, 45);      g.drawString("地区", windowWidth - 90, 45);      g.setColor(Gold);      g.setFont(new Font("黑体", Font.LAYOUT_LEFT_TO_RIGHT, 20));      g.drawString(1 + ": ", windowWidth - 200, 70 + fontlineheight * 0);      g.setColor(Color.white);      g.drawString(M[0].getName(), windowWidth - 160, 70 + fontlineheight * 0);      g.drawImage(M[0].getFlag(), windowWidth - 85, 55 + fontlineheight * 0, 27, 18, null);      g.setColor(Silver);      g.setFont(new Font("黑体", Font.LAYOUT_LEFT_TO_RIGHT, 20));      g.drawString(2 + ": ", windowWidth - 200, 70 + fontlineheight * 1);      g.setColor(Color.white);      g.drawString(M[1].getName(), windowWidth - 160, 70 + fontlineheight * 1);      g.drawImage(M[1].getFlag(), windowWidth - 85, 55 + fontlineheight * 1, 27, 18, null);      g.setColor(Copper);      g.setFont(new Font("黑体", Font.LAYOUT_LEFT_TO_RIGHT, 20));      g.drawString(3 + ": ", windowWidth - 200, 70 + fontlineheight * 2);      g.setColor(Color.white);      g.drawString(M[2].getName(), windowWidth - 160, 70 + fontlineheight * 2);      g.drawImage(M[2].getFlag(), windowWidth - 85, 55 + fontlineheight * 2, 27, 18, null);      for (int i = 3; i < ranknum; i++) {        g.setColor(CornflowerBlue);        g.setFont(new Font("黑体", Font.LAYOUT_LEFT_TO_RIGHT, 20));        g.drawString(i + 1 + ": ", windowWidth - 200, 70 + fontlineheight * i);        g.setColor(Color.white);        g.drawString(M[i].getName(), windowWidth - 160, 70 + fontlineheight * i);        g.drawImage(M[i].getFlag(), windowWidth - 85, 55 + fontlineheight * i, 27, 18, null);      }    }  }`