Danke schon mal. Ich werde es dann mal mit der Klasse Point versuchen.
Also, ich habe eine Basisklasse Alien, die ein par Methoden zur Verfügung stellt, die alle Alientypen brauchen.
Die Klasse sieht so aus:
[Java]
package aliens;
import java.awt.*;
import java.util.*;
public class Aliens {
/**
* Calculates randomly in which direction an object moves.
*
* @param currentPosition
* @return if it moves upwards it returns a positive, if downwards a negative number.
*/
private static int calculateMovingDirection(int currentPosition){
int randomDirection = (int)(Math.random()*100+1);
if(randomDirection <= 50){
return -1;
}
else{
return 1;
}
}
protected void setNewXCoordinates(ArrayList<Integer> coordinate, int speedX){
for(int i = 0; i < coordinate.size(); i++){
if(calculateMovingDirection(0) > 0){
int position = coordinate.get(i) - speedX;
coordinate.remove(i);
coordinate.add(i, position);
}
else{
int position = coordinate.get(i) + speedX;
coordinate.remove(i);
coordinate.add(i, position);
}
}
}
protected ArrayList setNewYCoordinates(ArrayList<Integer> coordinate, int speedY){
for(int i = 0; i <= coordinate.size(); i++){
if(calculateMovingDirection(0) > 0){
int position = coordinate.get(i) - speedY;
coordinate.remove(i);
coordinate.add(i, position);
}
else{
int position = coordinate.get(i) + speedY;
coordinate.remove(i);
coordinate.add(i, position);
}
}
return coordinate;
}
/*
protected void setNewYCoordinates(ArrayList<Integer> coordinate, int speedY){
//System.out.println("setNewYCoordinates called");
for(int i = 0; i < coordinate.size(); i++){
if(calculateMovingDirection(0) > 0){
int position = coordinate.get(i) - speedY;
coordinate.remove(i);
coordinate.add(i, position);
}
else{
int position = coordinate.get(i) + speedY;
coordinate.remove(i);
coordinate.add(i, position);
}
}
}
*/
/**
* Calculates randomly if a ship is going to shoot or not
* @return true if it shoots, false if it does not
*/
protected static boolean shipShoots(){
int randomShoot = (int)(Math.random()*100+1);
if(randomShoot == 10 || randomShoot == 50 || randomShoot == 40){
return true;
}
else{
return false;
}
}
/**
* Calculates randomly if a new ship is created or not
*
* @param currentAmountOfShips, the current amount of ships in the space
* @return true if a new ship is created, else false
*/
protected static boolean shipGoingToBeCreated(int currentAmountOfShips){
boolean status;
if(currentAmountOfShips <= 2){
status = true;
}
else if(currentAmountOfShips >= 10){
status = false;
}
else{
int randomShip = (int)(Math.random()*50+1);
if(randomShip == 5 || randomShip == 10){
status = true;
}
else{
status = false;
}
}
return status;
}
protected int calculateXCoordinatesNewShip(){
return 800;
}
protected int calculateYCoordinatesNewShip(){
return (int)(Math.random()*500-50);
}
/**
* Method to paint the ships of a class that inherits this method
*
* @param xCoordinates, ArrayList in which the coordinates are stored
* @param yCoordinates, ArrayList in which the coordinates are stored
* @param g, The Graphics object
* @param c, The Color of the Ship
* @param width
* @param height
*/
protected static void paintShips(ArrayList<Integer> xCoordinates,
ArrayList<Integer> yCoordinates,
Graphics g, Color c,
int width, int height){
for(int i = 0; i < xCoordinates.size(); i++){
g.fillRect(xCoordinates.get(i), yCoordinates.get(i), width, height);
}
}
}
[/Java]
Ausserdem ist da noch die erste Alientyp-Klassen Trins. Das soll das erste Alien sein.
[Java]
package aliens;
import java.awt.Color;
import java.awt.Graphics;
import java.util.*;
import tools.*;
public class Trins
extends Aliens
implements Updatable, Createable, Destroyable, Movable{
private final static int initialStructure = 50;
private final static int speedX = 5;
private final static int speedY = 2;
private static int currentAmountOfShips = 0;
private final static int shipDimension = 100;
private final static Color shipColor = Color.red;
//private final static int shotPower = 10;
//Attributes which store values during the game in Lists
private ArrayList<Integer> xCoordinates = new ArrayList<Integer>();
private ArrayList<Integer> yCoordinates = new ArrayList<Integer>();
private ArrayList<Integer> currentStructure = new ArrayList<Integer>();
//done: Updatable, Creatable, Movable
//missing: Destroyable
@Override
public void updateObjects(){
if(super.shipGoingToBeCreated(currentAmountOfShips)){
createNewObject();
currentAmountOfShips++;
}
setNewXCoordinates(xCoordinates, speedX);
setNewYCoordinates(yCoordinates, speedY);
}
@Override
public void createNewObject(){
xCoordinates.add(calculateXCoordinatesNewShip());
yCoordinates.add(calculateYCoordinatesNewShip());
}
@Override
public int getStructure(int shipNumber) {
return currentStructure.get(shipNumber);
}
@Override
public void removeDestroyedObject(int shipNumber) {
xCoordinates.remove(shipNumber);
yCoordinates.remove(shipNumber);
currentStructure.remove(shipNumber);
}
@Override
public void setDamage(int shipNumber, int damage) {
int newStructure = currentStructure.get(shipNumber)-damage;
currentStructure.set(shipNumber, newStructure);
}
@Override
public int getSpeed(int shipNumber) {
return 5;
}
@Override
public int getXCoordinate(int shipNumber) {
return xCoordinates.get(shipNumber);
}
@Override
public int getYCoordinate(int shipNumber) {
return yCoordinates.get(shipNumber);
}
@Override
public void paintObjects(Graphics g) {
//System.out.println("paintObjects called");
paintShips(xCoordinates, yCoordinates, g, shipColor, shipDimension, shipDimension);
}
}
[/Java]
Das ganze lauft in verschiedenen Threads ab. Einer aktualisiert das Bild all 30 ms, der andere führt die Berechnungen für neue Schiffe und alles durch. Aber wie gesagt, das speichern funktioniert irgendwie nicht.