Skip to content

Commit 2535a30

Browse files
Michael BalasMichael Balas
authored andcommitted
Document the init(), start(), and stop() methods
The init(), start(), and stop() methods all completely lacked any documentation. This fix provides comments describing how the colour array is populated by 6 shades of RGB in the init() method. It also describes everything else that occurs upon initialization (e.g. building the game level, spawning a vendor NPC, etc.) The documentation also explains what the start() and stop() methods do. These changes help summarize what is happening and provide context for the code.
1 parent b412e66 commit 2535a30

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/com/redomar/game/Game.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class Game extends Canvas implements Runnable {
3333
private static final String game_Version = "v1.8.3 Alpha"; // Current version of the game
3434
private static final int WIDTH = 160; // The width of the screen
3535
private static final int HEIGHT = (WIDTH / 3 * 2); // The height of the screen (two thirds of the width)
36-
private static final int SCALE = 3; // Scales the size of the screen (in either the x-direction, y-direction, or both)
36+
private static final int SCALE = 3; // Scales the size of the screen
3737
private static final String NAME = "Game"; // The name of the JFrame panel
3838
private static Game game;
3939
private static Time time = new Time(); // Date object that represents the calender's time value, in hh:mm:ss
@@ -61,14 +61,14 @@ public class Game extends Canvas implements Runnable {
6161
private static InputHandler input; // InputHandler object that accepts keyboard input and follows the appropriate actions
6262
private static MouseHandler mouse; // MouseHandler object that tracks mouse movement and clicks, and follows the appropriate actions
6363
private static InputContext context; // InputContext object that provides methods to control text input facilities
64-
private int tickCount = 0;
64+
private int tickCount = 0; // Updates the number of ticks
6565

6666
// Graphics
6767
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, // Describes a rasterized image with dimensions WIDTH and HEIGHT, and RGB colour
6868
BufferedImage.TYPE_INT_RGB);
6969
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()) // Array of red, green and blue values for each pixel, as well as an alpha value (if there is an alpha channel)
7070
.getData();
71-
private int[] colours = new int[6 * 6 * 6]; // Array of 216 unique colours
71+
private int[] colours = new int[6 * 6 * 6]; // Array of 216 unique colours (6 shades of R, 6 of G, and 6 of B)
7272
private BufferedImage image2 = new BufferedImage(WIDTH, HEIGHT - 30,
7373
BufferedImage.TYPE_INT_RGB);
7474
private Screen screen; // Screen object that accepts a width, height, and sprite sheet - to generate and render a screen
@@ -100,7 +100,7 @@ public Game() {
100100
setFrame(new JFrame(NAME)); // Creates the frame
101101
getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exits the program when user closes the frame
102102
getFrame().setLayout(new BorderLayout()); // Border lays out a container
103-
getFrame().add(this, BorderLayout.CENTER); // The centre layout constraint (middle of container)
103+
getFrame().add(this, BorderLayout.CENTER); // Centers the canvas inside the JFrame
104104
getFrame().pack(); // Sizes the frame so that all its contents are at or above their preferred sizes
105105
getFrame().setResizable(false); // Prevents the user from resizing the frame
106106
getFrame().setLocationRelativeTo(null); // Centres the window on the screen
@@ -334,38 +334,48 @@ public static void setClosing(boolean closing) {
334334
Game.closingMode = closing;
335335
}
336336

337+
/*
338+
* This method initializes the game once it starts. It populates the colour array with actual colours (6 shades each of RGB).
339+
* This method also builds the game level, spawns a new vendor NPC, and begins accepting keyboard and mouse input/tracking.
340+
*/
337341
public void init() {
338342
setGame(this);
339343
int index = 0;
340-
for (int r = 0; r < 6; r++) {
341-
for (int g = 0; g < 6; g++) {
342-
for (int b = 0; b < 6; b++) {
343-
int rr = (r * 255 / 5);
344-
int gg = (g * 255 / 5);
344+
for (int r = 0; r < 6; r++) { // For r in all 6 shades of R
345+
for (int g = 0; g < 6; g++) { // For g in all 6 shades of G
346+
for (int b = 0; b < 6; b++) { // For b in all 6 shades of B
347+
int rr = (r * 255 / 5); // 255 used instead of 256 colours in order to have a transparent colour. Splits 255 into 6 sections/shades
348+
int gg = (g * 255 / 5); // (e.g. 0, 51, 102, 153, etc.) for each of rr, gg, and bb.
345349
int bb = (b * 255 / 5);
346-
colours[index++] = rr << 16 | gg << 8 | bb;
350+
colours[index++] = rr << 16 | gg << 8 | bb; // All colour values (RGB) are placed into one 32-bit integer by using 'shift' and 'or' operations, populating the colour array
347351
}
348352
}
349353
}
350354

351-
screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
352-
input = new InputHandler(this);
353-
setMouse(new MouseHandler(this));
355+
screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png")); // Loads a new screen with a width, height, and SpriteSheet
356+
input = new InputHandler(this); // Input begins to record key presses
357+
setMouse(new MouseHandler(this)); // Mouse tracking and clicking is now recorded
354358
setWindow(new WindowHandler(this));
355-
setMap("/levels/custom_level.png");
356-
setMap(1);
359+
setMap("/levels/custom_level.png"); // custom_level.png map is set upon initialization
360+
setMap(1); // Map set to 1 (custom_level.png)
357361

358-
game.setVendor(new Vendor(level, "Vendor", 215, 215, 304, 543));
359-
level.addEntity(getVendor());
362+
game.setVendor(new Vendor(level, "Vendor", 215, 215, 304, 543)); // Set a new vendor NPC on the custom_level, with name "Vendor", at position (215, 215), with shirt colour 304 (purple) and face colour 543 (caucasian)
363+
level.addEntity(getVendor()); // Add the previously defined vendor NPC to the game level
360364
}
361365

366+
/*
367+
* This method will start the game and allow the user to start playing
368+
*/
362369
public synchronized void start() {
363-
Game.setRunning(true);
364-
new Thread(this, "GAME").start();
370+
Game.setRunning(true); // Game will run
371+
new Thread(this, "GAME").start(); // Thread is an instance of Runnable. Whenever it is started, it will run the run() method
365372
}
366373

374+
/*
375+
* This method will stop the game
376+
*/
367377
public synchronized void stop() {
368-
Game.setRunning(false);
378+
Game.setRunning(false); // Game will not run
369379
}
370380

371381
public void run() {

0 commit comments

Comments
 (0)