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

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;
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<Entity>();
}
/**
* 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) {
}
}

View File

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

View File

@ -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<Entity> 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<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 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<Background> i = Background.backgrounds.iterator();
while(i.hasNext()) {
Background b = i.next();
b.update();
}
}
private void updateEntities() {
Iterator<Entity> i = Entity.entities.iterator();
while(i.hasNext()) {

View File

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