From 2be8fa2becdf43743fac132721bfcb5eafdce009 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 21 Oct 2014 12:58:44 +0200 Subject: [PATCH] Erster, sehr simpler Player mit buntem Bild funktioniert. --- README.md | 9 +++ res/images/nyancat.png | Bin 0 -> 456 bytes src/de/teamteamteam/spacescooter/Main.java | 19 ++++-- .../spacescooter/controls/Keyboard.java | 32 ++++++++++ .../spacescooter/entities/Entity.java | 8 +-- .../spacescooter/entities/Player.java | 60 ++++++++++++++++++ .../spacescooter/entities/TestEntity.java | 7 +- .../spacescooter/gui/GameFrame.java | 19 +++++- ...ateThread.java => EntityUpdateThread.java} | 10 ++- 9 files changed, 146 insertions(+), 18 deletions(-) create mode 100644 res/images/nyancat.png create mode 100644 src/de/teamteamteam/spacescooter/controls/Keyboard.java create mode 100644 src/de/teamteamteam/spacescooter/entities/Player.java rename src/de/teamteamteam/spacescooter/threads/{UpdateThread.java => EntityUpdateThread.java} (72%) 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 0000000000000000000000000000000000000000..c26dff3c99fd5f6484d1f37b3978632ea7cbe069 GIT binary patch literal 456 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_>3?$zOPHh5GjKx9jP7LeL$-D$|v;urWTp0|3 zEQbFK3~9#y{~OO_nE8L^Odw_eQvYZEH~!D?|IEz)K=S{85!o#-Kt_}V`2{mLJiCzw z;v{*yyD)UH%6b4foCO|{#S9GG!XV7ZFl&wkP>{XE)7O>#9+xPWhD>ja`~jdIE>9Q7 z5R21qC)su#HV|+tZWOu9!=lLXPMm3FErYxQdw}thn^sr1o!-V|e#o37XJhIIQK1PQ zYj?ltsgqHE8T?dH;K$3!$E0+{^6%}Luq^YMZ<;LkzFDW(MZPTNOn!Jiyg0#Pk=t~M zVz7OE#c7iS->i}@d2prj(Ij4}a|=H9FDv#~&A4KQa`EX2 zj3UKzI=ZxlmCyLHocuD)$ZDqSXYM5`NiQb{H=bQ5&~eFf8jsMSGYcl(txErXf74BW zvuHNo5cPe`W*HnebGRM1|8$$Uga6a=t6Q#%6>)oIUd^#^EjWDs_qnN`5)Btkob{gP qhe9^9%MSC?8p>xc?tj<$kGVAS(#FR}o*V=Q6oaR$pUXO@geCyQ?Y7|n literal 0 HcmV?d00001 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()) {