null
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 1/28)
We invite you to write an exciting game — Racer. We'll start with the rules. 1. The objective of the game is to reach the finish line by driving a race car on a busy race track. 2. The score depends on how long the race lasts: the sooner a player finishes, the higher his or her score. 3. The game i
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 2/28)
Now we'll prepare the playing field. In this part, we'll create variables and methods that we'll use in the future to draw the road and road markings. A dividing line will be placed in the middle of the road. The value of its x coordinate will be half of the width of the playing field and will be s
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 3/28)
Let's proceed to drawing the race track in the drawField() method. To set the color of a specific cell, use the setCellColor(int, int, Color) method. The passed arguments are the x and y coordinates and the color. Keep in mind that the playing field consists of three zones that must be painted usin
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 4/28)
During the game, many objects will appear, move about the playing field, and sometimes leave it. If the setCellColor(int, int, Color) method is called with coordinates outside the bounds of the playing field, an exception will be thrown. To avoid this, override it so that the superclass's method is
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 5/28)
Create a GameObject class to represent game objects. All game objects will have the following common properties: - int x, int y — coordinates of the object's upper left corner on the playing field; - int[][] matrix — matrix for displaying the game object. The matrix values are the ordinals of the c
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 6/28)
The road, shoulders, and dividing line are stationary objects: they do not move. Motion will be simulated by other game objects. We don't have cars yet. For now, only two dashed lines on the road will move. Other road markings will be represented by a separate class. You don't need to create it. Yo
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 7/28)
Now we'll create the player's car. Let's create a PlayerCar class and make it inherit GameObject: At first, the player's car will be in the third lane, one position above the bottom edge of the playing field. Accordingly, the x coordinate will be equal to RacerGame.WIDTH/2 + 2, and the y coordinate
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 8/28)
The dashed lines must move to create the illusion that the car is moving. The car itself will always be located below the game field. We'll give the car a field responsible for speed. Its value will be passed to roadMarking.move(int) and indicates how much the markings need to move. In RacerGame, w
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 9/28)
Along the horizontal axis, the car can move right, left, or not at all. To indicate these directions, create a Direction enum with the following fields. RIGHT, LEFT, NONE. The player's car must have a field that stores the direction of motion (Direction direction). We'll access it through a getter
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 10/28)
The car's behavior currently doesn't match what we would expect: - the car doesn't stop moving to the side when an arrow key is released; - the car drives right off the edge of the road and the playing field. To fix this, you need to implement the correct behavior for the car when the RIGHT and LEF
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 11/28)
The player will face various obstacles on the road: other cars driving the wrong way, and spikes that don't move relative to the race track. These obstacles will inherit the RoadObject class. The types of all possible obstacles are listed in the RoadObjectType enum. You now have access to these cla
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 12/28)
Obstacles must appear at random locations on the road. Add two constants to the RoadManager class: FIRST_LANE_POSITION and FOURTH_LANE_POSITION — the far left and far right road positions of the x coordinates of the obstacles' matrices. Assign them the values 16 and 44 respectively. Additionally,
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 13/28)
The RoadManager class's name isn't arbitrary: it manages all the obstacles on the road. It must know how to draw and move all the stored objects. In the RacerGame class, create a RoadManager roadManager field so you can access it. Initialize the roadManager field in createGame(), and call draw(Gam
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 14/28)
The time has come to draw the spike on the screen. There must not be more than one Spike object on the screen at the same time. To check this condition, create a spikeExists() method in the RoadManager class. To check whether the RoadObject is a spike, you can use its type field. The void generat
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 15/28)
Currently, spikes appear only once (at the beginning of the game). To generate a new spike, the old spike must be removed from the items list after its leaves the screen. To remove the spike, you can either use an iterator, or create a copy of the items list and run through it in a loop. If an obje
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 16/28)
Currently, the player's car continues to move after colliding with an obstacle. In this part of the project, we'll implement a method that will check whether the car has hit an obstacle. When implementing this method, use the GameObject class's isCollision(GameObject) method, which we have implemen
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 17/28)
The game must stop in two cases: when the player wins or when the player loses. For now, we'll only stop the game for losses. To save the current game state, we'll create a boolean isGameStopped field whose initial value must be false. When the player hits an obstacle, he or she loses. This means
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 18/28)
After the game is over, the player must be able to start a new game. Implement the ability to restart the game by pressing the SPACE key. To allow the player to cover the race track faster, his or her car must be able to accelerate. Implement the ability to double the speed by pressing the UP key.
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 19/28)
In addition to spikes, it would be nice to see various types of cars on the race track. To represent cars on the playing field, in the road package, create a Car class that will be common to all types of cars and make it inherit RoadObject. All cars will move at a speed of 1. Because the RoadManag
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 20/28)
There are currently too many oncoming cars. To make it possible to win the game, there must be adequate spaces between obstacles. That said, if they are too big, then the game will become too easy and not very interesting. The optimal distance is 12, which is slightly larger than the dimensions of
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 21/28)
Let's make the game a little harder. Let's add a car whose driver is "drunk". To represent it on the playing field, the MovingCar class has been added to the road package. Use it to create these cars. Unlike ordinary cars, a car operated by a "drunk" driver will drift horizontally. To prevent it fr
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 22/28)
Let's move on to creating the finish line. A matrix for it is already in ShapeMatrix. The finish line appears on the race track at the end of the game. Actually, the object representing the finish line is created at the beginning of the game, but it is located beyond the playing field, so it is not
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 23/28)
In this part of the project, we'll draw and move the finish line. To do this, an instance must be stored in the game's main class and recreated when a new game starts. Like the methods of our other objects, the FinishLine class's draw(Game) and move(int) methods will be called every 40 ms, but the
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 24/28)
To check the win condition, we'll need to know the actual number of cars that the player has passed. To do this, create a passedCarsCount field and corresponding getter in the RoadManager class. The value of this field must increase by one after the player passes each car obstacle (removal of a car
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 25/28)
In this part of the project, we'll add a progress bar to indicate the player's progress through the game. Note that you have a new class: ProgressBar. Figure out how it works and what it does? To store an instance of the ProgressBar class, we'll need a corresponding field in the game's main class.
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 26/28)
The player win's when he or she crosses the finish line. To check whether the player has crossed the finish line, you need to compare his or her y coordinate with the y of the finish line. If the player's y coordinate is less than the finish line's y coordinate, then the line has been crossed. In
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 27/28)
To allow the player to share his or her achievements, we'll add a scoring system to the game. The player starts with 3500 points. At each step of the game (40 ms), the number of points decreases by 5 until the player finishes. This motivates the player to finish the game faster.
undefined
3
Task
Games, level 0, lesson 5
Locked
Racer (Part 28/28)
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: - make it possible to move onto the shoulder; - add turns to the race track; - add the ability to switch to different speeds; -