diff --git a/res/images/starbackground.png b/res/images/starbackground.png new file mode 100644 index 0000000..faae18f Binary files /dev/null and b/res/images/starbackground.png differ diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index 9dad9eb..134460f 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -2,6 +2,7 @@ package de.teamteamteam.spacescooter; import java.awt.EventQueue; +import de.teamteamteam.spacescooter.background.StarBackground; import de.teamteamteam.spacescooter.entities.Player; import de.teamteamteam.spacescooter.entities.TestEntity; import de.teamteamteam.spacescooter.gui.GameFrame; @@ -23,14 +24,14 @@ public class Main { final GameFrame gf = new GameFrame(); //Whatever. - new TestEntity(); + new StarBackground(); new Player(); //Initialize the GameFrame properly within the AWT EventQueue EventQueue.invokeLater(new Runnable() { public void run() { gf.init(); - gf.drawEntities(); //Draw nothing for the first time. + gf.draw(); //Draw nothing for the first time. } }); diff --git a/src/de/teamteamteam/spacescooter/background/Background.java b/src/de/teamteamteam/spacescooter/background/Background.java new file mode 100644 index 0000000..1f8beea --- /dev/null +++ b/src/de/teamteamteam/spacescooter/background/Background.java @@ -0,0 +1,29 @@ +package de.teamteamteam.spacescooter.background; + +import java.awt.Graphics; +import java.util.ArrayList; + +public abstract class Background { + + public static ArrayList backgrounds = null; + + /** + * We need to initialize the ArrayList, so the EntityUpdateThread won't beat us. + */ + static { + Background.backgrounds = new ArrayList(); + } + + public Background() { + Background.backgrounds.add(this); + } + + public void paint(Graphics g) { + + } + + public void update() { + //scroll shit + } + +} diff --git a/src/de/teamteamteam/spacescooter/background/StarBackground.java b/src/de/teamteamteam/spacescooter/background/StarBackground.java new file mode 100644 index 0000000..ba1dd0d --- /dev/null +++ b/src/de/teamteamteam/spacescooter/background/StarBackground.java @@ -0,0 +1,38 @@ +package de.teamteamteam.spacescooter.background; + +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import de.teamteamteam.spacescooter.entities.Player; + + +public class StarBackground extends Background { + + private static BufferedImage img; + static { + try { + img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/starbackground.png")); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private int offset = 0; + + public void update() { + this.offset -= 15; + if(Math.abs(this.offset) > this.img.getWidth()) { + this.offset += this.img.getWidth(); + } + } + + public void paint(Graphics g) { + g.drawImage(this.img, 0+this.offset, 0, this.img.getWidth(), this.img.getHeight(), null); + g.drawImage(this.img, this.img.getWidth()+this.offset, 0, this.img.getWidth(), this.img.getHeight(), null); + } + +} diff --git a/src/de/teamteamteam/spacescooter/entities/Entity.java b/src/de/teamteamteam/spacescooter/entities/Entity.java index 3957c36..e8cf00b 100644 --- a/src/de/teamteamteam/spacescooter/entities/Entity.java +++ b/src/de/teamteamteam/spacescooter/entities/Entity.java @@ -1,6 +1,8 @@ package de.teamteamteam.spacescooter.entities; import java.awt.Graphics; +import java.awt.Rectangle; +import java.awt.Shape; import java.util.ArrayList; public abstract class Entity { @@ -14,16 +16,27 @@ public abstract class Entity { Entity.entities = new ArrayList(); } + /** + * Default hitbox for every entity + */ + protected Shape HitBox; + /** * Constructor. * All entities are within a static array list for our convenience. */ public Entity() { Entity.entities.add(this); + + // } - public abstract void update(); + public void update() { + + } - public abstract void paint(Graphics g); + public void paint(Graphics g) { + + } } diff --git a/src/de/teamteamteam/spacescooter/entities/Player.java b/src/de/teamteamteam/spacescooter/entities/Player.java index c3329b0..8d85fdc 100644 --- a/src/de/teamteamteam/spacescooter/entities/Player.java +++ b/src/de/teamteamteam/spacescooter/entities/Player.java @@ -33,17 +33,18 @@ public class Player extends Entity { @Override public void update() { + int off = 3; if(Keyboard.isKeyDown(KeyEvent.VK_UP)) { - this.y -= 3; + this.y -= off; } if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) { - this.y += 3; + this.y += off; } if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) { - this.x -= 3; + this.x -= off; } if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) { - this.x += 3; + this.x += off; } } diff --git a/src/de/teamteamteam/spacescooter/gui/GameFrame.java b/src/de/teamteamteam/spacescooter/gui/GameFrame.java index 2a6dbf9..35f47a5 100644 --- a/src/de/teamteamteam/spacescooter/gui/GameFrame.java +++ b/src/de/teamteamteam/spacescooter/gui/GameFrame.java @@ -7,6 +7,7 @@ import java.util.Iterator; import javax.swing.JFrame; +import de.teamteamteam.spacescooter.background.Background; import de.teamteamteam.spacescooter.controls.Keyboard; import de.teamteamteam.spacescooter.entities.Entity; @@ -41,7 +42,7 @@ public class GameFrame extends JFrame { * The pain, do not underestimate it! * @see http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering for details. */ - public void drawEntities() { + public void draw() { this.createBufferStrategy(2); Graphics bufferedGraphics = null; BufferStrategy bufferStrategy = this.getBufferStrategy(); @@ -52,12 +53,9 @@ public class GameFrame extends JFrame { bufferedGraphics = bufferStrategy.getDrawGraphics(); // Now we can use bufferedGraphics to actually draw stuff - // ... - Iterator i = Entity.entities.iterator(); - while (i.hasNext()) { - Entity e = i.next(); - e.paint(bufferedGraphics); - } + this.drawBackgrounds(bufferedGraphics); + this.drawEntities(bufferedGraphics); + } catch(Exception e) { System.out.println("Hier geht was schief"); System.out.println(e); @@ -73,4 +71,22 @@ public class GameFrame extends JFrame { } + public void drawBackgrounds(Graphics g) { + Iterator i = Background.backgrounds.iterator(); + while (i.hasNext()) { + Background b = i.next(); + b.paint(g); + } + } + + public void drawEntities(Graphics g) { + Iterator i = Entity.entities.iterator(); + while (i.hasNext()) { + Entity e = i.next(); + e.paint(g); + } + } + + + } diff --git a/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java b/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java index 74b1b0f..1cfe4bc 100644 --- a/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java +++ b/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java @@ -2,6 +2,7 @@ package de.teamteamteam.spacescooter.threads; import java.util.Iterator; +import de.teamteamteam.spacescooter.background.Background; import de.teamteamteam.spacescooter.entities.Entity; import de.teamteamteam.spacescooter.gui.GameFrame; @@ -24,10 +25,19 @@ public class EntityUpdateThread extends Thread { } catch (InterruptedException e) { System.err.println(e); } + this.updateBackgrounds(); this.updateEntities(); } } + private void updateBackgrounds() { + Iterator i = Background.backgrounds.iterator(); + while(i.hasNext()) { + Background b = i.next(); + b.update(); + } + } + private void updateEntities() { Iterator i = Entity.entities.iterator(); while(i.hasNext()) { diff --git a/src/de/teamteamteam/spacescooter/threads/PaintThread.java b/src/de/teamteamteam/spacescooter/threads/PaintThread.java index a8d5cb8..0bf7741 100644 --- a/src/de/teamteamteam/spacescooter/threads/PaintThread.java +++ b/src/de/teamteamteam/spacescooter/threads/PaintThread.java @@ -26,7 +26,7 @@ public class PaintThread extends Thread { //Trigger redrawing the things. Important: AWT-Context needed here! EventQueue.invokeLater(new Runnable() { public void run() { - gf.drawEntities(); + gf.draw(); } }); }