Erster, sehr simpler Player mit buntem Bild funktioniert.
This commit is contained in:
parent
5ec52879ac
commit
2be8fa2bec
@ -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*!
|
||||
|
BIN
res/images/nyancat.png
Normal file
BIN
res/images/nyancat.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 456 B |
@ -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);
|
||||
//Initialize EntityUpdateThread
|
||||
EntityUpdateThread ut = new EntityUpdateThread(gf);
|
||||
ut.start();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
32
src/de/teamteamteam/spacescooter/controls/Keyboard.java
Normal file
32
src/de/teamteamteam/spacescooter/controls/Keyboard.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -4,12 +4,8 @@ import java.awt.Graphics;
|
||||
|
||||
public abstract class Entity {
|
||||
|
||||
public void update() {
|
||||
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
|
||||
}
|
||||
public abstract void update();
|
||||
|
||||
public abstract void paint(Graphics g);
|
||||
|
||||
}
|
||||
|
60
src/de/teamteamteam/spacescooter/entities/Player.java
Normal file
60
src/de/teamteamteam/spacescooter/entities/Player.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
||||
|
@ -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<Entity> entities;
|
||||
@ -18,22 +23,32 @@ public class GameFrame extends JFrame {
|
||||
this.entities = new ArrayList<Entity>();
|
||||
|
||||
//TODO: Remove this!
|
||||
TestEntity t = new TestEntity();
|
||||
this.entities.add(t);
|
||||
this.entities.add(new TestEntity());
|
||||
this.entities.add(new Player());
|
||||
}
|
||||
|
||||
public ArrayList<Entity> 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<Entity> i = this.entities.iterator();
|
||||
|
@ -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<Entity> entityList = this.gf.getEntityList();
|
||||
Iterator<Entity> i = entityList.iterator();
|
||||
while(i.hasNext()) {
|
Loading…
Reference in New Issue
Block a user