From 484e76f0057c1c9b1138c83ec86659a3ecd10bb9 Mon Sep 17 00:00:00 2001 From: DanielLopez10122 <44179036+DanielLopez10122@users.noreply.github.com> Date: Mon, 20 May 2019 12:18:53 -0500 Subject: [PATCH] Updated Game with Error This is the game with the tower code integrated with the penguin code. The projectile code is also here. Please note that the below error occurs when trying to run the code. The error comes from the arrays of slopeX, slopeY, slopXConstrain, and slopeYConstrain within the penguin class. java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at processing.core.PApplet.runSketch(PApplet.java:10741) at processing.core.PApplet.main(PApplet.java:10511) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at processing.core.PApplet.runSketch(PApplet.java:10735) ... 1 more Caused by: java.lang.NullPointerException at CPTD_TrackTest$Penguin.(CPTD_TrackTest.java:116) at CPTD_TrackTest$Interface.(CPTD_TrackTest.java:77) at CPTD_TrackTest.(CPTD_TrackTest.java:19) ... 6 more --- CPTD_TrackTest/CPTD_TrackTest.pde | 57 +++++++++++++ CPTD_TrackTest/Interface.pde | 27 +++++++ CPTD_TrackTest/Penguin.pde | 128 ++++++++++++++++++++++++++++++ CPTD_TrackTest/Projectile.pde | 29 +++++++ CPTD_TrackTest/SWORDS.PDE | 45 +++++++++++ CPTD_TrackTest/Stage.pde | 82 +++++++++++++++++++ CPTD_TrackTest/TARGET.PDE | 88 ++++++++++++++++++++ CPTD_TrackTest/TrackTiles.pde | 48 +++++++++++ CPTD_TrackTest/onScreen.pde | 40 ++++++++++ 9 files changed, 544 insertions(+) create mode 100644 CPTD_TrackTest/CPTD_TrackTest.pde create mode 100644 CPTD_TrackTest/Interface.pde create mode 100644 CPTD_TrackTest/Penguin.pde create mode 100644 CPTD_TrackTest/Projectile.pde create mode 100644 CPTD_TrackTest/SWORDS.PDE create mode 100644 CPTD_TrackTest/Stage.pde create mode 100644 CPTD_TrackTest/TARGET.PDE create mode 100644 CPTD_TrackTest/TrackTiles.pde create mode 100644 CPTD_TrackTest/onScreen.pde diff --git a/CPTD_TrackTest/CPTD_TrackTest.pde b/CPTD_TrackTest/CPTD_TrackTest.pde new file mode 100644 index 0000000..fcd884b --- /dev/null +++ b/CPTD_TrackTest/CPTD_TrackTest.pde @@ -0,0 +1,57 @@ +Cell cells[][] = new Cell[13][12]; +Stage track = new Stage(); +Interface UI = new Interface(); +Puffle puffle[] = new Puffle[10]; +Penguin peng[] = new Penguin[10]; +color blue = color(50, 50, 255); +color red = color(255, 25, 25); +color pink = color(255, 150, 150); + +void setup() { + rectMode(CENTER); + size(850, 600); + for (int i = 0; i < puffle.length; i++) { + puffle[i] = new Puffle(); + } + + for (int i=0; i<13; i++) { + for (int j=0; j<12; j++) { + cells[i][j] = new Cell(i*50+225, j*50+25); + } + } + + for (int i = 0; i < peng.length; i++) { + peng[i] = new Penguin(0, 0); + } +} + +void draw() { + background(200); + track.display(); + puffle[0].display(); + puffle[0].followTrack(); + + showTowers(); + for (int i = 0; i < peng.length; i++) { + peng[i].display(); + peng[i].update(); + peng[i].calculateSlope(); + } + + //cells[1][2].vertical(); + //cells[1][3].vertical(); + //cells[6][2].vertical(); + //cells[6][3].vertical(); + //cells[6][5].vertical(); + //cells[6][6].vertical(); + //cells[6][8].vertical(); + //cells[6][9].vertical(); + //cells[11][8].vertical(); + //cells[11][9].vertic); + + // puffle.update(); + + UI.display(); + // println(track.getCellX(mouseX)); + // println(track.getCellY(mouseY)); +} diff --git a/CPTD_TrackTest/Interface.pde b/CPTD_TrackTest/Interface.pde new file mode 100644 index 0000000..93870b4 --- /dev/null +++ b/CPTD_TrackTest/Interface.pde @@ -0,0 +1,27 @@ +class Interface { + float opt1X = 75, opt1Y = 75; + Penguin pI = new Penguin(opt1X, opt1Y); + + Interface() { + } + + void display() { // the left-most choosing ui thing + noStroke(); + fill(80); + rect(100, height/2, 200, height); + + displayTowers(); // displays the tower options + } + + void displayTowers() { + stroke(240); + strokeWeight(3); + fill(200, 140); + ellipse(opt1X, opt1Y, 60, 60); // surrounding circle + + pI.display(); + } + + void displayUpgrades() { + } +} diff --git a/CPTD_TrackTest/Penguin.pde b/CPTD_TrackTest/Penguin.pde new file mode 100644 index 0000000..e268ead --- /dev/null +++ b/CPTD_TrackTest/Penguin.pde @@ -0,0 +1,128 @@ +class Penguin { + float pengx, pengy, armrx, armlx, army, slopeX[], slopeY[], slopeXConstrain[], slopeYConstrain[]; + boolean stay = false; + + //float pengCellX[] = new float[13]; + // float peng + + Penguin(float tx, float ty) { + pengx = tx; + pengy = ty; + armrx = pengx + 20; + armlx = pengx - 20; + army = pengy - 5; + + slopeX = new float[puffle.length]; + slopeY = new float[puffle.length]; + slopeXConstrain = new float[puffle.length]; + slopeYConstrain = new float[puffle.length]; + } + + void calculateSlope() { + for (int i = 0; i < puffle.length; i++) { + slopeX[i] = 0.05 * (puffle[i].getX() - pengx); + slopeXConstrain[i] = constrain(slopeX[i], -6, 6); + + slopeY[i] = 0.05 * (puffle[i].getY() - pengy); + slopeYConstrain[i] = constrain(slopeY[i], -6, 6); + } + } + + float getX() { + return pengx; + } + + float getY() { + return pengy; + } + + void setX(float tx) { + pengx = tx; + armrx = pengx + 20; + armlx = pengx - 20; + } + + void setY(float ty) { + pengy = ty; + army = pengy - 5; + } + + //float getSlopeXConstrain(int k) { + // return slopeXConstrain[k]; + //} + //float getSlopeYConstrain(int k) { + // return slopeYConstrain[k]; + //} + + void display() { + fill(0); + strokeWeight(1); + stroke(255); + ellipse(pengx, pengy, 40, 30); //body + + beginShape(); + vertex(armlx, army); + vertex(armlx - 8, army + 5); + vertex(armlx - 12, army + 18); + vertex(armlx - 5, army + 10); + vertex(armlx, army + 8); + endShape(); + + beginShape(); + vertex(armrx, army); + vertex(armrx + 8, army + 5); + vertex(armrx + 12, army + 18); + vertex(armrx + 5, army + 10); + vertex(armrx + 1, army + 8); + endShape(); + + fill(255, 146, 3); + beginShape(); //beak2 + vertex(pengx - 5, pengy + 15); + vertex(pengx - 5, pengy + 20); + bezierVertex(pengx - 5, pengy + 20, pengx, pengy + 30, pengx + 5, pengy + 20); + vertex(pengx + 5, pengy + 15); + endShape(); + + fill(255); + ellipse(pengx - 7, pengy + 10, 8, 5); //eyes + ellipse(pengx + 7, pengy + 10, 8, 5); + + fill(0); + ellipse(pengx - 7, pengy + 11, 4, 5); + ellipse(pengx + 7, pengy + 11, 4, 5); + } + + void update() { //NEW + pengx = (track.getCellX(mouseX)*50)+225; + pengy = (track.getCellY(mouseY)*50)+25; + armrx = pengx + 20; + armlx = pengx - 20; + army = pengy - 5; + } + //void detectPuff(){ + // for(Puffle p: puffle){ + // p.check(); + // } + //} + + void setStay(boolean set) { //pass in t + if (mouseX > 200) { //extra to not interfere with ui + + if (set) { //core of the code + stay = true; + } else { + stay = false; + } + } + } + + boolean inside() { + float d = dist(pengx, pengy, mouseX, mouseY); + if (d <= 35) { + return true; + } else { + return false; + } + } +} diff --git a/CPTD_TrackTest/Projectile.pde b/CPTD_TrackTest/Projectile.pde new file mode 100644 index 0000000..c4667c0 --- /dev/null +++ b/CPTD_TrackTest/Projectile.pde @@ -0,0 +1,29 @@ +class Projectile { + float x = 0; + float y = 0; + + Projectile(float tx, float ty) { + x = tx; + y = ty; + } + + void move(float moveX, float moveY) { + x = x + moveX; + y = y + moveY; + } + + float getX() { + return x; + } + float getY() { + return y; + } + + void setX(float tx) { + x = tx; + } + + void setY(float ty) { + y = ty; + } +} \ No newline at end of file diff --git a/CPTD_TrackTest/SWORDS.PDE b/CPTD_TrackTest/SWORDS.PDE new file mode 100644 index 0000000..f212ee1 --- /dev/null +++ b/CPTD_TrackTest/SWORDS.PDE @@ -0,0 +1,45 @@ +class Sword { + + float x, y; + + Sword(float tx, float ty) { + x = tx; + y = ty; + } + + void display() { + pushMatrix(); + + translate(width/2 + x, height/2 + y); + rotate(0.003*PI* millis()); + rectMode(CENTER); + stroke(140); + fill(220); + + rect(0, 0, 5, 60, 4); + fill(139, 69, 19); + rect(0, 42, 6.5, 35); + + popMatrix(); + } + + void move(float moveX, float moveY) { + x = x + moveX; + y = y + moveY; + } + + float getX() { + return x; + } + float getY() { + return y; + } + + void setX(float tx) { + x = tx; + } + + void setY(float ty) { + y = ty; + } +} diff --git a/CPTD_TrackTest/Stage.pde b/CPTD_TrackTest/Stage.pde new file mode 100644 index 0000000..b02e733 --- /dev/null +++ b/CPTD_TrackTest/Stage.pde @@ -0,0 +1,82 @@ +class Stage { + + boolean show[][] = { {false, false, false, false, false, false, false, true, false, false, false, false }, + { false, true, true, true, true, false, false, true, false, false, false, false }, + { false, true, false, false, true, false, false, true, false, false, false, false }, + { false, true, false, false, true, false, false, true, false, false, false, false }, + { false, true, false, false, true, false, false, true, false, false, false, false }, + { false, true, false, false, true, false, false, true, false, false, false, false }, + { false, true, true, true, true, true, true, true, true, true, true, false }, + { false, false, false, false, true, false, false, true, false, false, true, false }, + { false, false, false, false, true, false, false, true, false, false, true, false }, + { false, false, false, false, true, false, false, true, false, false, true, false }, + { false, false, false, false, true, false, false, true, false, false, true, false }, + { false, false, false, false, true, false, false, true, true, true, true, false }, + { false, false, false, false, true, false, false, false, false, false, false, false } }; + + boolean pengCells[][] = { {false, false, false, false, false, false, false, true, false, false, false, false }, + { false, true, true, true, true, false, false, true, false, false, false, false }, + { false, true, false, false, true, false, false, true, false, false, false, false }, + { false, true, false, false, true, false, false, true, false, false, false, false }, + { false, true, false, false, true, false, false, true, false, false, false, false }, + { false, true, false, false, true, false, false, true, false, false, false, false }, + { false, true, true, true, true, true, true, true, true, true, true, false }, + { false, false, false, false, true, false, false, true, false, false, true, false }, + { false, false, false, false, true, false, false, true, false, false, true, false }, + { false, false, false, false, true, false, false, true, false, false, true, false }, + { false, false, false, false, true, false, false, true, false, false, true, false }, + { false, false, false, false, true, false, false, true, true, true, true, false }, + { false, false, false, false, true, false, false, false, false, false, false, false } }; + + Stage() { + } + + + void display() { + for (int i=0; i<13; i++) { + for (int j=0; j<12; j++) { + highlightGreen(i, j); + if ((show[i][j] == false)) { + cells[i][j].display(); + } + } + } + } + + int getCellX(float cellX) { + int mapX = int(map(cellX, 200, 850, 0, 13)); + int consX = constrain(mapX, 0, 13); + return consX; //always needs to return int + } + + int getCellY(float cellY) { + int mapY = int(map(cellY, 0, 650, 0, 13)); + return mapY; + } + + void highlightGreen(int x, int y) { + if (mouseX > 200) { //restriction to making the fill + if (getCellX(mouseX) == x && getCellY(mouseY) == y) { //IMPORTANT CODE STRUCTURE FOR CHECKING ALL CELLS + cells[x][y].cFill = color(24, 189, 28, 125); + } else { //refills when i and j do not match + cells[x][y].cFill = color(225, 240, 247); + } + } else { //refills when moves off the screen + cells[x][y].cFill = color(225, 240, 247); + } + } + + void highlightRed(int x, int y) { + if (mouseX > 200) { //restriction to making the fill + if (getCellX(mouseX) == x && getCellY(mouseY) == y) { //IMPORTANT CODE STRUCTURE FOR CHECKING ALL CELLS + cells[x][y].cFill = color(206, 19, 20, 25); + } else { //refills when i and j do not match + cells[x][y].cFill = color(225, 240, 247); + + // cells[x][y].cFill = color(225, 240, 247); } + } + } else { //refills when moves off the screen + cells[x][y].cFill = color(225, 240, 247); + } + } +} diff --git a/CPTD_TrackTest/TARGET.PDE b/CPTD_TrackTest/TARGET.PDE new file mode 100644 index 0000000..1f62cee --- /dev/null +++ b/CPTD_TrackTest/TARGET.PDE @@ -0,0 +1,88 @@ +class Puffle { + + float x, y, xspeed, yspeed; + float xPos[] = { 275, 275, 525, 525, 775, 775, 0 }; + float yPos[] = { 225, 75, 75, 525, 525, 375, 0 }; + int moves[] = { 3, 1, 4, 1, 3, 2 }; + int posOrder_ = 0; // needs to constrain + int posOrder; // actual count value for x and y positions of the track + float r, offset; + float s; // making the speed work off of the radius + + Puffle() { + x = width + 50; + y = 225; + posOrder = constrain(posOrder_, 0, 5); + s = 2.8; + offset = 3; + moveLeft(); + } + + void display() { + fill(0, 235, 0); + stroke(0); + strokeWeight(1); + ellipse(x, y, 40, 40); + } + + void update() { + x += xspeed; + y += yspeed; + println(xspeed); + } + + float getX() { + return x; + } + float getY() { + return y; + } + + void setX(float tx) { + x = tx; + } + + void setY(float ty) { + y = ty; + } + + void moveRight() { // 1 + xspeed = s; + yspeed = 0; + } + void moveLeft() { // 2 + xspeed = -s; + yspeed = 0; + } + void moveUp() { // 3 + xspeed = 0; + yspeed = -s; + } + void moveDown() { // 4 + xspeed = 0; + yspeed = s; + } + + void followTrack() { + update(); + + + if ( (x >= xPos[posOrder] - r && x <= xPos[posOrder] + r) + && (y >= yPos[posOrder] - r && y <= yPos[posOrder] + r) ) { + + if (moves[posOrder] == 1) { + moveRight(); + posOrder ++; + } else if (moves[posOrder] == 2) { + moveLeft(); + posOrder ++; + } else if (moves[posOrder] == 3) { + moveUp(); + posOrder ++; + } else if (moves[posOrder] == 4) { + moveDown(); + posOrder ++; + } + } + } +} diff --git a/CPTD_TrackTest/TrackTiles.pde b/CPTD_TrackTest/TrackTiles.pde new file mode 100644 index 0000000..b70e562 --- /dev/null +++ b/CPTD_TrackTest/TrackTiles.pde @@ -0,0 +1,48 @@ + +class Cell { + float x, y; + color cFill = color(225, 240, 247); + color cStroke = color(175, 190, 193); + + Cell(float x_, float y_) { + x = x_; + y = y_; + } + + void display() { + pushMatrix(); + translate(x, y); + strokeWeight(3); + stroke(cStroke); + fill(cFill); + rect(0, 0, 50, 50); + popMatrix(); + } + + void vertical() { // vertical lanes of ice + pushMatrix(); + translate(x, y); + + strokeWeight(6); + noStroke(); + fill(11, 225, 225, 100); + rect(0, 0, 50, 50); + stroke(255); + line(-25, -25, -25, 25); + line(25, -25, 25, 25); + popMatrix(); + } + void horizontal() { + pushMatrix(); + translate(x, y); + + strokeWeight(6); + noStroke(); + fill(11, 225, 225, 100); + rect(0, 0, 50, 50); + stroke(255); + line(-25, -25, 25, -25); + line(25, 25, -25, 25); + popMatrix(); + } +} diff --git a/CPTD_TrackTest/onScreen.pde b/CPTD_TrackTest/onScreen.pde new file mode 100644 index 0000000..61ec67a --- /dev/null +++ b/CPTD_TrackTest/onScreen.pde @@ -0,0 +1,40 @@ +int pengCount = 0; +int pengCountC; +Penguin pengs[] = new Penguin[10]; + +void showTowers() { + pengCountC = constrain(pengCount, 0, 10); + int pengCellX = 0; + int pengCellY = 0; + for (int i = 0; i < pengCountC; i++) { + pengs[i].display(); + + + if (mousePressed) { + pengCellX = track.getCellX(mouseX); + pengCellY = track.getCellY(mouseY); + println(track.pengCells[track.getCellX(mouseX)][track.getCellY(mouseY)]); //something doesnt work + if (track.show[pengCellX][pengCellY] == false // check for if its track + && track.pengCells[track.getCellX(mouseX)][track.getCellY(mouseY)] == false) { //check to see if spot is ocupied //checks for penguin on mouse + pengs[i].setStay(true); + } + } + if (!pengs[i].stay) { // only moves to place when the boolean is false + pengs[i].update(); + } + } + for (int i = 0; i < pengCount; i++) { + if (pengs[i].inside()) { // add the check for other penguins + track.pengCells[pengCellX][pengCellY] = true; + } else { + track.pengCells[pengCellX][pengCellY] = false; + } + } +} + +void mousePressed() { //mousePress function to not stack events + pengCountC = constrain(pengCount, 0, 10); + if (mouseX < 200) { // change to UI.pI.inside(); + pengCount += 1; + } +}