package com.javarush.games.racer.road;
import com.javarush.engine.cell.Game;
import com.javarush.games.racer.RacerGame;
import java.util.ArrayList;
import java.util.List;
public class RoadManager {
public static final int LEFT_BORDER = RacerGame.ROADSIDE_WIDTH;
public static final int RIGHT_BORDER = RacerGame.WIDTH - LEFT_BORDER;
private static final int FIRST_LANE_POSITION = 16;
private static final int FOURTH_LANE_POSITION = 44;
private List<RoadObject> items = new ArrayList<>();
private RoadObject createRoadObject(RoadObjectType type, int x,int y) {
if (type == RoadObjectType.THORN) {
return new Thorn(x, y);
} else {
return null;
}
}
private void addRoadObject(RoadObjectType objectType, Game game) {
int x = game.getRandomNumber(FIRST_LANE_POSITION, FOURTH_LANE_POSITION);
int y = - 1 * RoadObject.getHeight(objectType);
RoadObject roadObject = this.createRoadObject(objectType, x, y);
if (roadObject != null) {
this.items.add(roadObject);
}
}
public void draw(Game game) {
for (RoadObject item : items) {
item.draw(game);
}
}
public void move(int boost) {
for(RoadObject item : items) {
item.move(boost + item.speed);
}
}
}
package com.javarush.games.racer;
import com.javarush.engine.cell.*;
import com.javarush.games.racer.road.RoadManager;
public class RacerGame extends Game {
public static final int WIDTH = 64;
public static final int HEIGHT = 64;
public static final int CENTER_X = WIDTH / 2;
public static final int ROADSIDE_WIDTH = 14;
private RoadMarking roadMarking;
private PlayerCar player;
private RoadManager roadManager;
@Override
public void initialize() {
this.setScreenSize(WIDTH, HEIGHT);
this.showGrid(false);
this.createGame();
}
@Override
public void setCellColor(int x, int y, Color color) {
if (x > WIDTH - 1 || x < 0 || y < 0 || y > HEIGHT - 1) {
return;
}
super.setCellColor(x, y, color);
}
@Override
public void onTurn(int some) {
this.moveAll();
this.drawScene();
}
public static void main(String[] args) {
RacerGame racer = new RacerGame();
racer.initialize();
}
private void createGame() {
this.roadMarking = new RoadMarking();
this.player = new PlayerCar();
this.roadManager = new RoadManager();
this.drawScene();
this.setTurnTimer(40);
}
private void moveAll() {
this.roadMarking.move(player.speed);
this.player.move();
}
@Override
public void onKeyPress(Key key) {
if (key == Key.RIGHT) {
this.player.setDirection(Direction.RIGHT);
}
if (key == Key.LEFT) {
this.player.setDirection(Direction.LEFT);
}
}
@Override
public void onKeyReleased(Key key) {
if (key == Key.RIGHT && player.getDirection() == Direction.RIGHT) {
this.player.setDirection(Direction.NONE);
}
if (key == Key.LEFT && player.getDirection() == Direction.LEFT) {
this.player.setDirection(Direction.NONE);
}
}
private void drawScene()
{
this.drawField();
this.roadMarking.draw(this);
this.player.draw(this);
this.roadManager.draw(this);
}
private void drawField() {
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
if (x == CENTER_X) {
this.setCellColor(x, y, Color.WHITE);
} else if (x >= ROADSIDE_WIDTH && x < WIDTH - ROADSIDE_WIDTH) {
this.setCellColor(x, y, Color.DIMGREY);
} else {
this.setCellColor(x, y, Color.GREEN);
}
}
}
}
}