Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 11 additions and 14 deletions.
  1. +9 −6 src/game/Game.java
  2. +2 −8 src/game/InputHandler.java
View
15 src/game/Game.java
@@ -20,7 +20,8 @@
public static int FIELD_WIDTH = 15;
public static final int GAME_WIDTH = Game.FIELD_WIDTH * Game.BLOCK_SIZE;
- public static final int GAME_HEIGHT = ((Game.FIELD_WIDTH * 3) / 4) * Game.BLOCK_SIZE;
+ public static final int GAME_HEIGHT = ((Game.FIELD_WIDTH * 3) / 4)
+ * Game.BLOCK_SIZE;
public static final int SCALE = 1;
private static ArrayList<Entity> entities = new ArrayList<Entity>();
@@ -35,13 +36,13 @@
private InputHandler keys;
public Game() {
- Dimension d = new Dimension(Game.GAME_WIDTH * Game.SCALE, Game.GAME_HEIGHT * Game.SCALE);
+ Dimension d = new Dimension(Game.GAME_WIDTH * Game.SCALE,
+ Game.GAME_HEIGHT * Game.SCALE);
this.setPreferredSize(d);
this.setMinimumSize(d);
this.setMaximumSize(d);
this.setBackground(new Color(255, 255, 255));
- this.keys = new InputHandler();
- this.addKeyListener(this.keys);
+ this.keys = new InputHandler(this);
for (int x = 0; x < Game.FIELD_WIDTH; x++) {
Game.entities.add(new Wall(x * Game.BLOCK_SIZE, 0));
}
@@ -108,7 +109,8 @@ private void step(double delta) {
private void draw(Graphics g) {
g.setColor(this.getBackground());
- g.fillRect(0, 0, (Game.GAME_WIDTH * Game.SCALE) + 10, (Game.GAME_HEIGHT * Game.SCALE) + 10);
+ g.fillRect(0, 0, (Game.GAME_WIDTH * Game.SCALE) + 10,
+ (Game.GAME_HEIGHT * Game.SCALE) + 10);
for (Entity e : Game.entities) {
e.draw(g);
}
@@ -118,7 +120,8 @@ private void draw(Graphics g) {
public static List<Entity> getEntities(int x1, int y1, int x2, int y2) {
List<Entity> result = new ArrayList<Entity>();
- Box b = new Box(Math.max(0, x1), Math.max(0, y1), Math.min(x2, Game.GAME_WIDTH), Math.min(y2, Game.GAME_HEIGHT));
+ Box b = new Box(Math.max(0, x1), Math.max(0, y1), Math.min(x2,
+ Game.GAME_WIDTH), Math.min(y2, Game.GAME_HEIGHT));
for (Entity e : Game.entities) {
if (e.box.intersect(b)) {
View
10 src/game/InputHandler.java
@@ -2,36 +2,30 @@
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
-import java.util.ArrayList;
-import java.util.List;
public class InputHandler implements KeyListener {
public class Key {
public boolean down;
public Key() {
- InputHandler.this.keys.add(this);
}
public void toggle(boolean pressed) {
if (pressed != this.down) {
this.down = pressed;
}
}
-
}
- public List<Key> keys = new ArrayList<Key>();
-
public Key up = new Key();
public Key down = new Key();
public Key left = new Key();
public Key right = new Key();
public Key bomb = new Key();
public Key pause = new Key();
- public InputHandler() {
-
+ public InputHandler(Game game) {
+ game.addKeyListener(this);
}
private void toggle(KeyEvent ke, boolean pressed) {

No commit comments for this range

Something went wrong with that request. Please try again.