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

相关文章

一文详解Java Condition的await和signal等待通知机制

《一文详解JavaCondition的await和signal等待通知机制》这篇文章主要为大家详细介绍了JavaCondition的await和signal等待通知机制的相关知识,文中的示例代码讲... 目录1. Condition的核心方法2. 使用场景与优势3. 使用流程与规范基本模板生产者-消费者示例

C++实现获取本机MAC地址与IP地址

《C++实现获取本机MAC地址与IP地址》这篇文章主要为大家详细介绍了C++实现获取本机MAC地址与IP地址的两种方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实际工作中,项目上常常需要获取本机的IP地址和MAC地址,在此使用两种方案获取1.MFC中获取IP和MAC地址获取

使用PyQt实现简易文本编辑器

《使用PyQt实现简易文本编辑器》这篇文章主要为大家详细介绍了如何使用PyQt5框架构建一个简单的文本编辑器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录分析主窗口类 (MyWindow)菜单操作语法高亮 (SyntaxHighlighter)运行程序主要组件代码图示分析实现

查询Oracle数据库表是否被锁的实现方式

《查询Oracle数据库表是否被锁的实现方式》本文介绍了查询Oracle数据库表是否被锁的方法,包括查询锁表的会话、人员信息,根据object_id查询表名,以及根据会话ID查询和停止本地进程,同时,... 目录查询oracle数据库表是否被锁1、查询锁表的会话、人员等信息2、根据 object_id查询被

Pandas中多重索引技巧的实现

《Pandas中多重索引技巧的实现》Pandas中的多重索引功能强大,适用于处理多维数据,本文就来介绍一下多重索引技巧,具有一定的参考价值,感兴趣的可以了解一下... 目录1.多重索引概述2.多重索引的基本操作2.1 选择和切片多重索引2.2 交换层级与重设索引3.多重索引的高级操作3.1 多重索引的分组聚

Sentinel 断路器在Spring Cloud使用详解

《Sentinel断路器在SpringCloud使用详解》Sentinel是阿里巴巴开源的一款微服务流量控制组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、... 目录Sentinel 介绍同类对比Hystrix:Sentinel:微服务雪崩问题问题原因问题解决方案请

Spring IOC的三种实现方式详解

《SpringIOC的三种实现方式详解》:本文主要介绍SpringIOC的三种实现方式,在Spring框架中,IOC通过依赖注入来实现,而依赖注入主要有三种实现方式,构造器注入、Setter注入... 目录1. 构造器注入(Cons编程tructor Injection)2. Setter注入(Setter

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC