Add main game files

Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
Chris Cromer 2019-09-02 14:52:01 -04:00
parent a18c84046e
commit 0e61872461
7 changed files with 386 additions and 0 deletions

View File

@ -0,0 +1,108 @@
package cl.cromer.game;
import java.awt.*;
import javax.swing.JComponent;
public class Celda extends JComponent implements Constantes {
/**
* The x coordinate of the cell
*/
private int x;
/**
* The y coordinate of the cell
*/
private int y;
/**
* The type of cell
*/
private Type type = Type.SPACE;
/**
* The possible types of cell that this could be
*/
protected enum Type {
SPACE,
PLAYER,
END,
ENEMY,
OBSTACLE,
PRIZE
}
/**
* Initialize the cell with its coordinates
* @param x The x coordinate
* @param y The y coordinate
*/
public Celda(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Set the type of cell that this will be
* @param type The type
*/
public void setType(Type type) {
this.type = type;
}
/**
* Get the current type of this cell
* @return Returns the type of cell
*/
private Type getType() {
return this.type;
}
/**
* Override the paintComponent method of JComponent to pain the cell based on type
* @param g The graphics object to paint
*/
@Override
public void paintComponent(Graphics g) {
// Draw the borders
g.setColor(Color.black);
g.drawRect(x, y, CELL_PIXELS, CELL_PIXELS);
g.setFont(new Font("monospaced", Font.BOLD, 10));
// Fill in the cell
switch (getType()) {
case SPACE:
g.setColor(Color.orange);
g.fillRect(x + 1, y + 1, CELL_PIXELS - 1, CELL_PIXELS - 1);
break;
case PLAYER:
g.setColor(Color.green);
g.fillRect(x + 1, y + 1, CELL_PIXELS - 1, CELL_PIXELS - 1);
g.setColor(Color.black);
g.drawString(String.valueOf(PLAYER), x + (CELL_PIXELS / 2), y + (CELL_PIXELS / 2));
break;
case END:
g.setColor(Color.pink);
g.fillRect(x + 1, y + 1, CELL_PIXELS - 1, CELL_PIXELS - 1);
g.setColor(Color.black);
g.drawString(String.valueOf(END), x + (CELL_PIXELS / 2), y + (CELL_PIXELS / 2));
break;
case PRIZE:
g.setColor(Color.red);
g.fillRect(x + 1, y + 1, CELL_PIXELS - 1, CELL_PIXELS - 1);
g.setColor(Color.black);
g.drawString(String.valueOf(PRIZE), x + (CELL_PIXELS / 2), y + (CELL_PIXELS / 2));
break;
case ENEMY:
g.setColor(Color.blue);
g.fillRect(x + 1, y + 1, CELL_PIXELS - 1, CELL_PIXELS - 1);
g.setColor(Color.black);
g.drawString(String.valueOf(ENEMY), x + (CELL_PIXELS / 2), y + (CELL_PIXELS / 2));
break;
case OBSTACLE:
g.setColor(Color.black);
g.fillRect(x + 1, y + 1, CELL_PIXELS - 1, CELL_PIXELS - 1);
g.setColor(Color.white);
g.drawString(String.valueOf(OBSTACLE), x + (CELL_PIXELS / 2), y + (CELL_PIXELS / 2));
break;
}
}
}

View File

@ -0,0 +1,66 @@
package cl.cromer.game;
/**
* Constants used in the game
*/
public interface Constantes {
/**
* The size in pixels of the cells
*/
int CELL_PIXELS = 32;
/**
* The number of cells to draw horizontally
*/
int HORIZONTAL_CELLS = 40;
/**
* The number of cells to draw vertically
*/
int VERTICAL_CELLS = 20;
/**
* The window border width
*/
int WINDOW_BORDER_WIDTH = 30;
/**
* The window border height
*/
int WINDOW_BORDER_HEIGHT = 50;
/**
* The scene width
*/
int SCENE_WIDTH = (CELL_PIXELS * HORIZONTAL_CELLS) + WINDOW_BORDER_WIDTH;
/**
* The scene height
*/
int SCENE_HEIGHT = (CELL_PIXELS * VERTICAL_CELLS) + WINDOW_BORDER_HEIGHT;
/**
* The letter that represents the player
*/
char PLAYER = 'P';
/**
* The letter that represents the end
*/
char END = 'F';
/**
* The letter that represents the prize
*/
char PRIZE = 'G';
/**
* The letter that represents the enemy
*/
char ENEMY = 'E';
/**
* The letter that represents the obstacle
*/
char OBSTACLE = 'O';
/**
* The amount of prizes to draw
*/
int PRIZES = 5;
/**
* The amount of enemies to draw
*/
int ENEMIES = 3;
}

View File

@ -0,0 +1,86 @@
package cl.cromer.game;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JComponent;
/**
* The scene used for the game
*/
public class Escenario extends JComponent implements Constantes {
private Celda[][] celdas;
/**
* Initialize the scene
*/
public Escenario() {
Random random = new Random();
final int cells = (HORIZONTAL_CELLS * VERTICAL_CELLS);
final int obstacles = (int) Math.floor((double) cells * 0.1);
int random_value;
ArrayList<RandomPositionList> arrayList = new ArrayList<>();
for (int i = 0; i < ENEMIES; i++) {
random_value = random.nextInt(cells - 1) + 1;
while (arrayList.contains(new RandomPositionList(random_value, Celda.Type.ENEMY))) {
random_value = random.nextInt(cells - 1) + 1;
}
arrayList.add(new RandomPositionList(random_value, Celda.Type.ENEMY));
}
for (int i = 0; i < PRIZES; i++) {
random_value = random.nextInt(cells - 1) + 1;
while (arrayList.contains(new RandomPositionList(random_value, Celda.Type.PRIZE))) {
random_value = random.nextInt(cells - 1) + 1;
}
arrayList.add(new RandomPositionList(random_value, Celda.Type.PRIZE));
}
for (int i = 0; i < obstacles; i++) {
random_value = random.nextInt(cells - 1) + 1;
while (arrayList.contains(new RandomPositionList(random_value, Celda.Type.OBSTACLE))) {
random_value = random.nextInt(cells - 1) + 1;
}
arrayList.add(new RandomPositionList(random_value, Celda.Type.OBSTACLE));
}
random_value = random.nextInt(cells - 1) + 1;
while (arrayList.contains(new RandomPositionList(random_value, Celda.Type.END))) {
random_value = random.nextInt(cells - 1) + 1;
}
arrayList.add(new RandomPositionList(random_value, Celda.Type.END));
celdas = new Celda[HORIZONTAL_CELLS][VERTICAL_CELLS];
int cell_count = 0;
for (int i = 0; i < HORIZONTAL_CELLS; i++) {
for (int j = 0; j < VERTICAL_CELLS; j++) {
celdas[i][j] = new Celda(i * CELL_PIXELS + 10, j * CELL_PIXELS + 10);
if (i == 0 && j == 0) {
celdas[i][j].setType(Celda.Type.PLAYER);
}
else {
for (RandomPositionList randomList : arrayList) {
if (cell_count == randomList.getPosition()) {
celdas[i][j].setType(randomList.getType());
break;
}
}
}
cell_count++;
}
}
}
/**
* Override the painComponent method of JComponent to paint the scene
* @param g The graphics object to paint
*/
@Override
public void paintComponent(Graphics g) {
for (int i = 0; i < HORIZONTAL_CELLS; i++) {
for (int j = 0; j < VERTICAL_CELLS; j++) {
celdas[i][j].paintComponent(g);
}
}
}
}

View File

@ -0,0 +1,28 @@
package cl.cromer.game;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
public class Lienzo extends Canvas implements Constantes {
private Escenario escenario;
/**
* Initialize the canvas
*/
public Lienzo() {
escenario = new Escenario();
this.setBackground(Color.orange);
this.setSize(SCENE_WIDTH, SCENE_HEIGHT);
}
/**
* Override the paint method of Canvas to paint all the scene components
* @param g The graphics object to paint
*/
@Override
public void paint(Graphics g) {
escenario.paintComponent(g);
}
}

View File

@ -0,0 +1,19 @@
package cl.cromer.game;
import javax.swing.JFrame;
/**
* The main class of the game
*/
public class Main {
/**
* Open the main window
* @param args The arguments passed to the application
*/
public static void main (String[]args) {
VentanaPrincipal ventanaPrincipal = new VentanaPrincipal();
ventanaPrincipal.setVisible(true);
ventanaPrincipal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

View File

@ -0,0 +1,58 @@
package cl.cromer.game;
/**
* This class is used to save locations of random cells for enemies, obstacles, and prizes
*/
public class RandomPositionList {
/**
* The position
*/
private int position;
/**
* The type
*/
private Celda.Type type;
/**
* Initialize the position and type of the list
* @param position The position
* @param type The type
*/
public RandomPositionList(int position, Celda.Type type) {
this.position = position;
this.type = type;
}
/**
* Return the position that the object should be drawn
* @return The position
*/
public int getPosition() {
return position;
}
/**
* Get the type of object that will be stored at the cell position
* @return Returns the cell type
*/
public Celda.Type getType() {
return type;
}
/**
* Override the equals method so that we only compare the position and not the type
* @param o The object to compare
* @return Returns true if they are the same
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RandomPositionList that = (RandomPositionList) o;
return position == that.position;
}
}

View File

@ -0,0 +1,21 @@
package cl.cromer.game;
import javax.swing.JFrame;
import java.awt.Dimension;
/**
* The main window of the game
*/
public class VentanaPrincipal extends JFrame implements Constantes {
/**
* Initialize the main window
*/
public VentanaPrincipal() {
Lienzo lienzo = new Lienzo();
Dimension screenSize = super.getToolkit().getScreenSize();
this.getContentPane().add(lienzo);
this.setSize(screenSize.width, screenSize.height);
this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
}