Java实现简单台球游戏
作者:xujinshan361 时间:2022-06-28 23:55:59
Java实现简单台球桌问题,供大家参考,具体内容如下
需求:
使小球可以在桌面上移动,移动到桌面边缘将被弹回,显示小区的移动
素材:
小球照片
桌球照片
程序源代码:
package 桌球游戏;
import java.awt.*;
import javax.swing.*;
public class BallGame extends JFrame {
Image ball=Toolkit.getDefaultToolkit().getImage("image/ball.png");
Image desk=Toolkit.getDefaultToolkit().getImage("image/desk.jpg");
double x=100;//小球的横坐标
double y=100;//小球的纵坐标
/*boolean right =true; //方向*/
double degree=3.14/3; //弧度表示,60°
//窗口加载
void launchFrame() {
setSize(856,500); //参数和桌面有关
setLocation(400,400);
setVisible(true); //设置窗口可见
//重画窗口,每秒画25次
while(true) {
repaint();//repaint()方法会调用paint()方法
try {
Thread.sleep(20);//40ms,
}catch(Exception e) {
e.printStackTrace();
}
}
}
//画窗口方法
public void paint(Graphics g){
System.out.println("窗口被画一次!");
g.drawImage(desk,0,0,null);
g.drawImage(ball, (int)x,(int)y, null);
/* //注释内容为小球水平方向移动
x=x+1;
if(right) {
x+=10;
}else {
x-=10;
}
if(x>856-40-30) //30小球的直径,40球桌边框的
right=false;
if(x<40)
right=true;
//y=y+2;
* */
x=x+10*Math.cos(degree);
y=y+10*Math.sin(degree);
//当小球遇到桌面的上下界面时,发生反转,
if(y>500-40-30||y<40+40) //(40+40)一个表示标题栏,一个时球桌宽
degree=-degree;
if(x<0+40||x>856-40-30)
degree =3.14-degree;
}
//main 方法执行入口
public static void main(String []args) {
System.out.println("欢迎来到小球游戏!");
BallGame game=new BallGame();
game.launchFrame();
}
}
运行效果
来源:https://blog.csdn.net/xu_jin_shan/article/details/85237667
标签:java,台球,游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c语言动态数组示例
2023-11-02 22:56:44
Java解析调用webservice服务的返回XML串详解
2023-11-07 02:42:01
浅谈Android开发中项目的文件结构及规范化部署建议
2022-05-13 12:47:37
slf4j jcl jul log4j1 log4j2 logback各组件系统日志切换
2023-08-08 13:00:41
![](https://img.aspxhome.com/file/2023/8/57928_0s.png)
如何自动生成Mybatis的Mapper文件详解
2023-07-28 08:20:54
![](https://img.aspxhome.com/file/2023/8/57938_0s.jpg)
Maven实战之搭建Maven私服和镜像的方法(图文)
2023-11-27 22:27:06
![](https://img.aspxhome.com/file/2023/9/60249_0s.png)
利用Java写一个学生管理系统
2023-09-24 17:06:54
![](https://img.aspxhome.com/file/2023/1/61071_0s.jpg)
jenkins+maven+svn自动部署和发布的详细图文教程
2023-08-24 05:18:07
![](https://img.aspxhome.com/file/2023/2/58442_0s.png)
JAVA8 十大新特性详解
2023-07-02 10:03:27
基于idea Maven中的redis配置使用详解
2023-11-29 11:57:28
Java使用二分法进行查找和排序的示例
2023-03-16 10:16:37
详解 Corba开发之Java实现Service与Client
2023-09-23 03:42:48
![](https://img.aspxhome.com/file/2023/3/60183_0s.png)
svn 清理失败 (cleanup 失败) 的快速解决方法
2022-10-25 11:22:40
![](https://img.aspxhome.com/file/2023/3/60843_0s.jpg)
详解Java中的线程池
2023-11-10 16:33:27
![](https://img.aspxhome.com/file/2023/9/59369_0s.png)
Java真题实练掌握哈希表的使用
2023-11-09 06:33:15
![](https://img.aspxhome.com/file/2023/9/59079_0s.png)
SpringBoot中如何对actuator进行关闭
2022-11-30 01:56:37
![](https://img.aspxhome.com/file/2023/9/62349_0s.png)
简单记事本java源码实例
2023-11-26 02:03:17
![](https://img.aspxhome.com/file/2023/4/59744_0s.jpg)
Java代理模式的深入了解
2021-06-06 10:24:38
![](https://img.aspxhome.com/file/2023/0/60570_0s.jpg)
SpringBoot返回多种格式的数据的实现示例
2023-11-24 14:22:52
![](https://img.aspxhome.com/file/2023/0/59700_0s.png)
Studio 编译报错:compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.的解决办法
2023-06-19 17:19:41
![](https://img.aspxhome.com/file/2023/0/81320_0s.png)