* Player bleibt nun im Spelfeld
* Anfang der Gegner Kollision * Experimenteller Timmer
This commit is contained in:
parent
475bdb73f4
commit
5181be3619
Binary file not shown.
After Width: | Height: | Size: 51 KiB |
Binary file not shown.
|
@ -6,6 +6,7 @@ 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;
|
||||||
|
import de.teamteamteam.spacescooter.threads.BasicTimer;
|
||||||
import de.teamteamteam.spacescooter.threads.PaintThread;
|
import de.teamteamteam.spacescooter.threads.PaintThread;
|
||||||
import de.teamteamteam.spacescooter.threads.EntityUpdateThread;
|
import de.teamteamteam.spacescooter.threads.EntityUpdateThread;
|
||||||
|
|
||||||
|
@ -26,6 +27,7 @@ public class Main {
|
||||||
//Whatever.
|
//Whatever.
|
||||||
new StarBackground();
|
new StarBackground();
|
||||||
new Player();
|
new Player();
|
||||||
|
new TestEntity();
|
||||||
|
|
||||||
//Initialize the GameFrame properly within the AWT EventQueue
|
//Initialize the GameFrame properly within the AWT EventQueue
|
||||||
EventQueue.invokeLater(new Runnable() {
|
EventQueue.invokeLater(new Runnable() {
|
||||||
|
@ -42,5 +44,6 @@ public class Main {
|
||||||
//Initialize EntityUpdateThread
|
//Initialize EntityUpdateThread
|
||||||
EntityUpdateThread ut = new EntityUpdateThread(gf);
|
EntityUpdateThread ut = new EntityUpdateThread(gf);
|
||||||
ut.start();
|
ut.start();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ public class StarBackground extends Background {
|
||||||
private static BufferedImage img;
|
private static BufferedImage img;
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/starbackground.png"));
|
img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/testbackground.png"));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -24,7 +24,7 @@ public class StarBackground extends Background {
|
||||||
private int offset = 0;
|
private int offset = 0;
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
this.offset -= 15;
|
this.offset -= 5;
|
||||||
if(Math.abs(this.offset) > this.img.getWidth()) {
|
if(Math.abs(this.offset) > this.img.getWidth()) {
|
||||||
this.offset += this.img.getWidth();
|
this.offset += this.img.getWidth();
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import java.util.ArrayList;
|
||||||
public abstract class Entity {
|
public abstract class Entity {
|
||||||
|
|
||||||
public static ArrayList<Entity> entities = null;
|
public static ArrayList<Entity> entities = null;
|
||||||
|
private static boolean isEnemy = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We need to initialize the ArrayList, so the EntityUpdateThread won't beat us.
|
* We need to initialize the ArrayList, so the EntityUpdateThread won't beat us.
|
||||||
|
@ -21,6 +22,14 @@ public abstract class Entity {
|
||||||
*/
|
*/
|
||||||
protected Shape HitBox;
|
protected Shape HitBox;
|
||||||
|
|
||||||
|
public static boolean isEnemy() {
|
||||||
|
return isEnemy;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setEnemy(boolean isEnemy) {
|
||||||
|
this.isEnemy = isEnemy;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
* All entities are within a static array list for our convenience.
|
* All entities are within a static array list for our convenience.
|
||||||
|
|
|
@ -34,18 +34,22 @@ public class Player extends Entity {
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
int off = 3;
|
int off = 3;
|
||||||
if(Keyboard.isKeyDown(KeyEvent.VK_UP)) {
|
if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.y > 0) {
|
||||||
this.y -= off;
|
this.y -= off;
|
||||||
}
|
}
|
||||||
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) {
|
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.y < (600 - this.img.getHeight())) {
|
||||||
this.y += off;
|
this.y += off;
|
||||||
}
|
}
|
||||||
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) {
|
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT) && this.x > 0) {
|
||||||
this.x -= off;
|
this.x -= off;
|
||||||
}
|
}
|
||||||
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) {
|
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT) && this.x < (800 - this.img.getWidth())) {
|
||||||
this.x += off;
|
this.x += off;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Entity.isEnemy()) {
|
||||||
|
System.out.print("hit");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,31 +3,28 @@ package de.teamteamteam.spacescooter.entities;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import de.teamteamteam.spacescooter.controls.Keyboard;
|
import de.teamteamteam.spacescooter.controls.Keyboard;
|
||||||
|
|
||||||
public class TestEntity extends Entity {
|
public class TestEntity extends Entity {
|
||||||
|
|
||||||
private Color color;
|
private Color color;
|
||||||
private Random random;
|
|
||||||
|
|
||||||
public TestEntity() {
|
public TestEntity() {
|
||||||
super();
|
super();
|
||||||
|
this.setEnemy(true);
|
||||||
this.color = new Color(255, 0, 0);
|
this.color = new Color(255, 0, 0);
|
||||||
this.random = new Random();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paint(Graphics g) {
|
public void paint(Graphics g) {
|
||||||
g.setColor(this.color);
|
g.setColor(this.color);
|
||||||
g.drawRect(100, 200, 300, 300);
|
g.fillRect(300, 300, 100, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
if(Keyboard.isKeyDown(KeyEvent.VK_SPACE)) {
|
if(Keyboard.isKeyDown(KeyEvent.VK_SPACE)) {
|
||||||
System.out.println("Hallo Welt!");
|
System.out.println("Hallo Welt!");
|
||||||
}
|
}
|
||||||
this.color = new Color(this.random.nextInt(255),this.random.nextInt(255),this.random.nextInt(255));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,6 +13,7 @@ import de.teamteamteam.spacescooter.gui.GameFrame;
|
||||||
public class EntityUpdateThread extends Thread {
|
public class EntityUpdateThread extends Thread {
|
||||||
|
|
||||||
private GameFrame gf;
|
private GameFrame gf;
|
||||||
|
BasicTimer timer = new BasicTimer(60);
|
||||||
|
|
||||||
public EntityUpdateThread(GameFrame gf) {
|
public EntityUpdateThread(GameFrame gf) {
|
||||||
this.gf = gf;
|
this.gf = gf;
|
||||||
|
@ -20,11 +21,7 @@ public class EntityUpdateThread extends Thread {
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
timer.sync();
|
||||||
Thread.sleep(16);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
System.err.println(e);
|
|
||||||
}
|
|
||||||
this.updateBackgrounds();
|
this.updateBackgrounds();
|
||||||
this.updateEntities();
|
this.updateEntities();
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import de.teamteamteam.spacescooter.gui.GameFrame;
|
||||||
public class PaintThread extends Thread {
|
public class PaintThread extends Thread {
|
||||||
|
|
||||||
private GameFrame gf;
|
private GameFrame gf;
|
||||||
|
BasicTimer timer = new BasicTimer(120);
|
||||||
|
|
||||||
public PaintThread(GameFrame gf) {
|
public PaintThread(GameFrame gf) {
|
||||||
this.gf = gf;
|
this.gf = gf;
|
||||||
|
@ -18,11 +19,7 @@ public class PaintThread extends Thread {
|
||||||
public void run() {
|
public void run() {
|
||||||
final GameFrame gf = this.gf; // :'-(
|
final GameFrame gf = this.gf; // :'-(
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
timer.sync();
|
||||||
Thread.sleep(16);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
System.err.println(e);
|
|
||||||
}
|
|
||||||
//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() {
|
||||||
|
|
Loading…
Reference in New Issue