使用Java做一个简单的台球游戏,只有白球的运动,半成品
BallGame.
package com.company;
import javax.swing.*;
import java.awt.*;
//建立窗口
public class BallGame extends JFrame {
Desktop desk = new Desktop();
Ball WhiteBall = new Ball("白球",200,200,20,Math.PI/3,this);
Line line = new Line(0,0,0,0,this,WhiteBall);
/**定义画布*/
Image offScreenImage = null;
/**窗口创建的启动方法*/
public void launch(){
//窗口是否可见
this.setVisible(true);
//设置窗口大小
this.setSize(852,500);
//设置窗口的位置
this.setLocation(100,200);
//设置窗口的标题
this.setTitle("桌球游戏");
WhiteBall.setBallPicture("\\白球.png");
while(true){
repaint();
try{
Thread.sleep(40);//没40ms调用一次repaint方法
}catch (Exception e){
e.printStackTrace();
}
}
}
/**绘制画布*/
@Override
public void paint(Graphics g){
//创建和容器一样大下的图片
if(offScreenImage==null){
offScreenImage=this.createImage(this.getWidth(),this.getHeight());
}
//获取该图片的画布(大小和图片一样,也和球桌大小一样)
Graphics gImage = offScreenImage.getGraphics();
//设置背景颜色
gImage.setColor(Color.darkGray);
//填充整个画布
gImage.fillRect(0,0,this.getWidth(),this.getHeight());
//绘制桌面图片
desk.Painting(gImage);
//绘制白球
WhiteBall.Painting(gImage);
//改变直线画笔的颜色
gImage.setColor(Color.red);
//绘制直线
line.Painting(gImage);
//将缓冲区绘制好的图形整个绘制到容器中
g.drawImage(offScreenImage,0,0,null);
}
public static void main(String[] args) {
BallGame bg = new BallGame();
bg.launch();
}
}
Printable.java
package com.company;
import java.awt.*;
public interface Paintable {
public void Painting(Graphics g);
}
Desktop.java
package com.company;
import java.awt.*;
public class Desktop implements Paintable{
/**获取桌面图片*/
Image desk = Toolkit.getDefaultToolkit().getImage("src\\com\\company\\球桌.png");
@Override
public void Painting(Graphics g) {
g.drawImage(desk,0,20,null);
}
}
Line.java
package com.company;
import java.awt.*;
public class Desktop implements Paintable{
/**获取桌面图片*/
Image desk = Toolkit.getDefaultToolkit().getImage("src\\com\\company\\球桌.png");
@Override
public void Painting(Graphics g) {
g.drawImage(desk,0,20,null);
}
}
Ball.java
package com.company;
import java.awt.*;
public class Ball implements Paintable{
private String Ballname;
private Image BallPicture;
/**球坐标*/
private double x = 200;
private double y = 200;
/**球的移动速度*/
private double speed = 20;
/**移动角度*/
private double degree = Math.PI/3;
private BallGame ballgame;
public Ball(String Ballname,double x,double y,double speed,double degree,BallGame ballgame){
this.Ballname=Ballname;
this.degree=degree;
this.speed=speed;
this.x=x;
this.y=y;
this.ballgame = ballgame;
}
@Override
public void Painting(Graphics g) {
g.drawImage(this.BallPicture,(int)x,(int)y,null);
if(speed>=0) {
speed -= 0.15;//0,15为摩擦
x += speed*Math.cos(degree);
y += speed*Math.sin(degree);
}
System.out.println("x "+x+" y "+y);
//碰到上下边
if(y <= 40||y >= ballgame.getHeight()-50){
degree = -degree;
}
//碰到左右边
if(x <= 70||x >= ballgame.getWidth()-70){
degree = Math.PI - degree;
}
}
public void setBallPicture(String path) {
this.BallPicture = Toolkit.getDefaultToolkit().getImage(path);
}
public double getDegree() {
return degree;
}
public void setDegree(double degree) {
this.degree = degree;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public String getBallname() {
return Ballname;
}
public void setBallname(String ballname) {
Ballname = ballname;
}
}
GameUtils.java
package com.company;
public class GameUtils {
/**函数,返回两点之间的角度*/
public static double getAngle(double startx,double starty,double endx,double endy){
double tempx = endx - startx;
double tempy = endy - starty;
double z = Math.sqrt(tempx*tempx+tempy*tempy);
double angle = (float)Math.asin(Math.abs(tempy)/z);
if(tempx>0&&tempy<0){
angle = Math.PI*2 - angle;
}
else if (tempx<0&&tempy<0){
angle=Math.PI +angle;
}
else if(tempx<0&&tempy>0){
angle =Math.PI -angle;
}
else
{
angle = angle;
}
return angle;
}
}
视频网址:https://www.bilibili.com/video/BV1yo4y1C7bW?p=1