null
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 1/34)
Today we invite you to write an exciting game — Space Invaders. As always, we'll start with the rules. The object of the game is for the player to destroy every alien spaceship. The player controls a laser cannon and can move it left and right at the bottom of the screen. The player has an unlimit
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 2/34)
Everything in the game happens in outer space. To draw this setting, let's declare a drawField() method. We'll create all the elements of the game in the createGame() method, and we'll draw them in the drawScene() method. All objects are created when the game starts, so the initialize() method will
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 3/34)
Let's implement the drawField() method, which will paint the playing field a single color. To do this, we need to set a color and the empty string on each cell. The Game class's setCellValueEx(int, int, Color, String) method will help us. The first two parameters are the cell's x and y coordinates,
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 4/34)
We can use any Unicode symbol to represent stars. We'll store it in the Star class's STAR_SIGN field. To draw stars on the playing field, we'll create a draw(Game) method. We'll have it call the setCellValueEx method on the Game object passed to it as an argument. We'll pass the star's coordinates,
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 5/34)
Stars aren't the objects in this game. We will also need spaceships, of course! A game object's appearance is determined by a matrix, so we'll add a matrix field to the GameObject class. In addition to the object's outline, the matrix contains the ordinals of colors in the com.codegym.engine.cell.C
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 6/34)
Now we'll create a Ship class that will store properties that all spaceships share. It will have a setStaticView method that at present will set a matrix. Later, it will set an animation. First, we make a class for ships in the enemy's fleet: EnemyShip. We've provided its matrix for you in the Shap
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 7/34)
In this part of the project, we'll prepare the foundation for the enemy fleet. To do this, we'll create an EnemyFleet class. It will store the list of ships, number of rows of ships (ROWS_COUNT), number of ships per row (COLUMNS_COUNT), and distance between left corners of adjacent ships (STEP). We
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 8/34)
Let's continue working on the enemy fleet. To do this, we need to fill the ships list in the createShips() method. Keep in mind that the best distance from the top of the screen to the top row of ships is 12 cells. To add the enemy fleet to the game, we'll create an enemyFleet field in the SpaceInv
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 9/34)
Everything will move in the game, which means that we need to specify the direction in which objects are moving. It is convenient to store directions in an enum. We'll describe the motion of an enemy ship in the move() method. The method will change the appropriate coordinate depending on direction
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 10/34)
According to the rules of the game, the fewer ships there are, the faster they move from side to side. We'll implement this relationship in the getSpeed method. Note that the enemy ships' speed must not exceed 2. We'll also give the EnemyFleet class a direction field and a move method. We'll need t
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 11/34)
When the ships reach the edge of the screen, they begin to move in the opposite direction and drop lower. To achieve this, we need to implement the EnemyFleet class's move method.
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 12/34)
We'll create a Bullet class responsible for the projectiles. This class's constructor will take x and y coordinates and a direction. The constructor will also call the setMatrix method with the projectile's matrix (ShapeMatrix.BULLET), which you can find in the ShapeMatrix class. Because the projec
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 13/34)
Let's introduce a DIFFICULTY variable responsible for the game's difficulty, specifically the probability that an enemy ship will fire in a step of the game. All ships should be able to fire, so we'll add a fire method to the Ship class. We won't create any instances of this class, so this method w
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 14/34)
Let's create and implement a fire() method for the EnemyFleet class. This method will call the fire on a randomly selected ship in the fleet. Additionally, the probability that the fleet will fire will be determined by the value of the DIFFICULTY field.
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 15/34)
We'll store all the enemy projectiles in an enemyBullets field in the SpaceInvadersGame class. We'll add code to draw and move them in appropriate methods. To remove projectiles that are no longer relevant, we'll create and implement a removeDeadBullets() method in the SpaceInvadersGame class. To r
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 16/34)
Now we'll create the player's ship. We need the PlayerShip class for this. Its constructor will set the ship's coordinates so that the ship is displayed centered at the bottom of the screen. The constructor sets the ship's appearance by setting the PLAYER matrix, which we've provided for you in the
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 17/34)
We'll give the PlayerShip class a method that checks whether enemy projectiles have hit the player's ship (the isCollision() method in the GameObject class). If there is a hit, it will "kill" the ship and the projectile. We'll also add the player's ship to the Game class. We'll draw it and add the
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 18/34)
At this stage, we'll prepare an animation for when a ship explodes. To do this, we need a List frames field, which will store a list of matrices representing the animation frames. We'll also need a frameIndex field that stores the index of the current animation frame. We'll set the animati
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 19/34)
We'll implement logic to "kill" the player by overriding the kill() method. In addition to setting the isAlive flag to false, it will add an animation of the ship's destruction.
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 20/34)
We'll create the nextFrame method in the Ship class to switch to the next animation frame. It sets the matrix field to the next animation frame, if possible. We'll extend the functionality of the draw method by overriding it so that it calls the superclass's method for drawing and the nextFrame met
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 21/34)
The game ends when the player wins or loses. Let's implement the stopGame method, which will stop the game and display an appropriate message on the screen. You decide on the exact wording of the messages about winning and losing. If a projectile hits a player, the explosion animation must be displ
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 22/34)
Let's add a new property to the player's ship: direction. We'll set it through a setter method. The ship can move in two directions: left or right. But it can also hold still, so we'll use "up" to represent this state. The ship isn't moving when the game starts, so we'll set the ship's default dire
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 23/34)
In this part of the project, we'll move the player's ship. Keep in mind that the ship must not leave the playing field. To move the ship with the other objects, you need to call the move() method in the moveSpaceObjects() method of the game's main class.
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 24/34)
When you release the left key or the right key, the ship does not stop moving until it hits the edge of the playing field. Let's fix that. We'll override the Game class's onKeyReleased() method. This method is called when keys are released. If the player's ship is moving, the onKeyReleased(Key) met
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 25/34)
Now we'll prepare to give the player the ability to shoot. For this, we need a fire() method that will create projectiles. We will store them in the playerBullets list. Add logic to draw projectiles in the drawScene method, and to move them — in the moveSpaceObjects method. Now all that remains is
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 26/34)
In this part of the project, we'll add the ability to shoot by pressing the spacebar. The player's newly created projectiles are added to the playerBullets list, while those that have flown off the screen are removed from it. To remove the projectiles, you can either use an iterator, or create a co
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 27/34)
Let's override the method for destroying an enemy ship. It sets the isAlive field to false and sets the destruction animation matrix. We'll also create an isVisible() method in the Ship class. It will return false after the entire animation has been displayed for a "dead" ship.
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 28/34)
At this stage, we'll add a checkHit(List bullets) method for checking whether the player's projectile has hit an enemy ship. After a hit, we need to "kill" the corresponding projectile and enemy ship. Additionally, we'll add a deleteHiddenShips() method that will remove "hidden" ships from
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 29/34)
It's time to create the enemy boss. To make him look tougher, we'll add an animation when he moves. If the animation changes with every step, it will move too quickly. To create the desired effect, you need to change the frame once every 10 steps. That's why we need to override the nextFrame method
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 30/34)
Now we'll teach the boss to shoot. Depending on the animation frame, the boss will shoot from different cannons. We'll also override the kill() method, where we'll set the value of the isAlive field and set the frames of the explosion animation.
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 31/34)
Let's make the boss animate continuously. To do this, we need to give the setAnimatedView method an isLoopAnimation parameter responsible for indicating whether the animation should loop. This parameter is used to properly set the loopAnimation field. If the loopAnimation field is true, the nextFra
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 32/34)
The game is lost not only if the player's ship is destroyed, but also if the enemy fleet reaches the player's ship. To determine the coordinates of the lower edge of the enemy fleet, we'll add a getBottomBorder() method. If all the enemies are destroyed, the player wins. For convenience, we'll add
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 33/34)
At this stage, we'll add scorekeeping to the game. The player gets 15 points for each ordinary ship and 100 points for the boss. It will be most convenient to keep score in the checkHit() method, after reworking it a little. To store the total score, we'll add a score variable in the SpaceInvadersG
undefined
3
Task
Games, level 0, lesson 6
Locked
Space Invaders (Part 34/34)
Congratulations! The game is done! Run it and see what happens. If you have time and motivation, you can improve the game by adding some features of your own. For example: - add other types of ships; - change the number of cannons on the ships; - add new types of projectiles; - change the scoring s