removing all dead objects but doesnt validate and the also creating ufo object every 10 call.
Thanks!.
package com.codegym.task.task25.task2515;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
/**
* The game's main class is Space
*/
public class Space {
// Width and height of the game field
private int width;
private int height;
// Spaceship
private Spaceship ship;
// List of UFOs
private List<Ufo> ufos = new ArrayList<Ufo>();
// List of bombs
private List<Bomb> bombs = new ArrayList<Bomb>();
// List of rockets
private List<Rocket> rockets = new ArrayList<Rocket>();
public Space(int width, int height) {
this.width = width;
this.height = height;
}
/**
* The program's main loop.
* This is where all the important actions happen
*/
public void run() {
// Create a drawing canvas.
Canvas canvas = new Canvas(width, height);
// Create a KeyboardObserver object and start it.
KeyboardObserver keyboardObserver = new KeyboardObserver();
keyboardObserver.start();
// The game continues as long as the ship is alive
while (ship.isAlive()) {
// Does the observer have any key events?
if (keyboardObserver.hasKeyEvents()) {
KeyEvent event = keyboardObserver.getEventFromTop();
// If "left arrow", then move the game piece to the left
System.out.print(event.getKeyCode());
if (event.getKeyCode() == KeyEvent.VK_LEFT)
ship.moveLeft();
// If "right arrow", then move the game piece to the right
else if (event.getKeyCode() == KeyEvent.VK_RIGHT)
ship.moveRight();
// If "space", then launch a ball
else if (event.getKeyCode() == KeyEvent.VK_SPACE)
ship.fire();
}
// Move all of the objects in the game
moveAllItems();
// Detect collisions
checkBombs();
checkRockets();
// Remove dead objects from the list
removeDead();
// Create a UFO (once in every 10 calls)
createUfo();
// Draw all the objects on the canvas and display the canvas on the screen
canvas.clear();
draw(canvas);
canvas.print();
// Pause 300 milliseconds
Space.sleep(300);
}
// Display "Game Over"
System.out.println("Game Over!");
}
/**
* Move all of the objects in the game
*/
public void moveAllItems() {
// Get a list of all objects in the game and call the move() method on each of them.
for (BaseObject bo : getAllItems()) {
bo.move();
} }
/**
* The method returns a single list that contains all objects in the game
*/
public List<BaseObject> getAllItems() {
// You need to create a new list and put all the game objects into it.
List<BaseObject> list =new ArrayList<>();
list.add(ship);
for (Bomb bomb : bombs) {
list.add(bomb);
}
for (Rocket rocket : rockets) {
list.add(rocket);
}
for (BaseObject ufo : ufos) {
list.add(ufo);
}
return list;
}
/**
* Create a new UFO. Once in every 10 calls.
*/
private int callCount = 0;
public void createUfo() {
// You need to create a new UFO.
// Once in every 10 calls.
Ufo u = null;
callCount++;
if (callCount == 10) {
u = new Ufo(width / 2, 0);
callCount = 0;
}
if (u != null)
ufos.add(u);
if (ufos.isEmpty())
ship = new Spaceship(width / 2, height);
}
/**
* Check the bombs.
* a) collision with a ship (the bomb and the ship die)
* b) movement beyond the bottom of the game field (the bomb dies)
*/
public void checkBombs() {
// Here you need to check all possible collisions for each bomb.
for (Bomb bomb : bombs) {
if (bomb.intersects(ship)) {
ship.die();
bomb.die();
} else if (bomb.getY() > height)
bomb.die();
}
}
/**
* Check the rockets.
* a) collision with a UFO (the rocket and the UFO die)
* b) movement beyond the top of the playing field (the rocket dies)
*/
public void checkRockets() {
// Here you need to check all possible collisions for each rocket.
for (Rocket rocket : rockets) {
for (Ufo ufo : ufos) {
if (rocket.intersects(ufo)) {
rocket.die();
ufo.die();
} else {
if (rocket.getY() <0)
rocket.die();
if (ufo.getY() > height)
ufo.die();
}
}
}
}
/**
* Remove dead objects (bombs, rockets, ufos) from the lists
*/
public void removeDead() {
// Here you need to remove all dead objects from the lists.
// Except the spaceship — we use it to determine whether the game is still
// going.
for (Bomb bomb : bombs) {
if(bomb.isAlive()==false)
bombs.remove(bomb);
}
for (Rocket rocket : rockets) {
if(rocket.isAlive()==false)
rockets.remove(rocket);
}
for (Ufo ufo : ufos) {
if(ufo.isAlive()==false)
ufos.remove(ufo);
}
}
/**
* Draw all game objects:
* a) fill the entire canvas with periods.
* b) draw all the objects on the canvas.
*/
public void draw(Canvas canvas) {
// Here you need to draw all game objects
}
public Spaceship getShip() {
return ship;
}
public void setShip(Spaceship ship) {
this.ship = ship;
}
public List<Ufo> getUfos() {
return ufos;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public List<Bomb> getBombs() {
return bombs;
}
public List<Rocket> getRockets() {
return rockets;
}
public static Space game;
public static void main(String[] args) throws Exception {
game = new Space(20, 20);
game.setShip(new Spaceship(10, 18));
game.run();
}
/**
* The method pauses for delay seconds.
*/
public static void sleep(int delay) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
}
}
}