CodeGym /Java Course /Java Multithreading /Big task: Arkanoid

Big task: Arkanoid

Java Multithreading
Level 3 , Lesson 13
Available

"Hi, Amigo!"

"Hello, Captain Squirrels, sir!"

"Today you have a new, more challenging task."

"We will write the game Arkanoid."

"Here's a link to what it will look like, approximately."

"Will it have the same graphics?"

Big task: Arkanoid - 1

"Of course not, we aren't civilians."

"Then what will it have?"

Big task: Arkanoid - 2

"Everything will be in the Spartan console style, just as it should be in the military."

"Proceed."

"Yes, sir. Proceeding to the task."

7
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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
Task
Java Multithreading, level 3, lesson 13
Locked
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.
Comments (17)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Jaime Padilla Level 41, Chandler, United States Expert
21 September 2023
I really enjoyed this one. The directions were straight forward and I think I am starting to fully understand how everything comes together.
阿狼 Level 32, Zhengzhou, China
12 July 2022
d33
Justin Smith Level 41, Greenfield, USA, United States
12 January 2022
The end result of this one was pretty disappointing. This would be another situation where I offer criticism that teaching how to use a graphics interface (mainly Swing and JFrame) should be a central component of any Java course. It also didn't even work correctly (when I moved the paddle at the beginning of the game, the ball didn't move with the paddle). Overall, the individual parts were ridiculously beginner-level, except for one part I recall took more than one try. If you've done the separate game projects, this is really underwhelming.
Thomas Level 41, Bayreuth, Germany
4 May 2022
Cause it was so disappointing I wrote a gui based arkanoid from scratch and upped it on github. It even has some of the powerups the original Arkanoid had (triple ball, laser, glue, slow ball, enlarge, shrink... ), a level loader (you can design levels in a txt file) and some annoying sound effects ;) can be started from the console using javaw -jar Breakout.jar (if doubleclick does not work)
Jurij Thmsn Level 29, Flensburg, Germany
21 October 2021
A nice task, overall. It helped me to understand the interaction between classes and working on a "bigger" code. But tbh I was a little bit disappointed in the end - because my own written code was replaced by another. I also think a few of the (too easy) tasks could have been put together into less tasks. Instead, some of the completed code which was added in the end could have been explained in additional tasks. This way, it felt more like "coding along" and not like doing it by myself.
Fadi Alsaidi Level 34, Carrollton, TX, USA
12 December 2020
What's the deal with the intersect method? I thought the distance between the 2 circles should be less or equal to the sum of the 2 circles' radius? the method they want us to use is the bigger of the 2 circles' radius.
Manish Sinha Level 26, london, United Kingdom
22 October 2020
i tried all the mode like full screen,presentation mode but nothing works :(
Lawson Level 29, Lagos, Nigeria
27 September 2020
Pls if anyone passed recently, kindly help me... i am stuck at part 9 and no reply to my question yet thanks Edit: resolved and finished the task .. thanks to mateusz
BlueJavaBanana Level 37
27 July 2020
Here is a good laugh for you all, I failed the final test! My solution was better than 0% of students! I pressed a key in error before validating and it meant the code didn't compile. If any of you can beat failing a task that was 100% done from the beginning let me know!
Andrei Level 41
6 April 2021
😂😂 you are in a select group!
Surya Level 33, Newark, United States
29 June 2020
Caution: Please don't reset any one task it will reset the whole 20 tasks or Big Task even if you reset the Part 20. I wish code gym takes care of this bug. I did a reset at 17th task and had to redo all tasks from task 1.
Johannes Level 27, Centurion, Pretoria, South-Africa
4 May 2020
I'm on Windows 10. The game starts automatically, and runs in a couple of seconds, so I can't even view it properly before completion. It also comprises of dots and letters, like the snake and mouse game, that ALSO didn't work as expected. It looks nothing like the graphic implied before the task. Ah well, it was fun. There's a reason C++ is used for creating games, not Java (except Minecraft, which is a decade or two backwards for me, a Call of Duty fan ;)_ ) (though my children enjoyed it, weird irony.)