Java球球大作战小游戏源码(完整版)

球球大作战小游戏完整源码分享,可以当作Java入门课程设计作品

涉及知识点:文件IO流、多线程、面向对象、Java基础语法等知识

一、游戏设计思想:

(1)在一张地图上生成若干个小球以及玩家小球,有若干个地图可供选择。

(2)玩家通过移动鼠标进行移动,可停止移动,吐孢子,

(3)游戏期间通过Ranking排行榜实时更新小球排名,

(4)所有Ai小球都会想着吃掉其他小球,成为唯一的胜利者。

(5)当玩家小球被吃掉即游戏结束,但是可以通过重新开始,继续进行游戏。

(6)ai自动追逐算法 (自动追逐比自己小的球,自动躲避比自己大的球)

结尾附完整可直接运行代码百度网盘

二、 类及部分代码展示:

Ball.java------小球类

ballfight.java------游戏主实现类------启动类

Enemy.java------敌人小球类

MyMouse.java------鼠标控制类

Name.java------名字类

Particle.java------绘制辅助类

Player.java------玩家类

PlayMusic.java------音乐播放

Ranking.java------排名类

Spore.java------吐孢子类

2.1背景音乐播放代码

java 复制代码
package ballf;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class PlayMusic {

	public static void playBackgroundMusic() {
        try {
            // 获取音频文件的URL
            URL url = PlayMusic.class.getResource("/Music/bgmusic.wav");
            // 打开音频输入流
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
            // 获取一个音频剪辑
            Clip clip = AudioSystem.getClip();
            // 打开剪辑并加载样本
            clip.open(audioIn);
            // 设置剪辑循环播放
            clip.loop(Clip.LOOP_CONTINUOUSLY);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }
}

2.2UI界面绘制代码

java 复制代码
public void paint(Graphics g){
    super.paint(g);
      if (this.state == 0){
      g.drawImage(this.mainui, -30, 0, (int)(windowWidth * 1.2D), (int)(windowHeight * 1.1D), null);
      g.setColor(Color.BLACK);
      g.setFont(new Font("楷体", 1, 50));
      g.drawString("开始游戏", windowWidth / 2 - 155, windowHeight / 2);
      g.drawRect(windowWidth / 2 - 155, windowHeight / 2 - 45, 210, 50);
    }
    if (this.state == 1){
      g.drawImage(this.waiting, 0, 0, (int)(windowWidth * 1.1D), windowHeight, null);
      g.setColor(Color.magenta);
      g.setFont(new Font("Comic Sans MS", 0, 90));
      g.drawString("Loading......", 500, 600);
      if (this.pause > 320){
        this.state = 2;
        this.choosemap = 0;
        for (int i = 0; i < 7; i++) {
          if (this.choosemap == i) {
            this.map[i] = Color.red;
          } else {
            this.map[i] = Color.blue;
          }
        }
      }
      else if (this.pause == 10){
        Loadbg loadbg = new Loadbg();
        new Thread(loadbg).start();
        this.pause += 1;
      }else{
        this.pause += 1;
      }
    }

2.3小球类

java 复制代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

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(ballfight.state!=4) return;//移动小球
		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));
	}
}

2.4Spore类

java 复制代码
package ballf;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.PrintStream;

public class Spore
  extends Ball{
  public Spore(double x, double y, double speed, double degree, double m){
    super(x, y, speed, degree, m);
  }
  
  public void draw(Graphics g){
    Color c = g.getColor();
    g.setColor(Color.green);
    g.fillOval((int)getX(), (int)getY(), (int)getD(), (int)getD());
    g.setColor(c);
  }
  
  public BufferedImage getFlag(){
    return super.getFlag();
  }
  
  public void setFlag(BufferedImage flag){
    super.setFlag(flag);
  }
  
  public boolean isAlive() {
    return super.isAlive();
  }
  
  public void setAlive(boolean alive){
    super.setAlive(alive);
  }
  
  public double getSpeed(){
    return super.getSpeed();
  }
  
  public void setSpeed(double speed){
    super.setSpeed(speed);
  }
  
  public double getDegree(){
    return super.getDegree();
  }
  
  public void setDegree(double degree){
    super.setDegree(degree);
  }
  
  public double getM(){
    return super.getM();
  }
  
  public void setM(double m){
    super.setM(m);
  }
  
  public String getName(){
    return super.getName();
  }
  
  public void setName(String name){
    super.setName(name);
  }
  
  public double getX(){
    return super.getX();
  }
  
  public void setX(double x){
    super.setX(x);
  }
  
  public double getY(){
    return super.getY();
  }
  
  public void setY(double y){
    super.setY(y);
  }
  
  public double getD(){
    return super.getD();
  }
  
  public void setD(double d){
    super.setD(d);
  }
  
  public double getReal_x(){
    return super.getReal_x();
  }
  
  public void setReal_x(double real_x){
    super.setReal_x(real_x);
  }
  
  public double getReal_y(){
    return super.getReal_y();
  }
  
  public void setReal_y(double real_y){
    super.setReal_y(real_y);
  }
  
  public Color getOwncolor(){
    return super.getOwncolor();
  }
  
  public void setOwncolor(Color owncolor){
    super.setOwncolor(owncolor);
  }
  
  public void eat(Ball b){
    super.eat(b);
  }
  
  public void FreshD(){
    super.FreshD();
  }
  
  public Color randomcolor(){
    return super.randomcolor();
  }
  
  public int hashCode(){
    return super.hashCode();
  }
  
  public boolean equals(Object obj){
    return super.equals(obj);
  }
  
  protected Object clone()
    throws CloneNotSupportedException{
    return super.clone();
  }
  
  public String toString(){
    return super.toString();
  }
  
  protected void finalize()
    throws Throwable{
    super.finalize();
  }
  
  public void draw(Graphics g, int windowx, int windowy) {
    Color c = g.getColor();
    g.setColor(Color.green);
    g.fillOval((int)(getX() - windowx), (int)(getY() - windowy), (int)getD(), (int)getD());
    g.setColor(c);
  }
  
  public void move(){
    super.move();
    if (getSpeed() > 0.0D) {
      setSpeed(getSpeed() - 0.1D);
    }
  }
  
  public boolean can_eat(){
    System.out.println(getSpeed() < 1.0D);
    return getSpeed() < 1.0D;
  }
}

百度网盘链接:https://pan.baidu.com/s/1MIwhVQYLT1QAwUl20-AA_g?pwd=wgf8

提取码:wgf8

相关推荐
IT技术分享社区7 分钟前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
极客代码10 分钟前
【Python TensorFlow】入门到精通
开发语言·人工智能·python·深度学习·tensorflow
疯一样的码农16 分钟前
Python 正则表达式(RegEx)
开发语言·python·正则表达式
代码之光_198017 分钟前
保障性住房管理:SpringBoot技术优势分析
java·spring boot·后端
ajsbxi23 分钟前
苍穹外卖学习记录
java·笔记·后端·学习·nginx·spring·servlet
清涔秋风33 分钟前
自动驾驶革命:从特斯拉到百度,谁将主宰未来交通?
其他
&岁月不待人&38 分钟前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin
StayInLove41 分钟前
G1垃圾回收器日志详解
java·开发语言
对许1 小时前
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“
java·log4j
无尽的大道1 小时前
Java字符串深度解析:String的实现、常量池与性能优化
java·开发语言·性能优化