Java - 如何连接类,方法和构造函数?

时间:2018-06-12 03:56:16

标签: java class object connection

我在Processing中有一个游戏,有两个类 - 主要和玩家。我认为主类应该有设置和菜单,而玩家类有控制,碰撞,移动等。

我需要帮助找出如何连接这两个类,以便游戏可以运行。在主类中,一些变量和方法没有定义,所以我知道我没有引用或调用它们。 我对类和方法相当新,所以任何帮助都会受到赞赏。谢谢!

主要课程:

//used for setup
//makes program run
//diff currents? play/instr/dead/replay
PFont title, main, dead;
PImage candy, lollipop, cottonCandy;
PImage badApple, bananaPeel, pepper;
PImage panda, heart, bkgrd;
PImage [] goodCandy = new PImage [3];
PImage [] badFood = new PImage [3];
PrintWriter highScore;

String current="menu";
int menuBut1x, menuBut1y;
int menuBut2x, menuBut2y;
int menuBut3x, menuBut3y;
int backButx, backButy;

void setup() {
  size(1200,700);
  frameRate(30);
  title = loadFont("Gabriola-150.vlw");
  textFont(title);
  main = loadFont("SegoeUI-Light-60.vlw");
  textFont(main);
  dead = loadFont("Stencil-200.vlw");
  textFont(dead);

  highScore = createWriter("HighScore.txt");

  menuBut1x = width/3-70;
  menuBut1y = height/2-100;
  menuBut2x = width/3-70;
  menuBut2y = height/2+40;
  menuBut3x = width/3-70;
  menuBut3y = height/2+180;
  backButx = 1030;
  backButy = 10;
  candy = loadImage("goodCandy0.png");
  cottonCandy = loadImage("goodCandy1.png");
  lollipop = loadImage("goodCandy2.png");
  badApple = loadImage("badFood0.png");
  bananaPeel = loadImage("badFood1.png");
  pepper = loadImage("badFood2.png");
  panda = loadImage("panda.png");
  heart = loadImage("heart.png");
  bkgrd = loadImage("floatClouds.jpg");

  for (int i=0; i<goodCandy.length; i++) {
  goodCandy[i] = loadImage("goodCandy" + i + ".png"); //loading goodCandy imgs via array
}
  for (int i=0; i<badFood.length; i++) {
  badFood[i] = loadImage("badFood" + i + ".png"); //loading badFood imgs via array
}
}


public void menu (){
  background(#bfefff);
  fill(255);
  rect(menuBut1x, menuBut1y, 520, 100); //first button
  rect(menuBut2x, menuBut2y, 520, 100); //second button
  rect(menuBut3x, menuBut3y, 520, 100); //third button
  textSize(100);
  fill(#000000);
  textFont(title);
  text("Candy Dash!", 300, 180);
  textSize(200);
  fill(#000000);
  textFont(main);
  text("PLAY", 530, 320);
  textSize(65);
  fill(#000000);
  textFont(main);
  text("INSTRUCTIONS", 400, 465);
  textSize(65);
  fill(#000000);
  textFont(main);
  text("HIGH SCORES", 415, 600);

  if (current=="menu") {
    //background(#F0D5E2); DON'T HAVE BACKGROUND COLOUR HERE OR WILL COVER EVERYTHING - BKGRD TOP OF VOID DRAW
    if (mouseX >= menuBut1x && mouseX <= menuBut1x+520 && mouseY >= menuBut1y && mouseY <= menuBut1y+100 && mousePressed) {
          current="play";   //clicked play button     
  }
  if (mouseX >= menuBut2x && mouseX <= menuBut2x+520 && mouseY >= menuBut2y && mouseY <= menuBut2y+100 && mousePressed) {
          current="instructions";   //clicked instructions button     
  }
  if (mouseX >= menuBut3x && mouseX <= menuBut3x+520 && mouseY >= menuBut3y && mouseY <= menuBut3y+100 && mousePressed) {
          current="high scores";   //clicked high scores button     
  }
  pandaX = 550; //resets the panda to starting position 
  pandaY = 580; //RESET EVERYTHING WHEN EXIT
  points = 0;
  }

  if (current=="instructions") {
    background(#e8f0f7);
    textSize(80);
    text("INSTRUCTIONS", 360, 140);
    textSize(35);
    text("Use the arrow keys to move left and right.", 100,240);
    text("Your goal is to collect all the yummy candies, and avoid the bad foods!", 100, 300);
    text("Watch out! Touch a bad food and YOU ARE DEAD!", 100, 360);
    fill(#F0FFF0); //back button
    rect(backButx, backButy, 160, 100); //back button
    textSize(50); 
    fill(#000000);
    text("MENU", 1040, 75);
    textSize(35); 
    fill(#000000);
    text("COLLECT THESE:", 180, 450);
    image(candy, 80, 480, 120, 80);
    image(lollipop, 220, 480, 100, 170);
    image(cottonCandy, 350, 480, 100, 180);
    text("AVOID THESE:", 800, 450);
    image(badApple, 720, 480, 100, 140);
    image(bananaPeel, 830, 480, 180, 170);
    image(pepper, 1030, 470, 100, 170);

    if (mouseX >= backButx && mouseX <= backButx+160 && mouseY >= backButy && mouseY <= backButy+100 && mousePressed) {
          current="menu";   //clicked menu button     
  }
  }

  if (current=="high scores") {
    background(#e8f0f7);
    fill(#F0FFF0);
    rect(backButx, backButy, 160, 100); //back button
    textSize(50);
    fill(#000000);
    text("MENU", 1040, 75);
    textSize(100);
    text("HIGH SCORE: " + points, 300, height/2);

    if (mouseX >= backButx && mouseX <= backButx+160 && mouseY >= backButy && mouseY <= backButy+100 && mousePressed) {
          current="menu";   //clicked menu button      
  }
  }

    if (current=="play") {
    image(bkgrd, 0, 0, 1200, 700);
    //background(#cfe1e1);
    fill(#F0FFF0);
    rect(backButx, backButy, 160, 100); //back button
    textSize(50);
    fill(#000000);
    text("MENU", 1040, 75);

    candyFall(); //calling methods
    touchCandy();
    newCandy();
    badFoodFall();

    if (mouseX >= backButx && mouseX <= backButx+160 && mouseY >= backButy && mouseY <= backButy+100 && mousePressed) {
          current="menu";   //clicked menu button  
  }
  }
  if (current=="gameOver") {
    background(#7B0B1F);
    textFont(dead);
    textSize(150);
    text("YOU ARE DEAD", 70, height/2);

    textFont(main);
    fill(#fbfbfb);
    rect(200, 500, 300, 150); //replay button
    textSize(80);
    fill(#000000);
    noStroke();
    text("REPLAY", 220, 600);

    fill(#fbfbfb);
    rect(700, 500, 300, 150); //menu button
    textSize(80);
    fill(#000000);
    noStroke();
    text("MENU", 745, 600);
    highScore.println("High Score: " + points); // Write the points to the file

    points=0; //reset points to 0 when dead
    pandaX = 550; //resets pandaX when dead
    pandaY = 580; //resets pandaY when dead

  if (mouseX >= 200 && mouseX <= 200+300 && mouseY >= 500 && mouseY <= 500+150 && mousePressed) {
          current="play";   //clicked replay
  }
  if (mouseX >= 700 && mouseX <= 700+300 && mouseY >= 500 && mouseY <= 500+150 && mousePressed) {
          current="menu";   //clicked menu button 
  }
}
  }

玩家类:

//controls movement of panda, collision w/ falling objects, etc -- all in different methods

import java.io.Serializable;

public class Player implements Serializable {

  public int pandaX, pandaY, pandaW, pandaH;
  public int candyY, candyX, candyW, candyH;
  public int randCandyW, randCandyH, badAppleX, randY, lolliY;
  public int rand, randX, randX2, lolliX;
  public int points;
  public int candySpeed, foodSpeed;
  public int yDirCandy, yDirFood;

  //constructor of class, where default values are assigned (coordinates, points, etc)
  public Player(){
   //setting up default coordinates
   pandaX = 550;
   pandaY = 580;
   pandaW = 80;
   pandaH = 112;
   points = 0;

   candySpeed = 10;
   foodSpeed = 5;
   yDirCandy = 1;
   yDirFood = 1;
   candyY = 10;
   candyX = 200;
   candyW = 187;
   candyH = 121;
   randCandyW = 100;
   randCandyH = 100;
   badAppleX = 700;

   randY = -200;
   lolliY = -600;

   rand = (int) (2*Math.random()) +1;
   randX = (int) (1100*Math.random())+20; 
   randX2 = (int) (1100*Math.random())+20; 
   lolliX = (int) (1100*Math.random())+20;

  }
  public int GetPoints(){ //just test
    return points;
  }

  public void candyFall() {
  image(panda, pandaX, pandaY, pandaW, pandaH);
  fill(#000000);
  text("Points: " + points, 20, 70);
  touchCandy();
  newCandy();

  image(candy, candyX, candyY, candyW, candyH);  //original candy
  candyY = candyY + (candySpeed * yDirCandy);

  image(goodCandy[rand], randX, randY, randCandyW, randCandyH); //rand candy
  randY = randY + (candySpeed * yDirCandy);

  image(lollipop, lolliX, lolliY, randCandyW, randCandyH); //lolli candy
  lolliY = lolliY + (candySpeed * yDirCandy);

highScore.println("High Score: " + points); // Write the points to the file
}

public void badFoodFall(){
  image(badFood[rand], randX2, randY, randCandyW, randCandyH);  //rand food
  randY = randY + (candySpeed * yDirCandy);

  image(badApple, badAppleX, randY, randCandyW, randCandyH);  //rand food
  randY = randY + (candySpeed * yDirFood);
}
public void keyPressed() {
  if (key==CODED) {
    if (keyCode==LEFT) {
      pandaX = pandaX-20; 
    }
    if (keyCode==RIGHT) {
      pandaX = pandaX+20;
    }
   if (pandaX<=5) {
     pandaX=5; //if hit into wall panda won't go off screen
   }
   if (pandaX>=1120) {
     pandaX=1120;
   }
   highScore.flush(); // Writes the remaining data to the file
   highScore.close(); // Finishes the file
  }

}

public void touchCandy() {
  if (  (pandaX + pandaW > candyX && //original candy
        pandaX < candyX + candyW &&
        pandaY + pandaH > candyY &&
        pandaY < candyY + candyH) ||
        (pandaX + pandaW > randX && //rand candy
        pandaX < randX + randCandyW &&
        pandaY + pandaH > randY &&
        pandaY < randY + randCandyH) ||
        (pandaX + pandaW > lolliX && //lollipop
        pandaX < lolliX + randCandyW &&
        pandaY + pandaH > lolliY &&
        pandaY < lolliY + randCandyH)
        )
        {
          textSize(60);
          text("YUM! You get points!", 370, 300);
          points = points + 1; //if panda touches candy, get points!
          image(heart, pandaX+20, pandaY-50, 50, 50);
        }
  if ((pandaX + pandaW > randX2 && //rand food
        pandaX < randX2 + randCandyW &&
        pandaY + pandaH > randY &&
        pandaY < randY + randCandyH) ||
        (pandaX + pandaW > badAppleX && //apple
        pandaX < badAppleX + randCandyW &&
        pandaY + pandaH > randY &&
        pandaY < randY + randCandyH))
        {
          current="gameOver";
        }

}

public void newCandy() {
  if (candyY>=850) {
    candyY=0;  //resetting the original candy to top
  }
  if (lolliY>=850) {
    lolliY=0; //resetting lolli to top
  }
  if (randY>=850) {
    randY=0;
  }
}

}

0 个答案:

没有答案