Jetzt neu, mit Hintergründen.

This commit is contained in:
Jan Philipp Timme 2014-10-21 14:53:51 +02:00
parent 0fd68d8b44
commit 475bdb73f4
9 changed files with 124 additions and 16 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 252 KiB

View File

@ -2,6 +2,7 @@ package de.teamteamteam.spacescooter;
import java.awt.EventQueue; import java.awt.EventQueue;
import de.teamteamteam.spacescooter.background.StarBackground;
import de.teamteamteam.spacescooter.entities.Player; import de.teamteamteam.spacescooter.entities.Player;
import de.teamteamteam.spacescooter.entities.TestEntity; import de.teamteamteam.spacescooter.entities.TestEntity;
import de.teamteamteam.spacescooter.gui.GameFrame; import de.teamteamteam.spacescooter.gui.GameFrame;
@ -23,14 +24,14 @@ public class Main {
final GameFrame gf = new GameFrame(); final GameFrame gf = new GameFrame();
//Whatever. //Whatever.
new TestEntity(); new StarBackground();
new Player(); new Player();
//Initialize the GameFrame properly within the AWT EventQueue //Initialize the GameFrame properly within the AWT EventQueue
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(new Runnable() {
public void run() { public void run() {
gf.init(); gf.init();
gf.drawEntities(); //Draw nothing for the first time. gf.draw(); //Draw nothing for the first time.
} }
}); });

View File

@ -0,0 +1,29 @@
package de.teamteamteam.spacescooter.background;
import java.awt.Graphics;
import java.util.ArrayList;
public abstract class Background {
public static ArrayList<Background> backgrounds = null;
/**
* We need to initialize the ArrayList, so the EntityUpdateThread won't beat us.
*/
static {
Background.backgrounds = new ArrayList<Background>();
}
public Background() {
Background.backgrounds.add(this);
}
public void paint(Graphics g) {
}
public void update() {
//scroll shit
}
}

View File

@ -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);
}
}

View File

@ -1,6 +1,8 @@
package de.teamteamteam.spacescooter.entities; package de.teamteamteam.spacescooter.entities;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
import java.util.ArrayList; import java.util.ArrayList;
public abstract class Entity { public abstract class Entity {
@ -14,16 +16,27 @@ public abstract class Entity {
Entity.entities = new ArrayList<Entity>(); Entity.entities = new ArrayList<Entity>();
} }
/**
* Default hitbox for every entity
*/
protected Shape HitBox;
/** /**
* Constructor. * Constructor.
* All entities are within a static array list for our convenience. * All entities are within a static array list for our convenience.
*/ */
public Entity() { public Entity() {
Entity.entities.add(this); Entity.entities.add(this);
//
} }
public abstract void update(); public void update() {
}
public abstract void paint(Graphics g); public void paint(Graphics g) {
}
} }

View File

@ -33,17 +33,18 @@ public class Player extends Entity {
@Override @Override
public void update() { public void update() {
int off = 3;
if(Keyboard.isKeyDown(KeyEvent.VK_UP)) { if(Keyboard.isKeyDown(KeyEvent.VK_UP)) {
this.y -= 3; this.y -= off;
} }
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) { if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) {
this.y += 3; this.y += off;
} }
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) { if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) {
this.x -= 3; this.x -= off;
} }
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) { if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) {
this.x += 3; this.x += off;
} }
} }

View File

@ -7,6 +7,7 @@ import java.util.Iterator;
import javax.swing.JFrame; import javax.swing.JFrame;
import de.teamteamteam.spacescooter.background.Background;
import de.teamteamteam.spacescooter.controls.Keyboard; import de.teamteamteam.spacescooter.controls.Keyboard;
import de.teamteamteam.spacescooter.entities.Entity; import de.teamteamteam.spacescooter.entities.Entity;
@ -41,7 +42,7 @@ public class GameFrame extends JFrame {
* The pain, do not underestimate it! * The pain, do not underestimate it!
* @see http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering for details. * @see http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering for details.
*/ */
public void drawEntities() { public void draw() {
this.createBufferStrategy(2); this.createBufferStrategy(2);
Graphics bufferedGraphics = null; Graphics bufferedGraphics = null;
BufferStrategy bufferStrategy = this.getBufferStrategy(); BufferStrategy bufferStrategy = this.getBufferStrategy();
@ -52,12 +53,9 @@ public class GameFrame extends JFrame {
bufferedGraphics = bufferStrategy.getDrawGraphics(); bufferedGraphics = bufferStrategy.getDrawGraphics();
// Now we can use bufferedGraphics to actually draw stuff // Now we can use bufferedGraphics to actually draw stuff
// ... this.drawBackgrounds(bufferedGraphics);
Iterator<Entity> i = Entity.entities.iterator(); this.drawEntities(bufferedGraphics);
while (i.hasNext()) {
Entity e = i.next();
e.paint(bufferedGraphics);
}
} catch(Exception e) { } catch(Exception e) {
System.out.println("Hier geht was schief"); System.out.println("Hier geht was schief");
System.out.println(e); System.out.println(e);
@ -73,4 +71,22 @@ public class GameFrame extends JFrame {
} }
public void drawBackgrounds(Graphics g) {
Iterator<Background> i = Background.backgrounds.iterator();
while (i.hasNext()) {
Background b = i.next();
b.paint(g);
}
}
public void drawEntities(Graphics g) {
Iterator<Entity> i = Entity.entities.iterator();
while (i.hasNext()) {
Entity e = i.next();
e.paint(g);
}
}
} }

View File

@ -2,6 +2,7 @@ package de.teamteamteam.spacescooter.threads;
import java.util.Iterator; import java.util.Iterator;
import de.teamteamteam.spacescooter.background.Background;
import de.teamteamteam.spacescooter.entities.Entity; import de.teamteamteam.spacescooter.entities.Entity;
import de.teamteamteam.spacescooter.gui.GameFrame; import de.teamteamteam.spacescooter.gui.GameFrame;
@ -24,10 +25,19 @@ public class EntityUpdateThread extends Thread {
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.err.println(e); System.err.println(e);
} }
this.updateBackgrounds();
this.updateEntities(); this.updateEntities();
} }
} }
private void updateBackgrounds() {
Iterator<Background> i = Background.backgrounds.iterator();
while(i.hasNext()) {
Background b = i.next();
b.update();
}
}
private void updateEntities() { private void updateEntities() {
Iterator<Entity> i = Entity.entities.iterator(); Iterator<Entity> i = Entity.entities.iterator();
while(i.hasNext()) { while(i.hasNext()) {

View File

@ -26,7 +26,7 @@ public class PaintThread extends Thread {
//Trigger redrawing the things. Important: AWT-Context needed here! //Trigger redrawing the things. Important: AWT-Context needed here!
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(new Runnable() {
public void run() { public void run() {
gf.drawEntities(); gf.draw();
} }
}); });
} }