CodeGym/рдЕрднреНрдпрд╛рд╕рдХреНрд░рдо/рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕/рдореЛрдареЗ рдХрд╛рд░реНрдп: Java рдордзреНрдпреЗ рдЧреЗрдо рддрдпрд╛рд░ рдХрд░рдгреЗ

рдореЛрдареЗ рдХрд╛рд░реНрдп: Java рдордзреНрдпреЗ рдЧреЗрдо рддрдпрд╛рд░ рдХрд░рдгреЗ

рдЙрдкрд▓рдмреНрдз

"рд╣рд╛рдп, рдЕрдорд┐рдЧреЛ!"

"рд╣рд╛рдп!"

"рддреБрдореНрд╣реА рд╡рд░рд┐рд╖реНрда рдЕрдзрд┐рдХрд╛рд▒реНрдпрд╛рд▓рд╛ рдХрд╕реЗ рд╕рдВрдмреЛрдзрд┐рдд рдХрд░рддрд╛?"

"рд╕реЙрд░реА рд╕рд░."

"рд╣реЕрд▓реЛ, рдХреЕрдкреНрдЯрди рд╕реНрдХреНрд╡рд┐рд░реЗрд▓реНрд╕, рд╕рд░."

"рд╣реЗ рдЙрддреНрддрдо рдЭрд╛рд▓реЗ."

"рдЖрдЬ рддреБрдордЪреНрдпрд╛рдХрдбреЗ рдПрдХ рдирд╡реАрди рдЧреБрдкреНрдд рдорд┐рд╢рди рдЖрд╣реЗ."

"рддреБрдореНрд╣рд╛рд▓рд╛ рд╕реНрдкреЗрд╕ рдмреЕрдЯрд▓ рд╕рд┐рдореНрдпреБрд▓реЗрд╢рди рд▓рд┐рд╣рд┐рдгреНрдпрд╛рдЪреА рдЧрд░рдЬ рдЖрд╣реЗ."

"рдЫрд╛рди! рдЙрд╣... рдореНрд╣рдгрдЬреЗ, рдореА рддрдпрд╛рд░ рдЖрд╣реЗ, рд╕рд░."

"рдЕрд╕реЗ рдШрдбрддреЗ рдХреА рдорд▓рд╛ рддреНрдпрд╛рдЪреА рдХрд▓реНрдкрдирд╛ рдЖрд╣реЗ. рд╣реЗ рдХрд╕реЗ?"

рдореЛрдареЗ рдХрд╛рд░реНрдп: Java рдордзреНрдпреЗ рдЧреЗрдо рддрдпрд╛рд░ рдХрд░рдгреЗ - рез

"рдирд╛рд╣реА, рддреЗ рдЦреВрдк рдЕрд╡рдШрдб рдЖрд╣реЗ."

"рдЖрдореНрд╣реА рдЖрдгрдЦреА рд╕реЛрдкреНрдпрд╛ рдЧреЛрд╖реНрдЯреАрдиреЗ рд╕реБрд░реБрд╡рд╛рдд рдХрд░реВ:"

рдореЛрдареЗ рдХрд╛рд░реНрдп: Java рдордзреНрдпреЗ рдЧреЗрдо рддрдпрд╛рд░ рдХрд░рдгреЗ - 2

"рд╣рдореНрдо..."

"рдХрд╛рдп? рдЕрдЬреВрди рдЕрд╡рдШрдб рдЖрд╣реЗ? рдордЧ рдЕрдЬреВрди рд╕реЛрдкрдВ рдХрд░реВрдпрд╛."

"рдХрд╛рд░реНрдп рдкреНрд░рд╛рдкреНрдд рдХрд░рдгреНрдпрд╛рд╕рд╛рдареА рдПрдЬрдВрдЯ рдЗрдВрдЯреЗрд▓рд┐рдЬ рдЖрдпрдбреАрдИрдПрд╢реА рд╕рдВрдкрд░реНрдХ рд╕рд╛рдзрд╛. рддреНрдпрд╛рдЪреНрдпрд╛рдХрдбреЗ рддреЗ рдЖрдзреАрдЪ рдЖрд╣реЗ."

"рдкреНрд░рд╢реНрди рд╡рд┐рдЪрд╛рд░рд╛рдпрдЪреА рдкрд░рд╡рд╛рдирдЧреА, рд╕рд░?"

"рдмреЛрд▓рд╛."

"рддреБрдореНрд╣реА IntelliJ IDEA рд▓рд╛ рдПрдЬрдВрдЯ рдХрд╛ рдореНрд╣рдгрддрд╛ рд╕рд░? рд╣рд╛ рдлрдХреНрдд рдПрдХ рдХрд╛рд░реНрдпрдХреНрд░рдо рдЖрд╣реЗ."

"рддреБрдореНрд╣реА рдлрдХреНрдд рдПрдХ рдХрд╛рд░реНрдпрдХреНрд░рдо рдЖрд╣рд╛рдд."

"рдЖрдпрдбреАрдИрдП рдПрдХ рдХреГрддреНрд░рд┐рдо рдмреБрджреНрдзрд┐рдорддреНрддрд╛ рдЖрд╣реЗ рдЖрдгрд┐ рдЖрдордЪреНрдпрд╛ рдЬрд╣рд╛рдЬрд╛рд╡рд░реАрд▓ рдХреНрд░реВ рдореЗрдВрдмрд░ рдЖрд╣реЗ. рд╣реЗ рд╕рд░реНрд╡ рд╕реНрдкрд╖реНрдЯ рдЖрд╣реЗ рдХрд╛? рдЖрдзреАрдЪ рдЬрд╛."

"рд╣реЛ, рд╕рд░! рддреБрдордЪреА рдСрд░реНрдбрд░ рдкрд╛рд░ рдкрд╛рдбрддреЛрдп рд╕рд░."

8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 1)
Let's write a new computer game. We'll have a spaceship that will shoot rockets at UFOs. For their part, the UFOs will lob bombs at the spaceship. And, of course, all this is happening in outer space. We're going to need the following classes: Space, Spaceship, and Ufo.
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 2)
We will have bombs and rockets. That means we need Bomb and Rocket classes. Create them. Our classes will have a lot in common. They will move about in space and be drawn on the screen. That means that they'll all have coordinates and a size.
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 3)
Why do we need the Space class? To store all the objects and control how they interact. What fields should it have? width and height. What else?
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 4)
What is Space class missing? That's right! run() and draw() methods. If you recall, the run method manages all the game logic. And the draw method is responsible for drawing the next "frame". We'll also benefit from a sleep(int ms) method.
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 5)
Now let's move on to the BaseObject class. I want to make some suggestions. First, for simplicity, treat all of our objects in space 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.
14
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 6)
But there's more. The BaseObject class needs more methods. For now, these will be empty draw() and move() methods. Subclasses will have to override them and implement the necessary functionality. Also add a die() method, which causes the object to die (isAlive = false).
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 7)
Now let's tackle the Canvas class. It will contain a matrix 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).
28
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 8)
What are we going to do with Canvas? We'll draw on it (on its matrix). Therefore, we need two methods: public void setPoint(double x, double y, char c), public void drawMatrix(double x, double y, int[][] matrix, char c).
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 9)
Canvas needs another two methods. Write them. a) the clear() method(); This method will clear the matrix so it can be drawn on again. For example, replace all characters in the matrix with spaces. b) the print() method(); This method draws the matrix on the screen.
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 10)
Now we'll write the Bomb class. Everything is simple here. Bomb inherits BaseObject. Here's what we need: a) change the constructor.
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 11)
Now we'll write the Rocket class. It's almost the same as the Bomb class. But: a) Rockets fly upward (i.e. y is decreased by 1); b) We'll draw 'R' instead of 'B'.
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 12)
Now let's do the ship. For complexity, we'll do this: if the user presses the left button, the ship will start moving to the left and continue to do so. If the user presses the right button, it will start moving to the right and continue to do so until it hits the edge.
14
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 13)
We now turn to the draw and move methods. In the move() method, you need to: a) increase x by dx; b) check to see whether the ship has moved beyond the boundaries of outer space [0, Space.game.getWidth()]. Use the checkBorders method. Bear in mind that the ship's width is equal to twice its radius.
14
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 14)
Now write the Ufo class. It's a bit more complicated than the Bomb class and simpler than Spaceship. You will need draw(), move(), and fire() methods. Also, don't forget about the constructor. The parameters are doubles, and UFO's radius will be equal to 3.
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 15)
We still have to finish the Space class. Implement the getAllItems method: The method should return one common list of all BaseObject objects. Implement the moveAllItems method: The method should move all the objects all at the same time.
14
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 16)
And a little bit more: Implement the createUfo() method: If the list of UFOs is empty, create a ship in the center at the top of the screen. Implement the checkBombs() method: You need to check whether a bomb collided with the ship.
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 17)
We need to finish the draw() method. We'll do something similar with the move method. I'll write it myself today.
8
рдЯрд╛рд╕реНрдХ
рдЬрд╛рд╡рд╛ рдХрд▓реЗрдХреНрд╢рдиреНрд╕,  рдкрд╛рддрд│реА 3рдзрдбрд╛ 15
рд▓реЙрдХ рдХреЗрд▓реЗрд▓реЗ
Space (part 18)
Make width and height bigger and you can play! Enjoy the game :)
рдЯрд┐рдкреНрдкрдгреНрдпрд╛
  • рд▓реЛрдХрдкреНрд░рд┐рдп
  • рдирд╡реАрди
  • рдЬреБрдиреЗ
рдЯрд┐рдкреНрдкрдгреА рдХрд░рдгреНрдпрд╛рд╕рд╛рдареА рддреБрдореНрд╣реА рд╕рд╛рдИрди рдЗрди рдХреЗрд▓реЗрд▓реЗ рдЕрд╕рдгреЗ рдЖрд╡рд╢реНрдпрдХ рдЖрд╣реЗ
рдпрд╛ рдкрд╛рдирд╛рд╡рд░ рдЕрдЬреВрди рдХреЛрдгрддреНрдпрд╛рд╣реА рдЯрд┐рдкреНрдкрдгреНрдпрд╛ рдирд╛рд╣реАрдд