From 04cccbdef12edcc8dc11c21912eba10b7a01104a Mon Sep 17 00:00:00 2001 From: officereso Date: Sat, 25 May 2019 15:33:05 -0500 Subject: [PATCH] Code documentation, cleanup, and optimisation Signed-off-by: officereso --- .idea/dictionaries/QRSnow.xml | 3 + src/project/Draw.java | 9 ++- src/project/Main.java | 68 +++++++++---------- src/project/Sprites/NPC1.java | 18 ++--- src/project/Sprites/NPC2.java | 18 ++--- src/project/Sprites/NPC3.java | 18 ++--- src/project/Sprites/NPC4.java | 18 ++--- src/project/Sprites/NPC5.java | 18 ++--- src/project/Sprites/NPC6.java | 18 ++--- .../Sprites/{player.java => Player.java} | 13 +--- 10 files changed, 97 insertions(+), 104 deletions(-) create mode 100644 .idea/dictionaries/QRSnow.xml rename src/project/Sprites/{player.java => Player.java} (50%) diff --git a/.idea/dictionaries/QRSnow.xml b/.idea/dictionaries/QRSnow.xml new file mode 100644 index 0000000..1074b2c --- /dev/null +++ b/.idea/dictionaries/QRSnow.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/project/Draw.java b/src/project/Draw.java index dd54d6c..5d056c2 100644 --- a/src/project/Draw.java +++ b/src/project/Draw.java @@ -5,8 +5,7 @@ import java.awt.*; public class Draw extends Applet { public static void startScreen(Graphics g){ - - for(int i=0;i<=20;i++){ + for (int i = 0; i <= 20; i++) { // Draws the word START in a fall to the bottom left style. Expo.setRandomColor(g); Expo.setFont(g, "Arial", 1, 100 - i); Expo.drawString(g,"START",100+i*5,200+i*7); @@ -17,10 +16,10 @@ public class Draw extends Applet { road(g); btns(g); Expo.setFont(g, "Arial", 1, 15); - Expo.drawString(g, "Your score is " + score, 600, 100); + Expo.drawString(g, "Your score is " + score, 600, 100); // Draws score } - public static void end(Graphics g){ + public static void end(Graphics g) { // Draws screen that is shown on collision with another car or road shoulder Expo.setBackground(g,Colors.red); Expo.setColor(g,Colors.endGreen); Expo.setFont(g, "Arial", 1, 100); @@ -28,7 +27,7 @@ public class Draw extends Applet { } - public static void btns(Graphics g) { + public static void btns(Graphics g) { // Draws buttons responsible the user presses for player locomotion Expo.setFont(g, "Arial", 1, 100); Expo.setColor(g,Colors.startBlue); Expo.drawString(g,"<=",480,300); diff --git a/src/project/Main.java b/src/project/Main.java index b3af47e..2f99182 100644 --- a/src/project/Main.java +++ b/src/project/Main.java @@ -7,33 +7,17 @@ import java.awt.*; public class Main extends Applet { - private static boolean start = false; - private Rectangle startBtn = new Rectangle(100,100,340,600); - private static int score = 0; - private Rectangle lBtn = new Rectangle(480, 240, 150, 80); - private static int Px = 150; - private Rectangle rBtn = new Rectangle(660, 240, 150, 80); + private static boolean start = false; // Control for when game is started + private static boolean playerHitShoulder = false; + private static int score = 0; // Default player score + private static int Px = 150; // Default Px(Player x) + private Rectangle startBtn = new Rectangle(100, 100, 340, 600); // Rectangle that the user presses to start the game + private Rectangle lBtn = new Rectangle(480, 240, 150, 80); // Rectangle that the player presses to move player to the left + private Rectangle rBtn = new Rectangle(660, 240, 150, 80); // Rectangle that the player presses to move player to the right - public void paint(Graphics g) { - Expo.drawHeading(g, "Quentin Snow and Alekkai", "Final Project"); - if (start && !NPC1.ended && !NPC2.ended && !NPC3.ended && !NPC4.ended && !NPC5.ended && !NPC6.ended) { - Draw.begin(g, score); - playerLocomotion(g); - npcLocomotion(g); - - } - if (!start) { - Draw.startScreen(g); - } - - if (NPC1.ended || NPC2.ended || NPC3.ended || NPC4.ended || NPC5.ended || NPC6.ended) { - Draw.end(g); - } - } - - private static void npcLocomotion(Graphics g) { + private static void npcLocomotion(Graphics g) { // Logic for spawning NPC objects if (score > 3) { - new NPC1(g, Px); + new NPC1(g, Px); // Px is passed to NPCs for collision detection purposes } if (score > 30) { new NPC2(g, Px); @@ -52,28 +36,42 @@ public class Main extends Applet { } } - private static void playerLocomotion(Graphics g) { - project.Sprites.player.make(g, Px, 427); - if (Px <= 107 || Px >= 375) { - Draw.end(g); - Px = 10000; + Player.make(g, Px, 427); // Draw the player according to its Px + if (Px <= 107 || Px >= 375) { // Logic to detect if player has it the shoulder of road + playerHitShoulder = true; } } + public void paint(Graphics g) { + Expo.drawHeading(g, "Quentin Snow and Alekkai", "Final Project"); + if (start && !NPC1.collision && !NPC2.collision && !NPC3.collision && !NPC4.collision && !NPC5.collision && !NPC6.collision && !playerHitShoulder) { // Start and keep game running if 1. startBtn has been pressed. 2. No NPCs have detected a collision and player hasn't hit shoulder. + Draw.begin(g, score); + playerLocomotion(g); + npcLocomotion(g); + + } + if (!start) { + Draw.startScreen(g); // Draw the start screen if the game has not started + } + + if (NPC1.collision || NPC2.collision || NPC3.collision || NPC4.collision || NPC5.collision || NPC6.collision || playerHitShoulder) { // If any NPCs report a collision or player has hit the shoulder draw the end screen + Draw.end(g); + } + } public boolean mouseDown(Event e, int x, int y){ if (startBtn.inside(x,y)){ - start = true; + start = true; // Set start to true if user has clicked startBtn } if (lBtn.inside(x,y)){ - Px-=10; + Px -= 10; // Move player 10 pixels to left when lBtn is pressed } if (rBtn.inside(x,y)){ - Px+=10; + Px += 10; // Move player 10 pixels to left when lBtn is pressed } - score++; - repaint(); + score++; // For every click increase users score + repaint(); // Redraw everything return true; } } \ No newline at end of file diff --git a/src/project/Sprites/NPC1.java b/src/project/Sprites/NPC1.java index c3ba517..7c42f77 100644 --- a/src/project/Sprites/NPC1.java +++ b/src/project/Sprites/NPC1.java @@ -7,18 +7,18 @@ import java.applet.Applet; import java.awt.*; public class NPC1 extends Applet { - public static boolean ended = false; - private static int x = Expo.random(100, 400); - private static int y = -15; - private static int speed = Expo.random(10, 40); + public static boolean collision = false; + private static int x = Expo.random(100, 400); // Where NPC starts on road + private static int y = -15; // Default starting position for NPC. Negative because NPC is draw from bottom left to top right. + private static int speed = Expo.random(10, 40); // Value to move NPC down public NPC1(Graphics g, int Px) { make(g, Px); } public static void make(Graphics g, int Px) { - if (!(y >= 250 && y <= 350 && x >= Px - 40 && x <= Px + 43) && !ended) { - y += speed; + if (!(y >= 240 && y <= 350 && x >= Px - 40 && x <= Px + 43)) { // Collision detection + y += speed; // Moves NPC down according to its speed Expo.setColor(g, SpriteColors.NPC1Body); Expo.fillRectangle(g, x, y + 110, x + 35, y + 170); Expo.setColor(g, SpriteColors.Tires); @@ -27,10 +27,10 @@ public class NPC1 extends Applet { Expo.fillRectangle(g, x + 30, y + 118, x + 40, y + 138); Expo.fillRectangle(g, x + 30, y + 145, x + 40, y + 165); } else { - Draw.end(g); - ended = true; + Draw.end(g); // Draws end screen + collision = true; // Used by Main.java to detect if game is collision } - if (y > 350) { + if (y > 350) { // Used to "respawn" NPC once its y reaches a certain point. Does this by resetting all variables. x = Expo.random(100, 400); y = 25; speed = Expo.random(10, 40); diff --git a/src/project/Sprites/NPC2.java b/src/project/Sprites/NPC2.java index aa1595d..1178355 100644 --- a/src/project/Sprites/NPC2.java +++ b/src/project/Sprites/NPC2.java @@ -7,18 +7,18 @@ import java.applet.Applet; import java.awt.*; public class NPC2 extends Applet { - public static boolean ended = false; - private static int x = Expo.random(100, 400); - private static int y = -15; - private static int speed = Expo.random(10, 40); + public static boolean collision = false; + private static int x = Expo.random(100, 400); // Where NPC starts on road + private static int y = -15; // Default starting position for NPC. Negative because NPC is draw from bottom left to top right. + private static int speed = Expo.random(10, 40); // Value to move NPC down public NPC2(Graphics g, int Px) { make(g, Px); } public static void make(Graphics g, int Px) { - if (!(y >= 250 && y <= 350 && x >= Px - 40 && x <= Px + 43) && !ended) { - y += speed; + if (!(y >= 240 && y <= 350 && x >= Px - 40 && x <= Px + 43)) { // Collision detection + y += speed; // Moves NPC down according to its speed Expo.setColor(g, SpriteColors.NPC1Body); Expo.fillRectangle(g, x, y + 110, x + 35, y + 170); Expo.setColor(g, SpriteColors.Tires); @@ -27,10 +27,10 @@ public class NPC2 extends Applet { Expo.fillRectangle(g, x + 30, y + 118, x + 40, y + 138); Expo.fillRectangle(g, x + 30, y + 145, x + 40, y + 165); } else { - Draw.end(g); - ended = true; + Draw.end(g); // Draws end screen + collision = true; // Used by Main.java to detect if game is collision } - if (y > 350) { + if (y > 350) { // Used to "respawn" NPC once its y reaches a certain point. Does this by resetting all variables. x = Expo.random(100, 400); y = 25; speed = Expo.random(10, 40); diff --git a/src/project/Sprites/NPC3.java b/src/project/Sprites/NPC3.java index a163114..373f961 100644 --- a/src/project/Sprites/NPC3.java +++ b/src/project/Sprites/NPC3.java @@ -7,18 +7,18 @@ import java.applet.Applet; import java.awt.*; public class NPC3 extends Applet { - public static boolean ended = false; - private static int x = Expo.random(100, 400); - private static int y = -15; - private static int speed = Expo.random(10, 40); + public static boolean collision = false; + private static int x = Expo.random(100, 400); // Where NPC starts on road + private static int y = -15; // Default starting position for NPC. Negative because NPC is draw from bottom left to top right. + private static int speed = Expo.random(10, 40); // Value to move NPC down public NPC3(Graphics g, int Px) { make(g, Px); } public static void make(Graphics g, int Px) { - if (!(y >= 250 && y <= 350 && x >= Px - 40 && x <= Px + 43) && !ended) { - y += speed; + if (!(y >= 240 && y <= 350 && x >= Px - 40 && x <= Px + 43)) { // Collision detection + y += speed; // Moves NPC down according to its speed Expo.setColor(g, SpriteColors.NPC1Body); Expo.fillRectangle(g, x, y + 110, x + 35, y + 170); Expo.setColor(g, SpriteColors.Tires); @@ -27,10 +27,10 @@ public class NPC3 extends Applet { Expo.fillRectangle(g, x + 30, y + 118, x + 40, y + 138); Expo.fillRectangle(g, x + 30, y + 145, x + 40, y + 165); } else { - Draw.end(g); - ended = true; + Draw.end(g); // Draws end screen + collision = true; // Used by Main.java to detect if game is collision } - if (y > 350) { + if (y > 350) { // Used to "respawn" NPC once its y reaches a certain point. Does this by resetting all variables. x = Expo.random(100, 400); y = 25; speed = Expo.random(10, 40); diff --git a/src/project/Sprites/NPC4.java b/src/project/Sprites/NPC4.java index 5191096..62b93c4 100644 --- a/src/project/Sprites/NPC4.java +++ b/src/project/Sprites/NPC4.java @@ -7,18 +7,18 @@ import java.applet.Applet; import java.awt.*; public class NPC4 extends Applet { - public static boolean ended = false; - private static int x = Expo.random(100, 400); - private static int y = -15; - private static int speed = Expo.random(10, 40); + public static boolean collision = false; + private static int x = Expo.random(100, 400); // Where NPC starts on road + private static int y = -15; // Default starting position for NPC. Negative because NPC is draw from bottom left to top right. + private static int speed = Expo.random(10, 40); // Value to move NPC down public NPC4(Graphics g, int Px) { make(g, Px); } public static void make(Graphics g, int Px) { - if (!(y >= 250 && y <= 350 && x >= Px - 40 && x <= Px + 43) && !ended) { - y += speed; + if (!(y >= 240 && y <= 350 && x >= Px - 40 && x <= Px + 43)) { // Collision detection + y += speed; // Moves NPC down according to its speed Expo.setColor(g, SpriteColors.NPC1Body); Expo.fillRectangle(g, x, y + 110, x + 35, y + 170); Expo.setColor(g, SpriteColors.Tires); @@ -27,10 +27,10 @@ public class NPC4 extends Applet { Expo.fillRectangle(g, x + 30, y + 118, x + 40, y + 138); Expo.fillRectangle(g, x + 30, y + 145, x + 40, y + 165); } else { - Draw.end(g); - ended = true; + Draw.end(g); // Draws end screen + collision = true; // Used by Main.java to detect if game is collision } - if (y > 350) { + if (y > 350) { // Used to "respawn" NPC once its y reaches a certain point. Does this by resetting all variables. x = Expo.random(100, 400); y = 25; speed = Expo.random(10, 40); diff --git a/src/project/Sprites/NPC5.java b/src/project/Sprites/NPC5.java index b6ce787..81daf88 100644 --- a/src/project/Sprites/NPC5.java +++ b/src/project/Sprites/NPC5.java @@ -7,18 +7,18 @@ import java.applet.Applet; import java.awt.*; public class NPC5 extends Applet { - public static boolean ended = false; - private static int x = Expo.random(100, 400); - private static int y = -15; - private static int speed = Expo.random(10, 40); + public static boolean collision = false; + private static int x = Expo.random(100, 400); // Where NPC starts on road + private static int y = -15; // Default starting position for NPC. Negative because NPC is draw from bottom left to top right. + private static int speed = Expo.random(10, 40); // Value to move NPC down public NPC5(Graphics g, int Px) { make(g, Px); } public static void make(Graphics g, int Px) { - if (!(y >= 250 && y <= 350 && x >= Px - 40 && x <= Px + 43) && !ended) { - y += speed; + if (!(y >= 240 && y <= 350 && x >= Px - 40 && x <= Px + 43)) { // Collision detection + y += speed; // Moves NPC down according to its speed Expo.setColor(g, SpriteColors.NPC1Body); Expo.fillRectangle(g, x, y + 110, x + 35, y + 170); Expo.setColor(g, SpriteColors.Tires); @@ -27,10 +27,10 @@ public class NPC5 extends Applet { Expo.fillRectangle(g, x + 30, y + 118, x + 40, y + 138); Expo.fillRectangle(g, x + 30, y + 145, x + 40, y + 165); } else { - Draw.end(g); - ended = true; + Draw.end(g); // Draws end screen + collision = true; // Used by Main.java to detect if game is collision } - if (y > 350) { + if (y > 350) { // Used to "respawn" NPC once its y reaches a certain point. Does this by resetting all variables. x = Expo.random(100, 400); y = 25; speed = Expo.random(10, 40); diff --git a/src/project/Sprites/NPC6.java b/src/project/Sprites/NPC6.java index 72171ac..fb390ab 100644 --- a/src/project/Sprites/NPC6.java +++ b/src/project/Sprites/NPC6.java @@ -7,18 +7,18 @@ import java.applet.Applet; import java.awt.*; public class NPC6 extends Applet { - public static boolean ended = false; - private static int x = Expo.random(100, 400); - private static int y = -15; - private static int speed = Expo.random(10, 40); + public static boolean collision = false; + private static int x = Expo.random(100, 400); // Where NPC starts on road + private static int y = -15; // Default starting position for NPC. Negative because NPC is draw from bottom left to top right. + private static int speed = Expo.random(10, 40); // Value to move NPC down public NPC6(Graphics g, int Px) { make(g, Px); } public static void make(Graphics g, int Px) { - if (!(y >= 250 && y <= 350 && x >= Px - 40 && x <= Px + 43) && !ended) { - y += speed; + if (!(y >= 240 && y <= 350 && x >= Px - 40 && x <= Px + 43)) { // Collision detection + y += speed; // Moves NPC down according to its speed Expo.setColor(g, SpriteColors.NPC1Body); Expo.fillRectangle(g, x, y + 110, x + 35, y + 170); Expo.setColor(g, SpriteColors.Tires); @@ -27,10 +27,10 @@ public class NPC6 extends Applet { Expo.fillRectangle(g, x + 30, y + 118, x + 40, y + 138); Expo.fillRectangle(g, x + 30, y + 145, x + 40, y + 165); } else { - Draw.end(g); - ended = true; + Draw.end(g); // Draws end screen + collision = true; // Used by Main.java to detect if game is collision } - if (y > 350) { + if (y > 350) { // Used to "respawn" NPC once its y reaches a certain point. Does this by resetting all variables. x = Expo.random(100, 400); y = 25; speed = Expo.random(10, 40); diff --git a/src/project/Sprites/player.java b/src/project/Sprites/Player.java similarity index 50% rename from src/project/Sprites/player.java rename to src/project/Sprites/Player.java index 42bfd2b..c4ef788 100644 --- a/src/project/Sprites/player.java +++ b/src/project/Sprites/Player.java @@ -5,8 +5,8 @@ import project.Expo; import java.applet.Applet; import java.awt.*; -public class player extends Applet { - public static void make(Graphics g, int x, int y) { +public class Player extends Applet { + public static void make(Graphics g, int x, int y) { // Draws player at location of passes x and y integers Expo.setColor(g, SpriteColors.playerBody); Expo.fillRectangle(g, x, y, x+35, y+60); Expo.setColor(g, SpriteColors.Tires); @@ -15,11 +15,4 @@ public class player extends Applet { Expo.fillRectangle(g, x+30, y+8, x+40, y+28); Expo.fillRectangle(g, x+30, y+35, x+40, y+55); } -} -// Expo.setColor(g,SpriteColors.playerBody); -// Expo.fillRectangle(g, 100, 100, 135, 160); -// Expo.setColor(g,SpriteColors.Tires); -// Expo.fillRectangle(g, 105, 108, 95, 128); -// Expo.fillRectangle(g, 105, 135, 95, 155); -// Expo.fillRectangle(g, 130, 108, 140, 128); -// Expo.fillRectangle(g, 130, 135, 140, 155); \ No newline at end of file +} \ No newline at end of file