Случайный порядок и нерест кактусов в игре Trex - PullRequest
0 голосов
/ 12 июля 2020

Я пытаюсь воссоздать игру Trex из Chrome. Я использую Processing и Java, чтобы сделать эту игру, но я все еще новичок в программировании (игра также очень грубая на данном этапе). Чтобы создать свою игру, я создал отдельные страницы для игрока, основной игры и различных кактусов, которые появятся в игре.

Различные кактусы в игре названы cactus1, cactus2 и cactus3. Код, определяющий их, довольно большой, но имеет ту же структуру. Число в конце переменной показывает, к кактусу она относится.

Моя проблема в том, что когда я пытался добавить рандомизатор (прокомментированные операторы if в коде кактусов), игра либо не отображается хоть что-нибудь, а то высота кактусов начинает пугать. Я также попытался изменить порядок кода, но он по-прежнему не меняет случайное появление или порядок кактусов. Пожалуйста, помогите, заранее спасибо! Дополнительный код ниже:

https://pastebin.com/F4uD1ht3

void setup() {
size(1800,1000);
background(255);
}

int gameScore = 0;

void draw() {
  background(255);
  line(0,700,1800,700);//ground
  collision();
  player();
  cactus1();
  if (cactusX <= 0) {//repeat for cactus 1 and add score
      cactusX = 1800;
      cactusHeight = int(random(0,99));
      cactus1();
      gameScore += 1;
    }
  cactus2();
   if (cactusX2 <= 0) {//repeat for cactus 2 and add score
      cactusX2 = 1800;
      cactusHeight2 = int(random(0,99));
      cactus2();
      gameScore += 1;
    }
  cactus3();
  if (cactusX3 <= 0) {//repeat for cactus 3 and add score
      cactusX3 = 1800;
      cactusHeight3 = int(random(0,99));
      cactus3();
      gameScore += 1;
    }
    //println(playerY, cactusX3, cactusHeight3);//diagnosing
}

https://pastebin.com/v95e3XNj

int cactusWidth = 50;
int cactusX = 800;
int cactusHeight = int(random(0,99));
int randomChance = 0;

void cactus1() {//normal cactus
  //randomChance = int(random(0,100));//uncommenting this will break the game
  //if (randomChance > 50){
    fill(0);
    rect(cactusX, 600 + cactusHeight, cactusWidth, 100 - cactusHeight);
    //}
  for (int count = 0; count <= 1000; count = count + gameSpeed); {
    cactusX = cactusX - gameSpeed;
  }
}

int cactusWidth2 = 75;
int cactusX2 = 1300;
int cactusHeight2 = int(random(0,99));

void cactus2() {//fat cactus
  //randomChance = int(random(0,100));//uncommenting this will break the game
  //if (randomChance > 50){
    fill(0);
    rect(cactusX2, 600 + cactusHeight2, cactusWidth2, 100 - cactusHeight2);
    //}
  for (int count = 0; count <= 1000; count = count + gameSpeed); {
      cactusX2 = cactusX2 - gameSpeed;
  }
}

int cactusWidth3 = 50;
int cactusX3 = 1800;
int cactusHeight3 = int(random(0,99));

void cactus3() {//flying cactus, don't question it
  //randomChance = int(random(0,100));//uncommenting this will break the game
  //if (randomChance > 50){
    fill(0);
    rect(cactusX3, 600, cactusWidth3, 100 - cactusHeight3);
   // }
  for (int count = 0; count <= 1000; count = count + gameSpeed); {
      cactusX3 = cactusX3 - gameSpeed;
  }
}

https://pastebin.com/pkHbHutj

int playerY = 600;
int playerHeight = 100;
int gameSpeed = 3;

void player() {//player
  fill(0);
  rect(100, playerY, 50, playerHeight);
}


void keyPressed() {
  if (key == 'w') {//function for jump
    playerY = 400;
  }
  else if (key == 's') {//function for duck
    playerY = 650;
    playerHeight = 50;
  }
}

void keyReleased() {//also function for jump and duck but the reverse when button is released
  if (key == 'w') {
    playerY = 600;
  }
  else if (key == 's') {
    playerHeight = 100;
    playerY = 600;
  }
}

void collision() { //collision detection, cactus 3 doesn't work when ducking
  if (playerY + playerHeight >= 600 + cactusHeight && 150 >= cactusX && 100 <= cactusX + cactusWidth) {//cactus 1
    gameSpeed = 0;
    println("Your score was " + gameScore + " , Game Over");
  }
  else if (playerY + playerHeight >= 600 + cactusHeight2 && 150 >= cactusX2 && 100 <= cactusX2 + cactusWidth2) {//cactus 2
    gameSpeed = 0;
    println("Your score was " + gameScore + " , Game Over");
  }
  else if (playerY + playerHeight >= 600 && playerY <= 600 + (100 - cactusHeight3) && 150 > cactusX3 && 100 < cactusX3 + cactusWidth3) {//cactus 3
    gameSpeed = 0;
    println("Your score was " + gameScore + " , Game Over");
  }
  
}
...