Skip to content

Commit 20c635f

Browse files
Michael BalasMichael Balas
authored andcommitted
Document the render() and status() methods
Both methods completely lacked any documentation explaining what they do. To fix this, documentation was added describing what the code tries to achieve, and how it goes about achieving it. This commit makes it much easier to understand and visualize the code, especially since both methods are used to display text and images to a screen.
1 parent 0fb8ed3 commit 20c635f

File tree

1 file changed

+52
-43
lines changed

1 file changed

+52
-43
lines changed

src/com/redomar/game/Game.java

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,17 @@ public void tick() {
441441
level.tick();
442442
}
443443

444+
/*
445+
* This method displays the logic of the game.
446+
*/
444447
public void render() {
445-
BufferStrategy bs = getBufferStrategy();
448+
BufferStrategy bs = getBufferStrategy(); // An object to organize the data on the canvas
446449
if (bs == null) {
447-
createBufferStrategy(3);
450+
createBufferStrategy(3); // Creates a new bs with triple buffering, which reduces tearing and cross-image pixelation
448451
return;
449452
}
450453

454+
// Centres the player in the middle of the screen
451455
int xOffset = (int) getPlayer().getX() - (screen.getWidth() / 2);
452456
int yOffset = (int) getPlayer().getY() - (screen.getHeight() / 2);
453457

@@ -463,11 +467,12 @@ public void render() {
463467
level.renderEntities(screen);
464468
level.renderProjectileEntities(screen);
465469

470+
// Copies pixel data from the screen into the game
466471
for (int y = 0; y < screen.getHeight(); y++) {
467472
for (int x = 0; x < screen.getWidth(); x++) {
468473
int colourCode = screen.getPixels()[x + y * screen.getWidth()];
469-
if (colourCode < 255) {
470-
pixels[x + y * WIDTH] = colours[colourCode];
474+
if (colourCode < 255) { // If it is a valid colour code
475+
pixels[x + y * WIDTH] = colours[colourCode]; // Sets the corresponding pixel from the screen to the game
471476
}
472477
}
473478
}
@@ -477,85 +482,89 @@ public void render() {
477482
setChangeLevel(false);
478483
}
479484

480-
if (changeLevel == true) {
485+
if (changeLevel == true) { // If the player is teleporting to a different level
481486
print.print("Teleported into new world", PrintTypes.GAME);
482-
if (getMap() == 1) {
483-
setMap("/levels/water_level.png");
484-
if (getDummy() != null) { // Gave nullPointerException(); upon
487+
if (getMap() == 1) { // If player is currently on the first map, custom_level
488+
setMap("/levels/water_level.png"); // Change the map to water_level
489+
if (getDummy() != null) { // Gave nullPointerException(); upon // If there is currently a dummy NPC
485490
// entering new world.
486-
level.removeEntity(getDummy());
491+
level.removeEntity(getDummy()); // Remove the dummy NPC when teleporting
487492
setNpc(false);
488493
}
489-
level.removeEntity(getVendor());
494+
level.removeEntity(getVendor()); // When teleporting away from custom_level, remove vendor NPC (always found on custom_level)
490495
setMap(2);
491-
} else if (getMap() == 2) {
492-
setMap("/levels/custom_level.png");
493-
level.removeEntity(getDummy());
496+
} else if (getMap() == 2) { // If the player is currently on the second map, water_level
497+
setMap("/levels/custom_level.png"); // Change the map to custom_level
498+
level.removeEntity(getDummy()); // Remove the dummy NPC when teleporting
494499
setNpc(false);
495-
level.addEntity(getVendor());
500+
level.addEntity(getVendor()); // Add a vendor NPC (always found on custom_level)
496501
setMap(1);
497502
}
498503
changeLevel = false;
499504
}
500505

501506
Graphics g = bs.getDrawGraphics();
502-
g.drawRect(0, 0, getWidth(), getHeight());
503-
g.drawImage(image, 0, 0, getWidth(), getHeight() - 30, null);
507+
g.drawRect(0, 0, getWidth(), getHeight()); // Creates a rectangle the same size as the screen
508+
g.drawImage(image, 0, 0, getWidth(), getHeight() - 30, null); // Displays the contents of image on the screen
504509
status(g, isDevMode(), isClosing());
505510
// Font.render("Hi", screen, 0, 0, Colours.get(-1, -1, -1, 555), 1);
506-
g.drawImage(image2, 0, getHeight() - 30, getWidth(), getHeight(), null);
511+
g.drawImage(image2, 0, getHeight() - 30, getWidth(), getHeight(), null); // Displays the contents of image2 on the screen
507512
g.setColor(Color.WHITE);
508-
g.setFont(font.getSegoe());
513+
g.setFont(font.getSegoe()); // Sets the font to Segoe UI
509514
g.drawString(
510-
"Welcome "
515+
"Welcome " // Welcomes the player's username in white in the bottom left corner of the screen
511516
+ WordUtils.capitalizeFully(player
512517
.getSanitisedUsername()), 3, getHeight() - 17);
513518
g.setColor(Color.ORANGE);
514519

515-
if (context.getLocale().getCountry().equals("BE")
516-
|| context.getLocale().getCountry().equals("FR")) {
520+
if (context.getLocale().getCountry().equals("BE") // If the player resides in Belgium or France (i.e. uses AZERTY keyboard)
521+
|| context.getLocale().getCountry().equals("FR")) { // Displays "Press A to quit" in orange at the bottom-middle portion of the screen
517522
g.drawString("Press A to quit", (getWidth() / 2)
518523
- ("Press A to quit".length() * 3), getHeight() - 17);
519-
} else {
520-
g.drawString("Press Q to quit", (getWidth() / 2)
524+
} else { // If the player resides anywhere else (i.e. uses QWERTY keyboard)
525+
g.drawString("Press Q to quit", (getWidth() / 2) // Displays "Press Q to quit" in orange at the bottom-middle portion of the screen
521526
- ("Press Q to quit".length() * 3), getHeight() - 17);
522527
}
523528
g.setColor(Color.YELLOW);
524-
g.drawString(time.getTime(), (getWidth() - 58), (getHeight() - 3));
529+
g.drawString(time.getTime(), (getWidth() - 58), (getHeight() - 3)); // Display the current time in yellow in the bottom right corner of the screen (hh:mm:ss)
525530
g.setColor(Color.GREEN);
526-
if(backgroundMusic.getActive()) {
527-
g.drawString("MUSIC is ON ", 3, getHeight() - 3);
531+
if(backgroundMusic.getActive()) { // If music is turned on
532+
g.drawString("MUSIC is ON ", 3, getHeight() - 3); // Display "MUSIC IS ON" in green in the bottom left corner of the screen.
528533
}
529-
g.dispose();
530-
bs.show();
534+
g.dispose(); // Frees up memory and resources for graphics
535+
bs.show(); // Shows contents of buffer
531536
}
532537

538+
/*
539+
* This method displays information regarding various aspects/stats of the game, dependent upon
540+
* whether it is running in developer mode, or if the application is closing.
541+
*/
533542
private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) {
534-
if (TerminalMode == true) {
543+
if (TerminalMode == true) { // If running in developer mode
535544
g.setColor(Color.CYAN);
536-
g.drawString("JavaGame Stats", 0, 10);
537-
g.drawString("FPS/TPS: " + fps + "/" + tps, 0, 25);
538-
if ((player.getNumSteps() & 15) == 15) {
539-
steps += 1;
545+
g.drawString("JavaGame Stats", 0, 10); // Display "JavaGame Stats" in cyan at the bottom left of the screen
546+
g.drawString("FPS/TPS: " + fps + "/" + tps, 0, 25); // Display the FPS and TPS directly above "JavaGame Stats"
547+
if ((player.getNumSteps() & 15) == 15) {
548+
steps += 1;
540549
}
541-
g.drawString("Foot Steps: " + steps, 0, 40);
542-
g.drawString(
543-
"NPC: " + WordUtils.capitalize(String.valueOf(isNpc())), 0,
550+
g.drawString("Foot Steps: " + steps, 0, 40); // Display the number of "Foot Steps" (above the previous)
551+
g.drawString(
552+
"NPC: " + WordUtils.capitalize(String.valueOf(isNpc())), 0, // Displays whether the NPC is on the level (above the previous)
544553
55);
545-
g.drawString("Mouse: " + getMouse().getX() + "x |"
554+
g.drawString("Mouse: " + getMouse().getX() + "x |" // Displays the position of the cursor (above the previous)
546555
+ getMouse().getY() + "y", 0, 70);
547-
if (getMouse().getButton() != -1)
548-
g.drawString("Button: " + getMouse().getButton(), 0, 85);
556+
if (getMouse().getButton() != -1) // If a mouse button is pressed (1, 2, or 3)
557+
g.drawString("Button: " + getMouse().getButton(), 0, 85); // Displays the button that is pressed (above the previous)
549558
g.setColor(Color.CYAN);
550559
g.fillRect(getMouse().getX() - 12, getMouse().getY() - 12, 24, 24);
551560
}
552-
if (TerminalQuit == true) {
561+
if (TerminalQuit == true) { // If the game is quitting
553562
g.setColor(Color.BLACK);
554-
g.fillRect(0, 0, getWidth(), getHeight());
563+
g.fillRect(0, 0, getWidth(), getHeight()); // Make the screen fully black
555564
g.setColor(Color.RED);
556-
g.drawString("Shutting down the Game", (getWidth() / 2) - 70,
565+
g.drawString("Shutting down the Game", (getWidth() / 2) - 70, // Display "Shutting down the Game" in red in the middle of the screen
557566
(getHeight() / 2) - 8);
558-
g.dispose();
567+
g.dispose(); // Free up memory
559568
}
560569
}
561570

0 commit comments

Comments
 (0)