====== Projekt: Sidescroller-Game (Stand nach der 2. Hausaufgabe) ======
Hier die Programmdateien Stand 26.03.2020:
==== Die Klasse Sidescroller ====
World w = new World(800, 600);
new Sidescroller();
class Sidescroller extends Actor {
Raumschiff raumschiff;
Group bullets = new Group();
Group enemies = 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");
public Sidescroller() {
super();
// Hier startet das Programm
raumschiff = new Raumschiff(this);
livesText.setAlignment(Alignment.right);
}
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() {
generateEnemies();
checkCollisionsBulletsWithEnemies();
}
private void generateEnemies() {
if(Math.random() < 0.05) {
Enemy e = new Enemy();
enemies.add(e);
}
}
private void checkCollisionsBulletsWithEnemies() {
CollisionPair[] pairs = bullets.getCollisionPairs(enemies, true);
for(int i = 0; i < pairs.length; i++) {
CollisionPair cp = pairs[i];
cp.shapeA.destroy();
Rectangle r = (Rectangle)cp.shapeB;
double le = Math.sqrt(r.getWidth() * r.getHeight());
new Explosion(r.getCenterX(), r.getCenterY(), le/50);
cp.shapeB.destroy();
}
}
public void updateText() {
pointsText.setText(points + " Punkte");
livesText.setText(lives + " Leben");
}
public void addPoints(int p) {
points = points + p;
updateText();
}
}
==== Die Klasse Raumschiff ====
class Raumschiff extends Sprite {
double v = 6;
Sidescroller sidescroller;
int timeSinceLastBullet = 0;
public Raumschiff(Sidescroller s) {
super(80, 270, SpriteLibrary.Ship_Adrian, 2);
scale(2);
sidescroller = s;
}
public void act() {
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.shoot);
if(timeSinceLastBullet > 30) {
sidescroller.addBullet(getCenterX() + 15, getCenterY(), 15, 0, 10);
} else {
sidescroller.addBullet(getCenterX() + 15, getCenterY(), 15, 0, 5);
}
timeSinceLastBullet = 0;
}
}
}
}
==== Die Klasse 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();
}
}
}
==== Die Klasse Enemy ====
class Enemy extends Rectangle {
double vx;
double vy;
double vw;
public Enemy() {
super(850, Math.random() * 600, Math.random() * 250 + 50, Math.random() * 250 + 50);
vy = Math.random()*5 - 2.5;
vx = -Math.random() * 5 - 5;
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);
setFillColor(Color.fromRGBA(red, green, blue, Math.random() * 0.7 + 0.3));
}
public void act() {
move(vx, vy);
rotate(vw);
if(getCenterX() < - 1000) {
destroy();
}
}
}