Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed assests/A_key.png
Binary file not shown.
Binary file removed assests/B_key.png
Binary file not shown.
Binary file removed assests/c_key.png
Binary file not shown.
Binary file removed assests/d_key.png
Binary file not shown.
Binary file removed assests/e_key.png
Binary file not shown.
Binary file removed assests/f_key.png
Binary file not shown.
Binary file removed assests/g_key.png
Binary file not shown.
Binary file removed assests/h_key.png
Binary file not shown.
Binary file removed assests/i_key.png
Binary file not shown.
Binary file removed assests/j_key.png
Binary file not shown.
Binary file removed assests/k_key.png
Binary file not shown.
Binary file removed assests/l_key.png
Binary file not shown.
Binary file removed assests/m_key.png
Binary file not shown.
Binary file removed assests/n_key.png
Binary file not shown.
Binary file removed assests/o_key.png
Binary file not shown.
Binary file removed assests/p_key.png
Binary file not shown.
Binary file removed assests/q_key.png
Binary file not shown.
Binary file removed assests/r_key.png
Binary file not shown.
Binary file removed assests/s_key.png
Binary file not shown.
Binary file removed assests/t_key.png
Binary file not shown.
Binary file removed assests/u_key.png
Binary file not shown.
Binary file removed assests/v_key.png
Binary file not shown.
Binary file removed assests/w_key.png
Binary file not shown.
Binary file removed assests/x_key.png
Binary file not shown.
Binary file removed assests/y_key.png
Binary file not shown.
Binary file removed assests/z_key.png
Diff not rendered.
File renamed without changes
Binary file added assets/metalbackground.png
18 changes: 2 additions & 16 deletions src/org/codecentral/type/InputHandler.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
package org.codecentral.type;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
*
*/
public class InputHandler implements KeyListener {
public class InputHandler extends KeyAdapter {

public InputHandler() {

}

@Override
public void keyTyped(KeyEvent keyEvent) {
}

@Override
public void keyPressed(KeyEvent keyEvent) {
System.out.println(KeyEvent.getKeyText(keyEvent.getKeyCode()));
}

@Override
public void keyReleased(KeyEvent keyEvent) {

}

public interface Callback {

void onPress(char letter);
}
}
60 changes: 53 additions & 7 deletions src/org/codecentral/type/TTRGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JApplet;
import javax.swing.JFrame;

Expand All @@ -21,8 +26,13 @@ public class TTRGame extends JApplet {

private static final String NAME = "Type Type Revolution";

private List<Row> rows = new ArrayList<>(4);
private static final int ROW_AMOUNT = 4;

private BufferedImage image = null;

private InputHandler inputHandler = new InputHandler();

private List<Row> rows = new ArrayList<>();

public static void main(String[] args) {
JFrame f = new JFrame(NAME);
Expand All @@ -32,23 +42,30 @@ public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
TTRGame game = new TTRGame();
JApplet game = new TTRGame();
f.add(game);
game.init();
f.pack();
f.setSize(new Dimension(1280, 1024));
f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
f.setSize(screenSize.width - 100, screenSize.height - 100);
f.setVisible(true);
}

@Override
public void init() {

addKeyListener(inputHandler);
setFocusable(true);
initializeRows();
}

@Override
public void paint(Graphics graphics) {
super.paint(graphics);
drawBackground(graphics);
drawRows(graphics);
updateRows();

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Expand All @@ -57,11 +74,40 @@ public void paint(Graphics graphics) {
repaint();
}

@Override
public void resize(int width, int height) {
super.resize(width, height);
handleResizing();
}

private void drawBackground(Graphics g) {
if (image == null) {
try {
image = ImageIO.read(new File("assets/metalbackground.png"));
} catch (IOException e) {
e.printStackTrace();
System.err.println("Couldn't draw background");
}
}
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}

private void initializeRows() {
int yPos = 100;
for (int i = 0; i < ROW_AMOUNT; i++) {
Row row = new Row(0, yPos, getWidth());
rows.add(i, row);
yPos += row.getHeight() + 100;
}
}

private void updateRows() {
rows.forEach(Row::onUpdate);
}

private void handleResizing() {
for (int i = 0; i < rows.size(); i++) {
rows.set(i, new Row(0, yPos));
yPos += 50;
rows.get(i).setWidth(getWidth());
}
}

Expand All @@ -75,4 +121,4 @@ private void drawRows(Graphics graphics) {
row.onDraw(graphics);
}
}
}
}
37 changes: 34 additions & 3 deletions src/org/codecentral/type/objects/GameObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ abstract class GameObject {

private Point position;

public GameObject(int x, int y) {
this(new Point(x, y));
private int width;
private int height;

public GameObject(int x, int y, int width, int height) {
this(new Point(x, y), width, height);
}

public GameObject(Point position) {
public GameObject(Point position, int width, int height) {
this.position = position;
this.width = width;
this.height = height;
}

public int getX() {
Expand All @@ -35,6 +40,32 @@ public void setY(double y) {
position.setLocation(getX(), y);
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public void setWidth(int width) {
this.width = width;
}

public void setHeight(int height) {
this.height = height;
}

/**
* Updates game logic and prepare for a redraw.
*/
public void onUpdate() {

}

/**
* Redraws currently displayed graphics.
*/
public abstract void onDraw(Graphics g);

@Override
Expand Down
37 changes: 32 additions & 5 deletions src/org/codecentral/type/objects/Letter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,37 @@
import java.awt.Graphics;
import java.util.concurrent.ThreadLocalRandom;

/**
* A {@link GameObject} that represents a movable char on screen.
*/
public class Letter extends GameObject {

/**
* The number of pixels to move upon each movement.
*
* @see #moveForward()
* @see #setSpeed(int)
*/
private int speed = 10;

private char letter;

public Letter(int x, int y) {
this(generateRandomLetter(), x, y);
/**
* Creates a new Letter at the given position and with the given dimensions, generating a random
* char as the displayed letter.
*
* @see #Letter(char, int, int, int, int)
*/
public Letter(int x, int y, int width, int height) {
this(generateRandomLetter(), x, y, width, height);
}

public Letter(char letter, int x, int y) {
super(x, y);
/**
* Creates a new Letter at the given position and with the given dimensions using the given
* char.
*/
public Letter(char letter, int x, int y, int width, int height) {
super(x, y, width, height);
this.letter = letter;
}

Expand All @@ -30,15 +44,28 @@ public void onDraw(Graphics g) {
g.setFont(new Font("SansSerif", Font.BOLD, 10));
g.setColor(Color.WHITE);
g.drawString(String.valueOf(letter), getX(), getY());
moveForward();
}

/**
* Moves the this Letter to the left.
*
* @see #setSpeed(int)
*/
public void moveForward() {
setY(getY() - speed);
setX(getX() - speed);
}

/**
* Sets the rate in pixels per update in which this letter moves to the left.
*/
public void setSpeed(int newSpeed) {
speed = newSpeed;
}
@Override
public void onUpdate() {
moveForward();
}

private static char generateRandomLetter() {
int upperCase = ThreadLocalRandom.current().nextInt(56, 90 + 1);
Expand Down
53 changes: 41 additions & 12 deletions src/org/codecentral/type/objects/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,61 @@
import java.util.ArrayList;
import java.util.List;

/**
* A {@link GameObject} that displays {@link Letter}s coming from right to left.
*/
public class Row extends GameObject {

private static final int HEIGHT = 128;

private static final int DEFAULT_WIDTH = 1000;
private static final int DEFAULT_HEIGHT = 128;

private List<Letter> letters = new ArrayList<>();

public Row(int x, int y) {
super(x, y);
/**
* Creates a new row with {@link #DEFAULT_HEIGHT}
*
* @see #Row(int, int, int, int)
*/
public Row(int x, int y, int width) {
this(x, y, width, DEFAULT_HEIGHT);
}

/**
* Creates a new Row at the given position and with the given dimensions.
*/
public Row(int x, int y, int width, int height) {
super(x, y, width, height);
}

public void drawLetters(Graphics graphics) {
letters.forEach(letter -> letter.onDraw(graphics));
private void drawLetters(Graphics graphics) {
for (Letter letter : letters) {
letter.onDraw(graphics);
}
}

@Override
public void onDraw(Graphics g) {
if (letters.size() == 0) {
public void onUpdate() {
if (letters.isEmpty()) {
for (int i = 0; i < 10; i++) {
letters.add(new Letter(200, 200));
letters.add(new Letter(200, 200, 100, 100));
}
}
for (Letter letter : letters) {
if (checkX(letter))
letters.remove(letters.indexOf(letter));
}

letters.forEach(Letter::onUpdate);
}

public boolean checkX(Letter letter) {
return (letter.getX() <= -letter.getWidth());
}

@Override
public void onDraw(Graphics g) {
// Draw this row
g.setColor(Color.BLACK);
g.drawRect(getX(), getY(), HEIGHT, DEFAULT_WIDTH);
g.setColor(Color.WHITE);
g.fillRect(getX(), getY(), getWidth(), getHeight());
// Draw the letters
drawLetters(g);
}
Expand Down