“嗨,阿米戈!”
“您好,松鼠船長,先生!”
“今天你有一項新的、更具挑戰性的任務。”
“我們將編寫遊戲打磚塊。”
“這裡有一個鏈接,它大致看起來像什麼。”
“它會有相同的圖形嗎?”
“當然不是,我們不是平民。”
“那它會有什麼呢?”
“一切都將採用斯巴達控制颱風格,就像在軍隊中一樣。”
“繼續。”
“是的,先生。繼續執行任務。”
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 1)
Today let's write the game Arkanoid.
The original game looks something like this: http://www.youtube.com/watch?v=Th-Z6QQ5AOQ
To start, we'll write a simplified version of the game.
We will have bricks, a ball, and a flying paddle that prevents the ball from falling out the bottom.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 2)
The main class (Arkanoid) will need to store information about the size of the game field.
Therefore,
a) add two private int width and height fields to the Arkanoid class.
b) create public getters and setters for them.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 3)
Perhaps it's time to add a constructor to the Arkanoid class.
The constructor must accept two int parameters (width and height) and correctly set the values of the corresponding fields in the class.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 4)
The Arkanoid class will also need to store a reference to the ball (Ball), paddle (Paddle), and list of bricks.
Here's what we need:
a) create two private fields in the Arkanoid class: Ball ball and Paddle paddle.
b) add a private List bricks field.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 5)
What else is the Arkanoid class missing?
First, it needs a run() method, where we'll define the main logic of the program.
It also needs a move() method, which will move by one step all objects that need to be moved.
Create the run() and move() methods.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 6)
We will have bricks, a ball, and a paddle, and they'll have a lot in common.
They will move about the field and be drawn on the screen.
That means that they'll all have coordinates and a size.
And a move() method for moving and a draw() method for drawing them on the screen.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 7)
Now let's move on to the BaseObject class.
I want to make some suggestions.
Firstly, for simplicity, we'll treat all objects as circles.
We'll draw them as shapes, just as before.
But when calculating object collisions, we'll assume they are round.
Doing it this way is much simpler.
13
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 8)
But there's more.
The BaseObject class needs more methods.
For now, these will be empty draw(Canvas canvas) and move() methods.
Subclasses will have to override them and implement the necessary functionality (you can leave them empty for now—we just need the code to compile).
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 9)
Now let's tackle the Canvas class.
It will contain a matrix (two-dimensional array) that we will "draw" on.
The matrix has a width and height.
And we'll use it to store characters (chars) rather than numbers (ints).
Here's what we need:
a) Add two private width and height fields to the class.
26
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 10)
What are we going to do with Canvas?
We'll draw on it (on its matrix).
Therefore, we need two methods:
void setPoint(double x, double y, char c);
void drawMatrix(double x, double y, int[][] matrix, char c).
The first method (setPoint) will "set a point with coordinates (x, y) to color c".
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 11)
Canvas needs another two methods. Write them.
a) the clear() method.
This method will clear the matrix so it can be drawn on again.
You can just re-initialize the matrix with an empty array of the correct dimensions.
b) the print() method.
This method draws the matrix on the screen.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 12)
Now we'll write the Brick class.
First, let's take care of the constructor, where we'll specify a brick's "radius".
Let's do it like this:
public Brick(double x, double y) {
super(x, y, 3);
}.
We also need to override two methods: move() and draw(Canvas canvas).
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 13)
The Ball class is more complicated, since the ball moves.
We're going to need private fields:
a) double speed (the ball's speed);
b) double direction (the direction of motion in degrees: from 0 to 360).
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 14)
The Ball class is missing something:
First, you need to implement the move() method inherited from BaseObject:
a) x should increase by dx each turn;
b) y must increase by dy each turn;
If the ball is frozen, then x and y should not change.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 15)
You won't believe it, but we're still not done.
First, you need a setDirection method,
which not only sets the value of the direction variable,
but also computes the new values of the dx and dy variables.
The code should look something like this:
this.direction = direction;
double angle = Math.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 16)
And finally, the paddle!
It will need the following private fields:
a) double speed (the paddle's speed);
b) direction (direction of movement along the x axis: 1 - to the right, -1 - to the left, 0 (initial value) - hold still).
Also, create getters for them.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 17)
The paddle also needs methods:
a) move - see move in BaseObject.
The paddle moves horizontally, so we'll only change the x coordinate.
Think about how the x coordinate depends on direction and speed. Implement that dependence.
b) draw - see draw in BaseObject.
I'll handle the code myself.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 18)
Now we'll return to the Arkanoid class.
Implement the following methods:
a) move().
In this method, you need to move all movable objects (the paddle and ball).
b) draw(Canvas canvas).
In this method, you must call the draw method on all objects that have such a method.
13
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 19)
There's just a little bit left.
Create a private boolean isGameOver field in the Arkanoid class.
Implement the checkBrickCollision method.
In this method, you need to check whether the ball has collided with any of the bricks.
Use the intersects method to test for a collision.
7
任務
Java 多執行緒, 等級 3, 課堂 13
Arkanoid (part 20)
Good work! I added a couple of methods, as well a KeyboardObserver.
Take a little rest before the next level and play.
P.S. Just don't forget to adjust the height of the console.