记录一个咸鱼大学生三个月的奋进生活021
复习Java小球游戏
刚好上一次我们才复习完多线程的知识,这次就来实践一下
做一个基于Java多线程,Javaswing,random包 的小球游戏
就是类似以前电脑屏保的那个泡泡碰撞游戏,一看就能看一天的那种
我们也来做个多个小球(多线程)同时在界面(swing)中乱撞(random)的游戏
游戏界面的代码
还是先从这个游戏的窗体开始写
package com.javawork28;
// 这是小球弹跳的窗体
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BallPanel extends JFrame {
private JPanel mainPanel = new JPanel(); // 小球弹跳的panel
private JPanel operatePanel = new JPanel(); // 下方按钮的panel
public BallPanel() {
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
this.mainPanel.setBackground(Color.BLACK); // 将mainPanel的背景颜色设为黑色
c.add(this.mainPanel, BorderLayout.CENTER); // 将mainPanel添加进ContentPane实例化c
JButton b1 = new JButton("开始");
// 给开始按钮设置监听器直接在addActionListener()方法中实例化ActionListener添加方法,实例化小球Ball,执行线程的start方法
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Ball b = new Ball(mainPanel);
b.start();
}
});
JButton b2 = new JButton("退出");
// 给开始按钮设置监听器直接在addActionListener()方法中实例化ActionListener添加方法
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// 将两个按钮添加进底部按钮panel
this.operatePanel.add(b1);
this.operatePanel.add(b2);
c.add(this.operatePanel, BorderLayout.SOUTH);
this.setBounds(650, 200, 600, 600);
this.setTitle("小球游戏 by wyh");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
小球运动线程的代码
然后是小球运动的代码
要点:
① 首先这个游戏是可以有多个小球一起运动的,所以代码要继承Thread实现多线程,然后重写run方法来实现小球运动功能
② dx和dy变量是小球的移动量,他每次会被random包赋予不同的数值,这个是不同小球走的路径不同,速度不同的关键
③ 小球移动的原理是线程每隔一段很短的时间(30毫秒)就刷新一次,每次刷新就会移动dx和dy的距离,这个时间很短以至于咱们的肉眼就以为小球是一直移动的,但其实是一次又一次微距离的显示消失显示消失去实现的
④ 当小球撞到边框时要反弹,也就是当小球的坐标变量x和y超过了边框的最大值或是小于了边框的最小值也就是0时,都需要做出x和y以及dx和dy的变化来达到撞击边框反弹的效果,详细解释都在代码的备注里有写
package com.javawork28;
// 绘制小球线程代码
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class Ball extends Thread {
private JPanel mainPanel = null;
private int x = 0; // x,y 小球坐标
private int y = 0;
Random random = new Random();
private int dx = random.nextInt(8); // dx,dy位移大小,通过随机数赋值,实现每个小球运动的斜率不一样
private int dy = random.nextInt(8);
private int width = 20; // width,height是小球的长高,一样就是圆的形状了
private int height = 20;
// 重写构造方法将进行操作的页面实例化时传入
public Ball(JPanel p) {
this.mainPanel = p;
}
// 重写线程的run方法
public void run() {
init();
while(true) { // 写一个死循环,除了点击退出按钮不然一直执行
try {
Thread.sleep(30);
Graphics g = this.mainPanel.getGraphics(); // 实例化Graphics画笔g
g.setColor(Color.WHITE); // setColor设置画出来的东西颜色为白色,也就是白色的小球
g.setXORMode(Color.BLACK); // setXORMode设置当有画的东西时把他颜色背景变为黑色,也就相当于擦去原来的小球
g.fillOval(this.x, this.y, this.width, this.height); // fillOval画出图案
// 能一直偏移的代码
x = x + dx;
y = y + dy;
// 当小球碰到边的情况,也就是大于当前panel的边时,一定要减去小球自身的长、高,不然小球碰撞时就会嵌入一些距离才反弹
if(x > this.mainPanel.getWidth() - this.width) {
dx = -dx;
}
if(y > this.mainPanel.getHeight() - this.height) {
dy = -dy;
}
if(x < 0) {
x = 0;
dx = -dx;
}
if(y < 0) {
y = 0;
dy = -dy;
}
g.fillOval(x, y, this.width, this.height);
g.dispose(); // dispose释放绘画所占的资源
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 这是调用画笔方法,在run方法最开始就调用把画笔graphics实例化出来
private void init(){
Graphics graphics = this.mainPanel.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillOval(x, y, this.width, this.height);
graphics.dispose();
}
}
运行游戏的代码
最后就是调用窗体的运行代码,很简单,就是实例化小球窗体
package com.javawork28;
// 小球运动的运行程序
public class BallRun {
public static void main(String[] args) {
new BallPanel();
}
}
运行结果
代码分享
代码我已经分享到我的github了,有需要的朋友可以去下载:小球游戏代码