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);
	}

}



package org.crazyit.ball; import java.awt.Image; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; /** * 小球对象 * * @author yangenxiong yangenxiong2009@gmail.com * @author Kelvin Mak kelvin.mak125@gmail.com * @version 1.0 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br>Copyright (C), 2009-2010, yangenxiong * <br>This program is protected by copyright laws. */ public class Ball extends BallComponent { // 定义球的竖向速度 private int speedY = 10; // 定义弹球的横向速度 private int speedX = 8; // 定义是否在运动 private boolean started = false; // 定义是否结束运动 private boolean stop = false; /** * m 有参数构造器 * * @param panelWidth * int 画板宽度 * @param panelHeight * int 画板高度 * @param offset * int 位移 * @param path * String 图片路径 */ public Ball(int panelWidth, int panelHeight, int offset, String path) throws IOException { // 调用父构造器 super(panelWidth, panelHeight, path); // 设置y坐标 this.setY(panelHeight - super.getImage().getHeight(null) - offset); } /** * 设置横向速度 * * @param speed * int 速度 * @return void */ public void setSpeedX(int speed) { this.speedX = speed; } /** * 设置竖向速度 * * @param speed * int 速度 * @return void */ public void setSpeedY(int speed) { this.speedY = speed; } /** * 设置是否在运动 * * @param b * boolean * @return void */ public void setStarted(boolean b) { this.started = b; } /** * 设置是否结束运动 * * @param b * boolean * @return void */ public void setStop(boolean b) { this.stop = b; } /** * 返回横向速度 * * @return int 速度 */ public int getSpeedX() { return this.speedX; } /** * 返回竖向速度 * * @return int 速度 */ public int getSpeedY() { return this.speedY; } /** * 是否在运动 * * @return boolean 是否在运动 */ public boolean isStarted() { return this.started; } /** * 是否已经结束运动 * * @return boolean 是否已经结束运动 */ public boolean isStop() { return this.stop; } }
Java中实现弹球小游戏需要用到图形化库,比如Swing或JavaFX等。下面是一个简单的实现思路: 1. 创建一个游戏窗口,设置窗口的大小和标题。 2. 在窗口中创建一个挡板和一个小球,挡板可以通过键盘控制左右移动,小球则需要设置一个初始位置和速度。 3. 在每次游戏循环中,需要判断小球是否碰到了窗口的边缘,如果碰到了需要改变方向。 4. 同时也需要判断小球是否碰到了挡板,如果碰到了需要改变方向。 5. 如果小球掉到了窗口底部则游戏结束,可以根据需要进行重新开始或退出游戏的操作。 以下是一个简单的代码示例,供参考: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class BallGame extends JFrame implements KeyListener { private int panelWidth = 500; private int panelHeight = 500; private int ballSize = 20; private int ballX = panelWidth / 2 - ballSize / 2; private int ballY = panelHeight / 2 - ballSize / 2; private int ballSpeedX = 5; private int ballSpeedY = 5; private int paddleWidth = 80; private int paddleHeight = 10; private int paddleX = panelWidth / 2 - paddleWidth / 2; private int paddleY = panelHeight - paddleHeight - 20; private boolean leftPressed = false; private boolean rightPressed = false; public BallGame() { super("弹球小游戏"); setSize(panelWidth, panelHeight); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setLocationRelativeTo(null); addKeyListener(this); setVisible(true); } @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.BLUE); g.fillOval(ballX, ballY, ballSize, ballSize); g.setColor(Color.BLACK); g.fillRect(paddleX, paddleY, paddleWidth, paddleHeight); } public void moveBall() { ballX += ballSpeedX; ballY += ballSpeedY; if (ballX < 0 || ballX + ballSize > panelWidth) { ballSpeedX = -ballSpeedX; } if (ballY < 0) { ballSpeedY = -ballSpeedY; } if (ballY + ballSize > panelHeight) { JOptionPane.showMessageDialog(this, "游戏结束!"); System.exit(0); } if (ballY + ballSize >= paddleY && ballX + ballSize >= paddleX && ballX <= paddleX + paddleWidth) { ballSpeedY = -ballSpeedY; } } public void movePaddle() { if (leftPressed && paddleX > 0) { paddleX -= 5; } if (rightPressed && paddleX + paddleWidth < panelWidth) { paddleX += 5; } } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { leftPressed = true; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { rightPressed = true; } } @Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { leftPressed = false; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { rightPressed = false; } } @Override public void keyTyped(KeyEvent e) {} public static void main(String[] args) { new BallGame(); while (true) { try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } BallGame game = new BallGame(); game.moveBall(); game.movePaddle(); game.repaint(); } } } ``` 以上代码可能存在一些问题,仅供参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值