Bullet
class Bullet extends Circle {
double vx;
double vy;
public Bullet(double x, double y, double vx1, double vy1, double radius) {
super(x, y, radius);
vx = vx1;
vy = vy1;
setFillColor(Color.white);
}
public void act() {
move(vx, vy);
if(isOutsideView()) {
destroy();
}
}
}
Spaceship
class Spaceship extends Sprite {
double v = 6;
Sidescroller sidescroller;
int timeSinceLastBullet = 0;
public Spaceship(Sidescroller s) {
super(80, 270, SpriteLibrary.Ship_Adrian, 2);
scale(2);
sidescroller = s;
}
public void act() {
if(sidescroller.state != 1) return;
if(isKeyDown(Key.ArrowUp)) {
if(getCenterY() > 30) {
move(0, -v);
}
}
if(isKeyDown(Key.ArrowDown)) {
if(getCenterY() < 570) {
move(0, v);
}
}
if(isKeyDown(Key.ArrowRight)) {
if(getCenterX() < 370) {
move(v, 0);
}
}
if(isKeyDown(Key.ArrowLeft)) {
if(getCenterX() > 80) {
move(-v, 0);
}
}
timeSinceLastBullet++;
if(isKeyDown(" ")) {
if(timeSinceLastBullet > 5) {
System.playSound(Sound.laser_shoot);
double r = timeSinceLastBullet / 30 * 10;
if(r > 40) r = 40;
if(r < 5) r = 5;
sidescroller.addBullet(getCenterX() + 15, getCenterY(), 15, 0, r);
timeSinceLastBullet = 0;
}
}
}
}
World w = new World(800, 600);
new Sidescroller();
class Sidescroller extends Actor {
Spaceship spaceship;
Group bullets = new Group();
Group flyingObjects = new Group();
int points = 0;
Text pointsText = new Text(5, 5, 40, "0 Punkte");
int lives = 3;
Text livesText = new Text(795, 5, 40, lives + " Leben");
Text großerText = new Text(400, 200, 60, "");
Text kleinerText = new Text(400, 300, 40, "Press s to start!");
// zustand == 0 bedeutet: Warten auf Spielbeginn
// zustand == 1 bedeutet: im Spiel
// zustand == 2 bedeutet: zwischen zwei Spielen
int state = 0;
public Sidescroller() {
super();
// Hier startet das Programm
spaceship = new Spaceship(this);
livesText.setAlignment(Alignment.right);
großerText.setAlignment(Alignment.center);
kleinerText.setAlignment(Alignment.center);
setState(0);
}
public void onKeyTyped(String key) {
if(key == "s" && state != 1) {
setState(1);
}
}
public void addBullet(double x, double y, double vx, double vy, double radius) {
Bullet b = new Bullet(x, y, vx, vy, radius);
bullets.add(b);
}
public void act() {
if(state == 1) {
generateEnemies();
generatePowerups();
checkCollisionsBulletsWithEnemies();
checkCollisionSpaceshipWithEnemies();
}
}
private void checkCollisionSpaceshipWithEnemies() {
Shape[] collidingEnemies = flyingObjects.getCollidingShapes(spaceship);
if(collidingEnemies.length > 0) {
if(collidingEnemies[0] instanceof Enemy) {
Enemy e =(Enemy)collidingEnemies[0];
double le = Math.sqrt(e.getWidth() * e.getHeight());
new Explosion(e.getCenterX(), e.getCenterY(), le / 50);
e.destroy();
lives--;
if(lives == 0) {
System.playSound(Sound.far_bomb);
setState(2);
} else {
System.playSound(Sound.cannon_boom);
}
} else {
Powerup pu = (Powerup)collidingEnemies[0];
lives++;
pu.destroy();
}
updateText();
}
}
private void generateEnemies() {
double p = 0.03 * Math.sqrt(points) / 100;
if(p < 0.04) p = 0.04;
if(p > 0.3) p = 0.3;
if(Math.random() < p) {
Enemy e = new Enemy(this);
flyingObjects.add(e);
}
}
private void generatePowerups() {
if(Math.random() < 0.005) {
Powerup p = new Powerup();
flyingObjects.add(p);
}
}
private void checkCollisionsBulletsWithEnemies() {
CollisionPair[] pairs = bullets.getCollisionPairs(flyingObjects, true);
for(int i = 0; i < pairs.length; i++) {
CollisionPair cp = pairs[i];
if(cp.shapeB instanceof Enemy) {
Enemy enemy = (Enemy)cp.shapeB;
double le = Math.sqrt(enemy.getWidth() * enemy.getHeight());
points += Math.round(200000 / enemy.getFläche() * enemy.getGeschwindigkeit());
updateText();
if(enemy.alpha > 0.2) {
enemy.alpha = enemy.alpha - 0.2;
enemy.setAlpha(enemy.alpha);
} else {
new Explosion(enemy.getCenterX(), enemy.getCenterY(), le / 50);
System.playSound(Sound.shoot);
enemy.destroy();
}
Bullet b = (Bullet)cp.shapeA;
double r = b.getRadius();
if(r <= 5) {
b.destroy();
} else {
int anzahl = Math.round(r / 5.0);
for(int i = 0; i < anzahl; i++) {
addBullet(b.getCenterX(), b.getCenterY(), Math.random() * 10 + 10, Math.random() * 30 - 15, 5);
}
b.destroy();
}
}
// double r = b.getRadius() - 5;
// if(r <= 0) {
// b.destroy();
// } else {
// b.setRadius(r);
// }
}
}
public void updateText() {
pointsText.setText(points + " Punkte");
livesText.setText(lives + " Leben");
}
public void addPoints(int p) {
points = points + p;
updateText();
}
private void setState(int newState) {
if(newState == 0) {
spaceship.setVisible(false);
großerText.setText("Spielname");
großerText.setFillColor(Color.darkgreen);
großerText.setVisible(true);
kleinerText.setVisible(true);
} else if(newState == 1) {
spaceship.setVisible(true);
lives = 3;
points = 0;
spaceship.setCenter(100, 300);
updateText();
großerText.setVisible(false);
kleinerText.setVisible(false);
} else if(newState == 2) {
spaceship.setVisible(false);
großerText.setText("Game over");
großerText.setFillColor(Color.darkred);
großerText.setVisible(true);
kleinerText.setVisible(true);
}
state = newState;
}
}
Explosion
class Explosion extends Sprite {
public Explosion(double x, double y, double size) {
super(x, y, SpriteLibrary.Explosion_1, 0);
playAnimation(0, 60, RepeatType.once, 20);
scale(size);
}
}
Enemy
class Enemy extends Rectangle {
double vx;
double vy;
double vw;
double alpha;
public Enemy(Sidescroller s) {
super(850, Math.random() * 600, Math.random() * 250 + 50, Math.random() * 250 + 50);
double vMax = s.points / 1000.0 * 5;
if(vMax < 2) {
vMax = 2;
}
if(vMax > 10) {
vMax = 10;
}
vy = Math.random() * vMax - vMax / 2;
vx = -Math.random() * vMax - vMax / 2;
vw = Math.random() * 10 - 5;
int red = Math.floor(Math.random() * 200 + 56);
int green = Math.floor(Math.random() * 200 + 56);
int blue = Math.floor(Math.random() * 200 + 56);
alpha = Math.random() * 0.7 + 0.3;
setFillColor(Color.fromRGBA(red, green, blue, alpha));
}
public void act() {
move(vx, vy);
rotate(vw);
if(getCenterX() < - 1000) {
destroy();
}
}
public double getGeschwindigkeit() {
return Math.sqrt(vx * vx + vy * vy);
}
public double getFläche() {
return getWidth() * getHeight();
}
}
Powerup
class Powerup extends Circle {
double vx;
double vy;
public Powerup() {
super(850, Math.random() * 500 + 50, 10);
vx = Math.random() *(-5) - 5;
vy = Math.random() * 5 - 2.5;
setFillColor(null);
setBorderColor(Color.red);
}
public void act() {
move(vx, vy);
if(getCenterX() < - 300) {
destroy();
}
}
}