diff --git a/README.md b/README.md index 3175eaf..50f0748 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,12 @@ SpaceScooter ============ This isn't the best game in the world. This is just a tribute. + + +Building with eclipse +--------------------- + +There are some minor things you need to set up to your project in eclipse: + +* Tell your eclipse to use a new version of java. +* Add the folder res/ to your *build path*! diff --git a/res/images/nyancat.png b/res/images/nyancat.png new file mode 100644 index 0000000..c26dff3 Binary files /dev/null and b/res/images/nyancat.png differ diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index 6c1ea9e..5b53aa4 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -4,28 +4,35 @@ import java.awt.EventQueue; import de.teamteamteam.spacescooter.gui.GameFrame; import de.teamteamteam.spacescooter.threads.PaintThread; -import de.teamteamteam.spacescooter.threads.UpdateThread; +import de.teamteamteam.spacescooter.threads.EntityUpdateThread; +/** + * Nothing but a class containing the main method. + */ public class Main { /** - * @param args + * Main entry point of the game. + * "... for i am the Alpha and the Omega." - God + * + * @param args Command line arguments. */ public static void main(String[] args) { final GameFrame gf = new GameFrame(); + //Initialize the GameFrame properly within the AWT EventQueue EventQueue.invokeLater(new Runnable() { public void run() { gf.init(); } }); + //Initialize PaintThread PaintThread pt = new PaintThread(gf); pt.start(); - UpdateThread ut = new UpdateThread(gf); - ut.start(); - + //Initialize EntityUpdateThread + EntityUpdateThread ut = new EntityUpdateThread(gf); + ut.start(); } - } diff --git a/src/de/teamteamteam/spacescooter/controls/Keyboard.java b/src/de/teamteamteam/spacescooter/controls/Keyboard.java new file mode 100644 index 0000000..6547b8c --- /dev/null +++ b/src/de/teamteamteam/spacescooter/controls/Keyboard.java @@ -0,0 +1,32 @@ +package de.teamteamteam.spacescooter.controls; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + + +public class Keyboard implements KeyListener { + + private static boolean[] keys = new boolean[150]; //TODO: This might kill the game in case there are keycodes > 150 + + public static boolean isKeyDown(int keyCode) { + if (keyCode >= 0 && keyCode < keys.length) { + return keys[keyCode]; + } + return false; + } + + public void keyPressed(KeyEvent e) { + keys[e.getKeyCode()] = true; + } + + public void keyReleased(KeyEvent e) { + keys[e.getKeyCode()] = false; + } + + public void keyTyped(KeyEvent e) { + //Nice for cheatcodes and eastereggs later... + //System.out.println("keyTyped: " + e); + } + + +} diff --git a/src/de/teamteamteam/spacescooter/entities/Entity.java b/src/de/teamteamteam/spacescooter/entities/Entity.java index cc0c087..596be9c 100644 --- a/src/de/teamteamteam/spacescooter/entities/Entity.java +++ b/src/de/teamteamteam/spacescooter/entities/Entity.java @@ -3,13 +3,9 @@ package de.teamteamteam.spacescooter.entities; import java.awt.Graphics; public abstract class Entity { - - public void update() { - } + public abstract void update(); - public void paint(Graphics g) { - - } + public abstract void paint(Graphics g); } diff --git a/src/de/teamteamteam/spacescooter/entities/Player.java b/src/de/teamteamteam/spacescooter/entities/Player.java new file mode 100644 index 0000000..c1f76d5 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/entities/Player.java @@ -0,0 +1,60 @@ +package de.teamteamteam.spacescooter.entities; + +import java.awt.Graphics; +import java.awt.event.KeyEvent; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.io.InputStream; + +import javax.imageio.ImageIO; + +import de.teamteamteam.spacescooter.controls.Keyboard; + +public class Player extends Entity { + + private int x; + private int y; + + private static BufferedImage img; + + static { + try { + InputStream res = Player.class.getClassLoader().getResourceAsStream("images/nyancat.png"); + if(res == null) { + System.out.println("Kein res :/"); + } + img = ImageIO.read(res); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public Player() { + this.x = 200; + this.y = 300; + } + + @Override + public void update() { + if(Keyboard.isKeyDown(KeyEvent.VK_UP)) { + this.y--; + } + if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) { + this.y++; + } + if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) { + this.x--; + } + if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) { + this.x++; + } + + } + + @Override + public void paint(Graphics g) { + g.drawImage(img, this.x, this.y, null); + } + +} diff --git a/src/de/teamteamteam/spacescooter/entities/TestEntity.java b/src/de/teamteamteam/spacescooter/entities/TestEntity.java index 493efa9..606eb61 100644 --- a/src/de/teamteamteam/spacescooter/entities/TestEntity.java +++ b/src/de/teamteamteam/spacescooter/entities/TestEntity.java @@ -2,8 +2,11 @@ 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; @@ -21,7 +24,9 @@ public class TestEntity extends Entity { } public void update() { - System.out.println("TestEntity.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/gui/GameFrame.java b/src/de/teamteamteam/spacescooter/gui/GameFrame.java index c5e47de..e35ea44 100644 --- a/src/de/teamteamteam/spacescooter/gui/GameFrame.java +++ b/src/de/teamteamteam/spacescooter/gui/GameFrame.java @@ -6,9 +6,14 @@ import java.util.Iterator; import javax.swing.JFrame; +import de.teamteamteam.spacescooter.controls.Keyboard; import de.teamteamteam.spacescooter.entities.Entity; +import de.teamteamteam.spacescooter.entities.Player; import de.teamteamteam.spacescooter.entities.TestEntity; +/** + * The game will take place in this beautiful window. + */ public class GameFrame extends JFrame { private ArrayList entities; @@ -18,22 +23,32 @@ public class GameFrame extends JFrame { this.entities = new ArrayList(); //TODO: Remove this! - TestEntity t = new TestEntity(); - this.entities.add(t); + this.entities.add(new TestEntity()); + this.entities.add(new Player()); } public ArrayList getEntityList() { return this.entities; } + /** + * Set up the GameFrame before showing it to the world. + */ public void init() { this.setTitle("Unser schöner Titel"); this.setSize(800, 600); this.setUndecorated(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); + + //Make sure we get the keyboard events. Use Keyboard.isKeyDown() to ask about keys status. + this.addKeyListener(new Keyboard()); + this.setVisible(true); } + /** + * Have the GameFrame repaint all the visible entities. + */ @Override public void paint(Graphics g) { Iterator i = this.entities.iterator(); diff --git a/src/de/teamteamteam/spacescooter/threads/UpdateThread.java b/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java similarity index 72% rename from src/de/teamteamteam/spacescooter/threads/UpdateThread.java rename to src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java index 53454e3..c8c1ff9 100644 --- a/src/de/teamteamteam/spacescooter/threads/UpdateThread.java +++ b/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java @@ -7,11 +7,15 @@ import java.util.Iterator; import de.teamteamteam.spacescooter.entities.Entity; import de.teamteamteam.spacescooter.gui.GameFrame; -public class UpdateThread extends Thread { +/** + * This thread is responsible for updating all the entities. + * It runs about 60 times per second. + */ +public class EntityUpdateThread extends Thread { private GameFrame gf; - public UpdateThread(GameFrame gf) { + public EntityUpdateThread(GameFrame gf) { this.gf = gf; } @@ -26,7 +30,7 @@ public class UpdateThread extends Thread { } } - public void updateEntities() { + private void updateEntities() { ArrayList entityList = this.gf.getEntityList(); Iterator i = entityList.iterator(); while(i.hasNext()) {