java弹球GAME

做一个简单的弹球GAME,竟然比我想象代码量要少的多,基本上基于animation,球球是一闪一闪自动改变颜色的,接下来可能会做个登陆界面啥的,看心情。

上下方向键改变速度

注意:两个程序要放在同一个package里面//新手友好

下面是弹球主要代码,不包括主函数

package project;

import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;

public class Ballpane extends Pane {
	public final double radius = 15;
	private double x = (Math.random() * 200 + 15), y = radius;
	double dx = 1, dy = 1;
	private Circle circle = new Circle(x, y, radius);
	private Rectangle rt = new Rectangle(100, 180, 50, 20);
	private Timeline animation;
	private Timeline animation1;

	public Ballpane() {
		circle.setFill(Color.RED);
		rt.setFill(Color.BLACK);

		getChildren().add(rt);
		getChildren().add(circle);

		animation = new Timeline(new KeyFrame(Duration.millis(50), e -> moveball()));
		animation.setRate(5);
		animation.setCycleCount(Timeline.INDEFINITE);
		animation.play();

		animation1 = new Timeline(new KeyFrame(Duration.millis(500), e -> circlecolor()));

		animation1.setCycleCount(Timeline.INDEFINITE);
		animation1.play();
	}

	public void circlecolor() {
		circle.setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));
	}

	public void play() {
		animation.play();
	}

	public void right() {
		rt.setX(rt.getX() + 5);
	}

	public void left() {
		rt.setX(rt.getX() - 5);
	}

	public void increasespeed() {
		animation.setRate(animation.getRate() + 0.1);
	}

	public void decreasespeed() {
		animation.setRate(animation.getRate() > 0 ? animation.getRate() - 0.1 : 0.1);
	}

	public void moveball() {
		if (x < 15 || x > 235) {
			dx *= -1;

		}
		if (y > 235 && (x < rt.getX() || x > rt.getX() + 50))
			System.exit(0);
		if (y < 15 || (y > 165 && x > rt.getX() && x < rt.getX() + 50))
			dy *= -1;
		x += dx;
		y += dy;
		circle.setCenterX(x);
		circle.setCenterY(y);
	}
}

下面是主函数的控制界面
package project;

import javafx.application.*;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.input.KeyCode;

public class Ballcontrol extends Application{
	public void start(Stage pr) {
		Ballpane ballpane=new Ballpane();
		
		ballpane.setOnKeyPressed(e->{
			if(e.getCode()==KeyCode.UP)
				ballpane.increasespeed();
			else if(e.getCode()==KeyCode.DOWN)
				ballpane.decreasespeed();
			else if(e.getCode()==KeyCode.RIGHT)
				ballpane.right();
			else if(e.getCode()==KeyCode.LEFT)
				ballpane.left();
		});
		
		Scene scene=new Scene(ballpane,250,200);
		pr.setTitle("弹球");
		pr.setScene(scene);
		pr.show();
		ballpane.requestFocus();
	}
	public static void main(String [] args) {
		Application.launch(args);
	}

}



  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值