Java弹球

本文介绍了一款使用Java编写的弹球游戏的开发过程。通过整合线程画图和数组列表知识,实现了小球的随机生成、移动及碰撞检测等功能。文章详细描述了小球类的属性与方法,以及如何通过鼠标监听器和线程实现实时绘图和多球碰撞。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java弹球

前面的博客中讲了线程画图和数组列表,对这类的知识有了进一步的理解,这里将之前的知识进行了整合,为之后的编小游戏做准备。

小球类

首先,我们先确定我们需要的小球具有什么样的属性,比如大小,速度,颜色等;同时呢,它也需要实现一些功能,如移动,检测碰撞等,我们将这些属性和功能整合在一个小球类中

package Ball;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Ball {

	private int size, x, y, speedx, speedy;

	private Color color;
	public Ball(int x, int y) {
		super();
		this.x = x;
		this.y = y;
		Random rand = new Random();
		size = rand.nextInt(50) + 20;
		speedx = rand.nextInt(10) - 5;
		speedy = rand.nextInt(10) - 5;
		color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
	}
	public void draw(Graphics g) {
		g.setColor(color);
		g.fillOval(x, y, size, size);
		move();
	}
	public void move() {
		x += speedx;
		y += speedy;
	}
	public boolean isHit(Ball other) {
		double x11 = this.x + this.size / 2;
		double y11 = this.y + this.size / 2;
		double x22 = other.x + other.size / 2;
		double y22 = other.y + other.size / 2;
		double length = Math.sqrt((x11 - x22) * (x11 - x22) + (y11 - y22) * (y11 - y22));
		double length2 = (this.size + other.size) / 2;
		if (length <= length2) {
			return true;
		} else {
			return false;
		}
	}
	public int isHit2() {
		if (this.x <= 0 || this.x + size >= 800) {
			return 1;
		} else if (this.y <= 0 || this.y + size >= 500) {
			return 2;
		} else
			return 0;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	
	public int getspeedx() {
		return speedx;
	}
	
	public int getspeedy() {
		return speedy;
	}
	
	public void setSpeed(int speedx, int speedy) {
		this.speedx = speedx;
		this.speedy = speedy;
	}

具体实现

接着就是要把这个小球画出来了,我的想法是通过鼠标监听器点击画板,监听器事件e获取鼠标所点位置的坐标,并将其放进Arraylist中,第一次点击时同时开启线程,之后每次点击将后面的坐标加进数组列表中,来实现弹球的画面。

UI

package Ball;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

public class UI {
	public static void main(String[] args) {
	UI ui = new UI();
	ui.showUI();
	}

	public void showUI() {
	JFrame jf = new JFrame();
	jf.setTitle("画板");
	jf.setSize(800, 500);
	jf.setResizable(false);
	jf.setDefaultCloseOperation(3);
	jf.setVisible(true);
	jf.setLocationRelativeTo(null);
	Graphics g = jf.getGraphics();
	List<Ball> data = new ArrayList();
	BallListener bl = new BallListener(data, g);
	jf.addMouseListener(bl);
	}
}

监听器

package Ball;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

public class BallListener implements MouseListener {
	Graphics g;
	List<Ball> data;
	
	public BallListener(List<Ball> data, Graphics g) {
		super();
		this.data = data;
		this.g = g;
	}
	public int x, y;
	public void mouseClicked(MouseEvent e) {
	
 	}

	public void mousePressed(MouseEvent e) {
	
	}

	public void mouseReleased(MouseEvent e) {
		x = e.getX();
		y = e.getY();
		Ball b = new Ball(x,y);
		data.add(b);
		if(data.size()==1){
		Run r  = new Run(data,g);
		r.start();
		}
	}
	public void mouseEntered(MouseEvent e) {
	
	}
	public void mouseExited(MouseEvent e) {
	
	}

线程

package Ball;

import java.awt.Color;
import java.awt.Graphics;
import java.util.List;
import java.util.Random;

public class Run extends Thread {

	Graphics g;
	List<Ball> data;
	Random rand = new Random();

	public Run(List<Ball> data, Graphics g) {
		super();
		this.data = data;
		this.g = g;	
	}
 
	public void run() {
		while (true) {
			g.setColor(new Color(238, 238, 238));
			g.fillRect(0, 0, 800, 500);
			for (int i = 0; i < data.size(); i++) {
				data.get(i).draw(g);
			}
			try {
				sleep(40);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			for (int i = 0; i < data.size(); i++) {
				for (int j = i + 1; j < data.size(); j++) {
					if (data.get(i).isHit(data.get(j))) {
						data.get(i).setSpeed(-data.get(i).getspeedx(), -data.get(i).getspeedy());
						data.get(j).setSpeed(-data.get(j).getspeedx(), -data.get(j).getspeedy());
					}
				}
			}
			for (int i = 0; i < data.size(); i++) {
				if (data.get(i).isHit2() == 1) {
					data.get(i).setSpeed(-data.get(i).getspeedx(), data.get(i).getspeedy());
				} else if (data.get(i).isHit2() == 2) {
					data.get(i).setSpeed(data.get(i).getspeedx(), -data.get(i).getspeedy());
				}
			}
		}
	}
}

效果

在这里插入图片描述

package org.crazyit.ball; import java.awt.Image; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; /** * 小球对象 * * @author yangenxiong yangenxiong2009@gmail.com * @author Kelvin Mak kelvin.mak125@gmail.com * @version 1.0 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br>Copyright (C), 2009-2010, yangenxiong * <br>This program is protected by copyright laws. */ public class Ball extends BallComponent { // 定义球的竖向速度 private int speedY = 10; // 定义弹球的横向速度 private int speedX = 8; // 定义是否在运动 private boolean started = false; // 定义是否结束运动 private boolean stop = false; /** * m 有参数构造器 * * @param panelWidth * int 画板宽度 * @param panelHeight * int 画板高度 * @param offset * int 位移 * @param path * String 图片路径 */ public Ball(int panelWidth, int panelHeight, int offset, String path) throws IOException { // 调用父构造器 super(panelWidth, panelHeight, path); // 设置y坐标 this.setY(panelHeight - super.getImage().getHeight(null) - offset); } /** * 设置横向速度 * * @param speed * int 速度 * @return void */ public void setSpeedX(int speed) { this.speedX = speed; } /** * 设置竖向速度 * * @param speed * int 速度 * @return void */ public void setSpeedY(int speed) { this.speedY = speed; } /** * 设置是否在运动 * * @param b * boolean * @return void */ public void setStarted(boolean b) { this.started = b; } /** * 设置是否结束运动 * * @param b * boolean * @return void */ public void setStop(boolean b) { this.stop = b; } /** * 返回横向速度 * * @return int 速度 */ public int getSpeedX() { return this.speedX; } /** * 返回竖向速度 * * @return int 速度 */ public int getSpeedY() { return this.speedY; } /** * 是否在运动 * * @return boolean 是否在运动 */ public boolean isStarted() { return this.started; } /** * 是否已经结束运动 * * @return boolean 是否已经结束运动 */ public boolean isStop() { return this.stop; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值