JAVA实现小球弹跳

2023-10-11 17:59
文章标签 java 实现 小球 弹跳

本文主要是介绍JAVA实现小球弹跳,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、效果

二、教程

三、代码


一、效果

首先,我们来看效果,一共五个颜色不相同的球,每撞击一下边界,分数加1,分数越大,球的速度越快。(效果是动态的

 

二、教程

1、使用IDEA搭建一个项目,项目名称:MyBall(可根据自己的喜好)

具体搭建过程可看博文用IDEA构建一个简单的Java程序范例,这里就不详细说了。

2、MyBall.class

(1)导入包

import java.awt.Component;
import javax.swing.JFrame;

(2)主函数

在这里我的panel大小设置的是800*600,大家可以根据自己的喜好设置的更大一些。

public static void main(String[] args) {JFrame frame = new JFrame("球球");MyBallPanel panel = new MyBallPanel();frame.add(panel);Thread t = new Thread(panel);t.start();frame.setSize(800, 600);frame.setLocationRelativeTo((Component)null);frame.setDefaultCloseOperation(3);frame.setVisible(true);}

3、MyBallPanel.class

(1)导入包

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;

(2)类的继承,接口的实现

  • JPanel类:面板组件,非顶层容器
public class MyBallPanel extends JPanel implements Runnable {}

(3)数据类型的定义

  • color:Color
  • speed:int,为小球的速度
  • score:int,为分数
  • xx,yy:int,为小球出现的坐标
  • ff:int,5个小球的运动方向
Color color;
int speed = 10;
int score = 0;
int[] xx = new int[5];
int[] yy = new int[5];
Color[] colors = new Color[5];
int[] ff = new int[5];

(4)画布函数MyStarPanel()

  • for循环:5个小球的一开始出现的位置和颜色

画布的大小:800*600,小球的大小为30*30

我们小球的起始位置xx:0 - 770      yy:0 - 570

如下图:划线部分为小球的起始位置,若小球在图中部分,则小球出了画布,不会完整显示。

 

public MyBallPanel() {for(int i = 0; i < 5; ++i) {this.xx[i] = (int)(Math.random() * 770);this.yy[i] = (int)(Math.random() * 570);this.colors[i] = this.randomColor();this.ff[i] = (int)(Math.random() * 4);}}

(5)画笔函数paint()

  • 画笔名称g
  • 小球的颜色为我们一开始设定的颜色,大小为30
  • 左上角的“分数”,字体为微软雅黑,大小50
public void paint(Graphics g) {super.paint(g);for(int i = 0; i < 5; ++i) {g.setColor(this.colors[i]);g.fillOval(this.xx[i], this.yy[i], 30, 30);}g.setFont(new Font("微软雅黑", 1, 28));g.drawString("分数:" + this.score, 50, 50);}

(6)线程函数run()

for循环:循环5次,分别代表5个小球

  • 定义小球运动方向ff:

ff:是一个长度为5的int型数组,分别存储5个小球的运动状态,即小球的运动方向。

ff = 0, 1, 2, 3

注:画布的坐标轴和我们数学上的不太一样,起始点O在左上角。因此ff = 0是右下方向,如下图所示。

// ff = 0:右下
if (this.ff[i] == 0) {this.xx[i]++;this.yy[i]++;
}
// ff = 1:右上
if (this.ff[i] == 1) {this.xx[i]++;this.yy[i]--;
}
// ff = 2:左上
if (this.ff[i] == 2) {this.xx[i]--;this.yy[i]--;
}
// ff = 3:左下
if (this.ff[i] == 3) {this.xx[i]--;this.yy[i]++;
}
  • 小球运动到边界时,需要更改小球运动状态,改变小球颜色,分数加1 ,速度减1

一共四个边界

 下面我们以一个边界为例:下边界,即xx[i] > 770(画布大小为800*600)

小球撞击右边边界有两种情况:

一是由①撞击,那么此时小球的运动状态为ff = 0,此时我们需要改变小球的运动状态ff = 3

二是由②撞击,那么此时小球的运动状态为ff  = 1,此时我们需要改变小球的运动状态ff = 2

// 撞击下边
if (this.yy[i] > 540) {++this.score;--this.speed;this.colors[i] = this.randomColor();if (this.ff[i] == 0) {this.ff[i] = 1;} else {this.ff[i] = 2;}
}
// 撞击右边
if (this.xx[i] > 770) {++this.score;--this.speed;this.colors[i] = this.randomColor();if (this.ff[i] == 1) {this.ff[i] = 2;} else {this.ff[i] = 3;}
}
// 撞击上边
if (this.yy[i] < 0) {++this.score;--this.speed;this.colors[i] = this.randomColor();if (this.ff[i] == 2) {this.ff[i] = 3;} else {this.ff[i] = 0;}
}
// 撞击左边
if (this.xx[i] < 0) {++this.score;--this.speed;this.colors[i] = this.randomColor();if (this.ff[i] == 3) {this.ff[i] = 0;} else {this.ff[i] = 1;}
} 
  • 当速度为1时,速度保持为1
if (this.speed < 1) {this.speed = 1;
}

(7)小球颜色函数randomColor() 

public Color randomColor() {int R = (int)(Math.random() * 255);int G = (int)(Math.random() * 255);int B = (int)(Math.random() * 255);Color color = new Color(R, G, B);return color;}
}

三、代码

1、MyBall.class

package MyBall;import java.awt.Component;
import javax.swing.JFrame;public class MyBall {public static void main(String[] args) {JFrame frame = new JFrame("球球");MyBallPanel panel = new MyBallPanel();frame.add(panel);Thread t = new Thread(panel);t.start();frame.setSize(800, 600);frame.setLocationRelativeTo((Component)null);frame.setDefaultCloseOperation(3);frame.setVisible(true);}
}

2、MyBallPanel.class

package MyBall;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;public class MyBallPanel extends JPanel implements Runnable {Color color;int speed = 10;int score = 0;int[] xx = new int[5];int[] yy = new int[5];Color[] colors = new Color[5];int[] ff = new int[5];public MyBallPanel() {for(int i = 0; i < 5; ++i) {this.xx[i] = (int)(Math.random() * 770);this.yy[i] = (int)(Math.random() * 570);this.colors[i] = this.randomColor();this.ff[i] = (int)(Math.random() * 4);}}public void paint(Graphics g) {super.paint(g);for(int i = 0; i < 5; ++i) {g.setColor(this.colors[i]);g.fillOval(this.xx[i], this.yy[i], 30, 30);}g.setFont(new Font("微软雅黑", 1, 28));g.drawString("分数:" + this.score, 50, 50);}public void run() {while(true) {for(int i = 0; i < 5; ++i) {if (this.ff[i] == 0) {this.xx[i]++;this.yy[i]++;}if (this.ff[i] == 1) {this.xx[i]++;this.yy[i]--;}if (this.ff[i] == 2) {this.xx[i]--;this.yy[i]--;}if (this.ff[i] == 3) {this.xx[i]--;this.yy[i]++;}if (this.yy[i] > 540) {++this.score;--this.speed;this.colors[i] = this.randomColor();if (this.ff[i] == 0) {this.ff[i] = 1;} else {this.ff[i] = 2;}}if (this.xx[i] > 770) {++this.score;--this.speed;this.colors[i] = this.randomColor();if (this.ff[i] == 1) {this.ff[i] = 2;} else {this.ff[i] = 3;}}if (this.yy[i] < 0) {++this.score;--this.speed;this.colors[i] = this.randomColor();if (this.ff[i] == 2) {this.ff[i] = 3;} else {this.ff[i] = 0;}}if (this.xx[i] < 0) {++this.score;--this.speed;this.colors[i] = this.randomColor();if (this.ff[i] == 3) {this.ff[i] = 0;} else {this.ff[i] = 1;}}if (this.speed < 1) {this.speed = 1;}}try {Thread.sleep((long)this.speed);} catch (InterruptedException var2) {var2.printStackTrace();}this.repaint();}}public Color randomColor() {int R = (int)(Math.random() * 255);int G = (int)(Math.random() * 255);int B = (int)(Math.random() * 255);Color color = new Color(R, G, B);return color;}
}

 

这篇关于JAVA实现小球弹跳的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/189837

相关文章

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获