Inhaltsverzeichnis
Projekt: Sidescroller-Game
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"); public Sidescroller() { super(); // Hier startet das Programm raumschiff = new Raumschiff(this); } 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(Math.random() < 0.05) { Enemy e = new Enemy(); enemies.add(e); } } public void updateText() { pointsText.setText(points + " Punkte"); } 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) { 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 = 0; 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(); } } }