11 | Application // the Application class, extends JFrame and will be an instance of the game
12 | Game // the Game class, extends JPanel and will be used to render the game
13 | Scene // this class will contain all level data, with tiles
14 | Tile // a tile is a unit on the map, and will contain data like it's position and if it collides with the player
15 | Player // the player, a base class for Peasant and Hero
16 | Peasant // a player subclass which is very weak
17 | Hero // a player subclass that uses superior technology and is a lot stronger
18 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/InputManager.java:
--------------------------------------------------------------------------------
1 |
2 | import java.awt.event.KeyEvent;
3 | import java.awt.event.KeyListener;
4 |
5 | /**
6 | * Created by cor on 13-12-14.
7 | */
8 | public class InputManager implements KeyListener {
9 |
10 | // booleans containing key data
11 | public boolean up = false;
12 | public boolean down = false;
13 | public boolean right = false;
14 | public boolean left = false;
15 |
16 |
17 | // it's required to override keyTyped because we're implementing KeyListener
18 | @Override
19 | public void keyTyped(KeyEvent e) {}
20 |
21 | @Override
22 | public void keyPressed(KeyEvent e) {
23 |
24 | //update key data
25 | int key = e.getKeyCode();
26 |
27 | if (key == KeyEvent.VK_UP) { up = true; }
28 | if (key == KeyEvent.VK_DOWN) { down = true; }
29 | if (key == KeyEvent.VK_RIGHT) { right = true; }
30 | if (key == KeyEvent.VK_LEFT) { left = true; }
31 | }
32 |
33 | @Override
34 | public void keyReleased(KeyEvent e) {
35 |
36 | //update key data
37 | int key = e.getKeyCode();
38 |
39 | if (key == KeyEvent.VK_UP) { up = false; }
40 | if (key == KeyEvent.VK_DOWN) { down = false; }
41 | if (key == KeyEvent.VK_RIGHT) { right = false; }
42 | if (key == KeyEvent.VK_LEFT) { left = false; }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/Application.java:
--------------------------------------------------------------------------------
1 | import java.awt.EventQueue;
2 | import javax.swing.*;
3 |
4 |
5 | /**
6 | * The Application class is the entry point of the game
7 | */
8 | public class Application extends JFrame {
9 |
10 | /**
11 | * The main Application initializer
12 | */
13 | public Application() {
14 | initUI();
15 | }
16 |
17 | /**
18 | * Initialize the UI
19 | */
20 |
21 | private Game game;
22 | private void initUI() {
23 |
24 | // UI Initialization
25 |
26 | // Add a new instance of the Game to the Application's JFrame and configure it
27 | game = new Game();
28 | add(game);
29 | setTitle("PCMasterRace");
30 |
31 | // Quit the program when the window is closed
32 | setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
33 |
34 | // center the window on the screen
35 | setLocationRelativeTo(null);
36 |
37 | // disable window resizing
38 | setResizable(false);
39 |
40 | addKeyListener(game.inputManager);
41 |
42 | // set the JFrame to the correct size (specified by the Game JPanel)
43 | pack();
44 | }
45 |
46 | public static void main(String[] args) {
47 |
48 | // create an instance of the application and make it visible
49 | EventQueue.invokeLater(new Runnable() {
50 | @Override
51 | public void run() {
52 | Application ex = new Application();
53 | ex.setVisible(true);
54 | ex.requestFocus();
55 | ex.setLocationRelativeTo(null);
56 | }
57 |
58 | });
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/src/Game.java:
--------------------------------------------------------------------------------
1 | import java.awt.*;
2 | import java.awt.event.*;
3 | import java.util.*;
4 | import java.util.Timer;
5 | import javax.swing.*;
6 |
7 |
8 | /**
9 | * The Game class is the JPanel where the game takes place.
10 | */
11 | public class Game extends JPanel {
12 |
13 | // game objects
14 | private Scene scene;
15 | private Player player;
16 |
17 | // animation
18 | private Timer timer;
19 | private final int INITIAL_DELAY = 100;
20 | private final int PERIOD_INTERVAL = 16;
21 |
22 | // input
23 | public InputManager inputManager = new InputManager();
24 |
25 | // JPanel
26 | private Dimension size = new Dimension(540,540);
27 |
28 |
29 |
30 | public Game() {
31 | initGame();
32 | }
33 |
34 |
35 | private void initGame() {
36 | // set the size of the JPanel
37 | setPreferredSize(size);
38 |
39 | // configure game objects
40 | scene = new Scene(size);
41 | player = new Player();
42 |
43 | //configure timer for animation
44 | timer = new Timer();
45 | timer.scheduleAtFixedRate(new Tick(), INITIAL_DELAY, PERIOD_INTERVAL);
46 |
47 |
48 | // configure input
49 | addKeyListener(inputManager);
50 |
51 | }
52 |
53 | private void update() {
54 | player.update(inputManager);
55 | }
56 |
57 | @Override
58 | public void paintComponent(Graphics g) {
59 | super.paintComponent(g);
60 |
61 | //enable anti-aliasing
62 | Graphics2D g2d = (Graphics2D) g;
63 | RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
64 | rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
65 | g2d.setRenderingHints(rh);
66 |
67 | scene.draw(g2d);
68 | player.draw(g2d);
69 | }
70 |
71 | private class Tick extends TimerTask {
72 |
73 | // main game loop, gets called approximately 60 times per second
74 | @Override
75 | public void run() {
76 | update();
77 | repaint();
78 | }
79 | }
80 |
81 | }
82 |
83 |
--------------------------------------------------------------------------------
/src/Player.java:
--------------------------------------------------------------------------------
1 | import javax.swing.*;
2 | import java.awt.*;
3 |
4 | /**
5 | * Player class
6 | */
7 | public class Player {
8 |
9 | public Dimension size;
10 | public Point position;
11 |
12 | public int moveSpeed;
13 |
14 | /**
15 | * The default initializer
16 | */
17 | public Player() {
18 | position = new Point(200, 200);
19 | size = new Dimension(60, 60);
20 | moveSpeed = 10;
21 | }
22 |
23 | /**
24 | * Move the player
25 | * @param direction the direction to move the player in
26 | * @param distance the distance for the player to walk
27 | */
28 | public void move(Direction direction, int distance){
29 | switch (direction) {
30 | case NORTH:
31 | position.y -= distance;
32 | break;
33 | case EAST:
34 | position.x += distance;
35 | break;
36 | case SOUTH:
37 | position.y += distance;
38 | break;
39 | case WEST:
40 | position.x -= distance;
41 | break;
42 | }
43 | }
44 |
45 | /**
46 | * Draw the player
47 | * @param g2d the Graphics2D object to draw the player to
48 | */
49 | public void draw(Graphics2D g2d) {
50 | // Image Peasant = new ImageIcon("images/Peasant.png").getImage();
51 | // g2d.drawImage(Peasant, position.x, position.y, 60, 60, null);
52 | }
53 |
54 | /**
55 | * Update the player
56 | * @param inputManager the game's input manager
57 | */
58 | public void update(InputManager inputManager) {
59 |
60 | // move the player
61 | if (inputManager.up) {
62 | this.move(Direction.NORTH, moveSpeed);
63 | }
64 |
65 | if (inputManager.down) {
66 | this.move(Direction.SOUTH, moveSpeed);
67 | }
68 |
69 | if (inputManager.left) {
70 | this.move(Direction.WEST, moveSpeed);
71 | }
72 |
73 | if (inputManager.right) {
74 | this.move(Direction.EAST, moveSpeed);
75 | }
76 | }
77 |
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/src/Scene.java:
--------------------------------------------------------------------------------
1 | import javax.swing.*;
2 | import java.awt.*;
3 |
4 | /**
5 | *
6 | * Created by cor on 12-12-14.
7 | */
8 | public class Scene {
9 |
10 | private Dimension resolution;
11 | private Dimension tileSize; // a ninth of the resolution since the level is 9*9 tiles
12 |
13 | public static int[][] map = new int[9][9]; // a 2D array containing the level data
14 |
15 | public Scene() {
16 | this.resolution = new Dimension(540, 540);
17 | // set the tile size to 1/9 of the resolution
18 | this.tileSize = new Dimension(this.resolution.width / 9, this.resolution.height / 9);
19 |
20 |
21 | map[2][4] = 1; // example data
22 |
23 | }
24 |
25 | public Scene(Dimension resolution) {
26 | this.resolution = resolution;
27 | this.tileSize = new Dimension(this.resolution.width / 9, this.resolution.height / 9);
28 |
29 | for (int i = 1; i < 9; i += 2){
30 |
31 | for (int j = 1; j < 9; j += 2){
32 | map[i][j] = 1;
33 | }
34 | }
35 |
36 | map[2][5] = 2;
37 | }
38 |
39 | public void draw(Graphics2D g2d) {
40 |
41 | // iterate over vertical rows
42 | for (int x = 0; x < 9; x++) {
43 |
44 | //iterate through vertical row
45 | for (int y = 0; y < 9; y++) {
46 |
47 | // check the integer at the position and set the color accordingly
48 | if (map[x][y] == 0) {
49 | g2d.setColor(new Color(122, 83, 43));
50 | g2d.fillRect(x * tileSize.width, y * tileSize.height, tileSize.width, tileSize.height);
51 | } else if (map[x][y] == 1) {
52 | g2d.setColor(new Color(0, 0, 0));
53 | g2d.fillRect(x * tileSize.width, y * tileSize.height, tileSize.width, tileSize.height);
54 | } else if (map[x][y] == 2) {
55 | g2d.setColor(new Color(122, 83, 43));
56 | g2d.fillRect(x * tileSize.width, y * tileSize.height, tileSize.width, tileSize.height);
57 | Image Peasant = new ImageIcon("images/Peasant.png").getImage();
58 | g2d.drawImage(Peasant, tileSize.width * x, y * tileSize.height, tileSize.height, tileSize.width, null);
59 | }
60 | }
61 | }
62 |
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
146 |
147 |
148 |
149 |
150 |
151 |
152 | localhost
153 | 5050
154 |
155 |
156 |
157 |
158 |
159 |
160 | 1.8
161 |
162 |
167 |
168 |
169 |
170 |
171 |
172 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |