You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: src/com/redomar/game/Game.java
+30-20Lines changed: 30 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ public class Game extends Canvas implements Runnable {
33
33
privatestaticfinalStringgame_Version = "v1.8.3 Alpha"; // Current version of the game
34
34
privatestaticfinalintWIDTH = 160; // The width of the screen
35
35
privatestaticfinalintHEIGHT = (WIDTH / 3 * 2); // The height of the screen (two thirds of the width)
36
-
privatestaticfinalintSCALE = 3; // Scales the size of the screen (in either the x-direction, y-direction, or both)
36
+
privatestaticfinalintSCALE = 3; // Scales the size of the screen
37
37
privatestaticfinalStringNAME = "Game"; // The name of the JFrame panel
38
38
privatestaticGamegame;
39
39
privatestaticTimetime = newTime(); // Date object that represents the calender's time value, in hh:mm:ss
@@ -61,14 +61,14 @@ public class Game extends Canvas implements Runnable {
61
61
privatestaticInputHandlerinput; // InputHandler object that accepts keyboard input and follows the appropriate actions
62
62
privatestaticMouseHandlermouse; // MouseHandler object that tracks mouse movement and clicks, and follows the appropriate actions
63
63
privatestaticInputContextcontext; // InputContext object that provides methods to control text input facilities
64
-
privateinttickCount = 0;
64
+
privateinttickCount = 0;// Updates the number of ticks
65
65
66
66
// Graphics
67
67
privateBufferedImageimage = newBufferedImage(WIDTH, HEIGHT, // Describes a rasterized image with dimensions WIDTH and HEIGHT, and RGB colour
68
68
BufferedImage.TYPE_INT_RGB);
69
69
privateint[] 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)
privateScreenscreen; // Screen object that accepts a width, height, and sprite sheet - to generate and render a screen
@@ -100,7 +100,7 @@ public Game() {
100
100
setFrame(newJFrame(NAME)); // Creates the frame
101
101
getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exits the program when user closes the frame
102
102
getFrame().setLayout(newBorderLayout()); // 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
104
104
getFrame().pack(); // Sizes the frame so that all its contents are at or above their preferred sizes
105
105
getFrame().setResizable(false); // Prevents the user from resizing the frame
106
106
getFrame().setLocationRelativeTo(null); // Centres the window on the screen
@@ -334,38 +334,48 @@ public static void setClosing(boolean closing) {
334
334
Game.closingMode = closing;
335
335
}
336
336
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
+
*/
337
341
publicvoidinit() {
338
342
setGame(this);
339
343
intindex = 0;
340
-
for (intr = 0; r < 6; r++) {
341
-
for (intg = 0; g < 6; g++) {
342
-
for (intb = 0; b < 6; b++) {
343
-
intrr = (r * 255 / 5);
344
-
intgg = (g * 255 / 5);
344
+
for (intr = 0; r < 6; r++) {// For r in all 6 shades of R
345
+
for (intg = 0; g < 6; g++) {// For g in all 6 shades of G
346
+
for (intb = 0; b < 6; b++) {// For b in all 6 shades of B
347
+
intrr = (r * 255 / 5);// 255 used instead of 256 colours in order to have a transparent colour. Splits 255 into 6 sections/shades
348
+
intgg = (g * 255 / 5);// (e.g. 0, 51, 102, 153, etc.) for each of rr, gg, and bb.
345
349
intbb = (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
game.setVendor(newVendor(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
360
364
}
361
365
366
+
/*
367
+
* This method will start the game and allow the user to start playing
368
+
*/
362
369
publicsynchronizedvoidstart() {
363
-
Game.setRunning(true);
364
-
newThread(this, "GAME").start();
370
+
Game.setRunning(true);// Game will run
371
+
newThread(this, "GAME").start();// Thread is an instance of Runnable. Whenever it is started, it will run the run() method
0 commit comments