diff --git a/res/images/testbackground.png b/res/images/testbackground.png new file mode 100644 index 0000000..39dd34d Binary files /dev/null and b/res/images/testbackground.png differ diff --git a/res/images/testbackground.xcf b/res/images/testbackground.xcf new file mode 100644 index 0000000..35351aa Binary files /dev/null and b/res/images/testbackground.xcf differ diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index 134460f..19e9c68 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -6,6 +6,7 @@ import de.teamteamteam.spacescooter.background.StarBackground; import de.teamteamteam.spacescooter.entities.Player; import de.teamteamteam.spacescooter.entities.TestEntity; import de.teamteamteam.spacescooter.gui.GameFrame; +import de.teamteamteam.spacescooter.threads.BasicTimer; import de.teamteamteam.spacescooter.threads.PaintThread; import de.teamteamteam.spacescooter.threads.EntityUpdateThread; @@ -26,6 +27,7 @@ public class Main { //Whatever. new StarBackground(); new Player(); + new TestEntity(); //Initialize the GameFrame properly within the AWT EventQueue EventQueue.invokeLater(new Runnable() { @@ -42,5 +44,6 @@ public class Main { //Initialize EntityUpdateThread EntityUpdateThread ut = new EntityUpdateThread(gf); ut.start(); + } } diff --git a/src/de/teamteamteam/spacescooter/background/StarBackground.java b/src/de/teamteamteam/spacescooter/background/StarBackground.java index ba1dd0d..b66073a 100644 --- a/src/de/teamteamteam/spacescooter/background/StarBackground.java +++ b/src/de/teamteamteam/spacescooter/background/StarBackground.java @@ -14,7 +14,7 @@ public class StarBackground extends Background { private static BufferedImage img; static { try { - img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/starbackground.png")); + img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/testbackground.png")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -24,7 +24,7 @@ public class StarBackground extends Background { private int offset = 0; public void update() { - this.offset -= 15; + this.offset -= 5; if(Math.abs(this.offset) > this.img.getWidth()) { this.offset += this.img.getWidth(); } diff --git a/src/de/teamteamteam/spacescooter/entities/Entity.java b/src/de/teamteamteam/spacescooter/entities/Entity.java index e8cf00b..9626adf 100644 --- a/src/de/teamteamteam/spacescooter/entities/Entity.java +++ b/src/de/teamteamteam/spacescooter/entities/Entity.java @@ -8,6 +8,7 @@ import java.util.ArrayList; public abstract class Entity { public static ArrayList entities = null; + private static boolean isEnemy = false; /** * We need to initialize the ArrayList, so the EntityUpdateThread won't beat us. @@ -21,6 +22,14 @@ public abstract class Entity { */ protected Shape HitBox; + public static boolean isEnemy() { + return isEnemy; + } + + protected void setEnemy(boolean isEnemy) { + this.isEnemy = isEnemy; + } + /** * Constructor. * All entities are within a static array list for our convenience. diff --git a/src/de/teamteamteam/spacescooter/entities/Player.java b/src/de/teamteamteam/spacescooter/entities/Player.java index 8d85fdc..1394719 100644 --- a/src/de/teamteamteam/spacescooter/entities/Player.java +++ b/src/de/teamteamteam/spacescooter/entities/Player.java @@ -34,18 +34,22 @@ public class Player extends Entity { @Override public void update() { int off = 3; - if(Keyboard.isKeyDown(KeyEvent.VK_UP)) { + if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.y > 0) { this.y -= off; } - if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) { + if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.y < (600 - this.img.getHeight())) { this.y += off; } - if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) { + if(Keyboard.isKeyDown(KeyEvent.VK_LEFT) && this.x > 0) { this.x -= off; } - if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) { + if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT) && this.x < (800 - this.img.getWidth())) { this.x += off; } + + if (Entity.isEnemy()) { + System.out.print("hit"); + } } diff --git a/src/de/teamteamteam/spacescooter/entities/TestEntity.java b/src/de/teamteamteam/spacescooter/entities/TestEntity.java index 606eb61..1410afd 100644 --- a/src/de/teamteamteam/spacescooter/entities/TestEntity.java +++ b/src/de/teamteamteam/spacescooter/entities/TestEntity.java @@ -3,31 +3,28 @@ package de.teamteamteam.spacescooter.entities; import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyEvent; -import java.util.Random; import de.teamteamteam.spacescooter.controls.Keyboard; public class TestEntity extends Entity { private Color color; - private Random random; public TestEntity() { super(); + this.setEnemy(true); this.color = new Color(255, 0, 0); - this.random = new Random(); } public void paint(Graphics g) { g.setColor(this.color); - g.drawRect(100, 200, 300, 300); + g.fillRect(300, 300, 100, 100); } public void update() { if(Keyboard.isKeyDown(KeyEvent.VK_SPACE)) { System.out.println("Hallo Welt!"); } - this.color = new Color(this.random.nextInt(255),this.random.nextInt(255),this.random.nextInt(255)); } } diff --git a/src/de/teamteamteam/spacescooter/threads/BasicTimer.java b/src/de/teamteamteam/spacescooter/threads/BasicTimer.java new file mode 100644 index 0000000..a47ae4e --- /dev/null +++ b/src/de/teamteamteam/spacescooter/threads/BasicTimer.java @@ -0,0 +1,48 @@ +package de.teamteamteam.spacescooter.threads; + +public class BasicTimer { + private int fps; + private long timeThen; + boolean newVersion = true; + public BasicTimer(int frameRate) { + if (System.getProperty("java.version").startsWith("1.4")) + newVersion = false; + if (newVersion) { + fps = frameRate; + timeThen = System.nanoTime(); + } + else { + fps = frameRate; + System.out.println("Old Version Detected: Running Old Java Timer Version"); + timeThen = System.currentTimeMillis(); + } + } + public void changeFPS(int frameRate) { + fps = frameRate; + } + public void sync() { + if (newVersion) { + long gapTo = 1000000000L / fps + timeThen; + long timeNow = System.nanoTime(); + + while (gapTo > timeNow) { + try { Thread.sleep(1); + } catch (InterruptedException e) {} + timeNow = System.nanoTime(); + } + + timeThen = timeNow; + } else { + long gapTo = 1000 / fps + timeThen; + long timeNow = System.currentTimeMillis(); + + while (gapTo > timeNow) { + try { Thread.sleep(1); + } catch (InterruptedException e) {} + timeNow = System.currentTimeMillis(); + } + + timeThen = timeNow; + } + } +} \ No newline at end of file diff --git a/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java b/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java index 1cfe4bc..d1ce30f 100644 --- a/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java +++ b/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java @@ -13,6 +13,7 @@ import de.teamteamteam.spacescooter.gui.GameFrame; public class EntityUpdateThread extends Thread { private GameFrame gf; + BasicTimer timer = new BasicTimer(60); public EntityUpdateThread(GameFrame gf) { this.gf = gf; @@ -20,11 +21,7 @@ public class EntityUpdateThread extends Thread { public void run() { while (true) { - try { - Thread.sleep(16); - } catch (InterruptedException e) { - System.err.println(e); - } + timer.sync(); this.updateBackgrounds(); this.updateEntities(); } diff --git a/src/de/teamteamteam/spacescooter/threads/PaintThread.java b/src/de/teamteamteam/spacescooter/threads/PaintThread.java index 0bf7741..e01b3e5 100644 --- a/src/de/teamteamteam/spacescooter/threads/PaintThread.java +++ b/src/de/teamteamteam/spacescooter/threads/PaintThread.java @@ -10,6 +10,7 @@ import de.teamteamteam.spacescooter.gui.GameFrame; public class PaintThread extends Thread { private GameFrame gf; + BasicTimer timer = new BasicTimer(120); public PaintThread(GameFrame gf) { this.gf = gf; @@ -18,11 +19,7 @@ public class PaintThread extends Thread { public void run() { final GameFrame gf = this.gf; // :'-( while (true) { - try { - Thread.sleep(16); - } catch (InterruptedException e) { - System.err.println(e); - } + timer.sync(); //Trigger redrawing the things. Important: AWT-Context needed here! EventQueue.invokeLater(new Runnable() { public void run() {