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

相关文章

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

AJAX请求上传下载进度监控实现方式

《AJAX请求上传下载进度监控实现方式》在日常Web开发中,AJAX(AsynchronousJavaScriptandXML)被广泛用于异步请求数据,而无需刷新整个页面,:本文主要介绍AJAX请... 目录1. 前言2. 基于XMLHttpRequest的进度监控2.1 基础版文件上传监控2.2 增强版多

Spring Security方法级安全控制@PreAuthorize注解的灵活运用小结

《SpringSecurity方法级安全控制@PreAuthorize注解的灵活运用小结》本文将带着大家讲解@PreAuthorize注解的核心原理、SpEL表达式机制,并通过的示例代码演示如... 目录1. 前言2. @PreAuthorize 注解简介3. @PreAuthorize 核心原理解析拦截与

一文详解JavaScript中的fetch方法

《一文详解JavaScript中的fetch方法》fetch函数是一个用于在JavaScript中执行HTTP请求的现代API,它提供了一种更简洁、更强大的方式来处理网络请求,:本文主要介绍Jav... 目录前言什么是 fetch 方法基本语法简单的 GET 请求示例代码解释发送 POST 请求示例代码解释

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Redis分片集群的实现

《Redis分片集群的实现》Redis分片集群是一种将Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一... 目录1. Redis Cluster的核心概念哈希槽(Hash Slots)主从复制与故障转移2.

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的