Javafx barrier game development Lesson 3

Source: Internet
Author: User

Suddenly found that the javafx game development tutorial has not been updated for a long time...

But it doesn't matter. It may be updated more frequently in the future.

Next we will take the third lesson in javafx brick-making game development.


In the previous lesson, we created a mouse-controlled bezel. And a ball on the screen.

In this lesson, we will add some bricks and so on.

We will not discuss the algorithm issues here as stated in advance. That is to say, the games are the simplest and most basic, and there must be a lot of problems. You can modify it on your own, or I will write an article later.


First, we create a brick class.

import javafx.geometry.Bounds;import javafx.scene.effect.BoxBlur;import javafx.scene.effect.Lighting;import javafx.scene.paint.Color;import javafx.scene.shape.Rectangle;public class Brick extends BaseObject{  private Rectangle mRectangle;  private int hp;  private BoxBlur mBlur;  public Brick(Color color, int hp)  {    this.hp = hp;    this.mRectangle = new Rectangle();    this.mRectangle.widthProperty().bindBidirectional(widthProperty());    this.mRectangle.heightProperty().bindBidirectional(heightProperty());    this.mRectangle.xProperty().bindBidirectional(xProperty());    this.mRectangle.yProperty().bindBidirectional(yProperty());    this.mRectangle.setFill(color);    this.mBlur = new BoxBlur();    this.mBlur.setWidth(5);    this.mBlur.setHeight(5);    this.mRectangle.setEffect(new Lighting());    setWidth(100);    setHeight(25);    getChildren().add(this.mRectangle);  }  public Bounds getBounds() {    return this.mRectangle.getLayoutBounds();  }  public int getHp() {    return this.hp;  }  public void setHp(int hp) {    this.hp = hp;  }}

It is actually a rectangle that contains a blur effect and an HP attribute. We bind the attributes of the rectangle to the attributes of the object. In this way, we operate on the rectangle.


Next, create a level load class.

import javafx.scene.paint.Color;public class LevelLoader{  private static int[][] level1 = { { 0, 1, 1, 1, 1, 1, 0 }, { 0, 1, 1, 1, 1, 1, 0 }, { 0, 1, 1, 1, 1, 1, 0 }, { 0, 1, 1, 1, 1, 1, 0 } };  public static void load(GameScene scene, int level)  {    int[][] datas = getLevel(level);    for (int i = 0; i < datas.length; i++)      for (int j = 0; j < datas[0].length; j++)        if (datas[i][j] != 0) {          Brick brick = new Brick(Color.RED, datas[i][j]);          brick.setWidth(108.0D);          brick.setX((760- 7 * brick.getWidth()) / 2.0D + j * (brick.getWidth()));          brick.setY(i * (brick.getHeight()) + 100);          scene.addChild(brick);          scene.getBricks().add(brick);        }  }  public static int[][] getLevel(int i)  {    if (i == 1) {      return level1;    }    return (int[][])null;  }}

Here, we use a two-dimensional array to place the bricks. Gamescene is our game scenario, and of course it has undergone many changes. The hpvalue of the bricks is determined based on the values in the two-dimensional array. You can also bind the color and value of the bricks. Similarly, you can use a two-dimensional data to determine the type and hpvalue of the bricks.


Next, let's take a look at the scenario gamescene class:

import java.util.concurrent.CopyOnWriteArrayList;import javafx.animation.FadeTransition;import javafx.animation.KeyFrame;import javafx.animation.KeyValue;import javafx.animation.Timeline;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Parent;import javafx.scene.input.MouseEvent;import javafx.scene.paint.Color;import javafx.scene.shape.Rectangle;import javafx.util.Duration;public class GameScene extends Parent {private int width;private int height;private Rectangle background;private MainBrick mainBrick = new MainBrick();private Ball ball = new Ball(15, 15, 15);private Timeline timeline;private KeyFrame keyFrame;private CopyOnWriteArrayList<Brick> bricks = new CopyOnWriteArrayList<>();public GameScene(int width, int height) {this.width = width;this.height = height;initGameObjects();initTimeLine();initLevel();}private void initGameObjects() {this.background = new Rectangle(0.0D, 0.0D, this.width, this.height);this.background.setOnMouseMoved(new EventHandler<MouseEvent>() {@Overridepublic void handle(MouseEvent event) {mainBrick.onMouseMove(event);}});this.background.setFill(Color.BLACK);this.mainBrick.setX(0.0D);this.mainBrick.setY(this.height - this.mainBrick.getHeight());this.ball.setX((this.mainBrick.getWidth() - this.ball.getWidth()) / 2.0D);this.ball.setY(this.height - this.mainBrick.getHeight() - this.ball.getHeight());getChildren().add(this.background);getChildren().add(this.mainBrick);getChildren().add(this.ball);}private void initTimeLine() {this.timeline = new Timeline();this.timeline.setCycleCount(-1);this.keyFrame = new KeyFrame(Duration.millis(5.0D), new EventHandler<ActionEvent>() {public void handle(ActionEvent arg0) {ball.moveX(ball.getSpeedX());ball.moveY(ball.getSpeedY());if ((ball.getX() <= 0.0D) || (ball.getX() >= 800.0D - ball.getWidth())) {ball.setSpeedX(-ball.getSpeedX());}if ((ball.getY() <= 0.0D) || (ball.getY() >= 600.0D - ball.getHeight())) {ball.setSpeedY(-ball.getSpeedY());}if (ball.isCollisionWith(mainBrick)) {ball.setSpeedY(-ball.getSpeedY());}for (Brick brick : bricks) {if (ball.isCollisionWith(brick)) {brick.setHp(brick.getHp() - 1);ball.setSpeedY(-ball.getSpeedY());if (brick.getHp() <= 0) {destroyObject(brick);}break;}}}}, new KeyValue[0]);this.timeline.getKeyFrames().add(this.keyFrame);this.timeline.play();}private void destroyObject(final BaseObject brick) {FadeTransition fade = new FadeTransition(Duration.millis(200.0D), brick);fade.setFromValue(1.0D);fade.setToValue(0.0D);fade.setOnFinished(new EventHandler<ActionEvent>() {public void handle(ActionEvent t) {getChildren().remove(brick);}});this.bricks.remove((Brick) brick);fade.play();}private void initLevel() {LevelLoader.load(this, 1);}public void addChild(Parent parent) {getChildren().add(parent);}public CopyOnWriteArrayList<Brick> getBricks() {return this.bricks;}}

Gamescene contains a set of brick bricks. The bezel mainbrick and a ball are controlled with the mouse.

We create a timeline to move the ball in the frame and determine the collision.

If HP is less than or equal to 0, the destroyobject method is executed. This method performs a fadetransition on Brick to make it disappear with a gradient. Then remove from the set.


You can see the results:


Of course, there will be problems with the collision here. Then there is a collision with the baffle, and no angle or other judgment is made. But it doesn't matter. These can be solved in the future.

The first thing we need to do is complete the entire game process.

In the next lesson, we will create a game menu, and then complete the score of the level.


Reprinted please indicate the source:

Http://blog.csdn.net/ml3947

Bytes ------------------------------------------------------------------------------------------------------------------------

Recently, I am playing with my own blog. I have applied for a domain name and a space. Currently, my blog is focused on javafx technology and is developing wjfxgameengine. I have applied for a googlecode open-source project. The progress is still good, that is, the efficiency of javafx's current canvas is not flattering.

However, I believe there will be great improvements in the future. After all, canvas is only available in the latest version, and javafx 3D is also available in JDK 8.

After my self-developed wjfxgameengine game examples and javafx software increase, I will pin the website link in the article. You can check it out later.

------------------------------------------------------------


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.