gametheory

updated
mail@pastecode.io avatar
unknown
java
a year ago
1.6 kB
20
Indexable
Never
float characterX = 35;
float characterY = 35;

float obstacle1X;
float obstacle1Y;
float obstacle2X;
float obstacle2Y;

void setup() {
  size(700, 700);
  obstacle1X = random(width);
  obstacle1Y = random(height);
  obstacle2X = random(width);
  obstacle2Y = random(height);
}

void draw() {
  background(0);
  drawCharacter();
  drawObstacles();
  if (checkCollision()) {
    endGame();
  }
  line(characterX + 35, characterY, characterX + 35, 0);
}

void drawCharacter() {
  pushMatrix();
  translate(characterX, characterY);
  beginShape();
  // draw character shape
  endShape(CLOSE);
  popMatrix();
}

void keyPressed() {
  if (keyCode == UP_ARROW) {
    characterY = max(characterY - 70, 0);
  } else if (keyCode == DOWN_ARROW) {
    characterY = min(characterY + 70, height - 70);
  } else if (keyCode == LEFT_ARROW) {
    characterX = max(characterX - 70, 0);
  } else if (keyCode == RIGHT_ARROW) {
    characterX = min(characterX + 70, width - 70);
  }
}

void drawObstacles() {
  rect(obstacle1X, obstacle1Y, 50, 50);
  rect(obstacle2X, obstacle2Y, 50, 50);
}

boolean checkCollision() {
  if (characterX + 70 > obstacle1X && characterX < obstacle1X + 50 &&
      characterY + 70 > obstacle1Y && characterY < obstacle1Y + 50) {
    return true;
  }
  if (characterX + 70 > obstacle2X && characterX < obstacle2X + 50 &&
      characterY + 70 > obstacle2Y && characterY < obstacle2Y + 50) {
    return true;
  }
  return false;
}

void endGame() {
  background(0);
  textSize(32);
  textAlign(CENTER, CENTER);
  fill(255);
  text("Game Over", width/2, height/2);
}