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

相关文章

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解