GameTheory

mail@pastecode.io avatar
unknown
java
a year ago
2.1 kB
2
Indexable
Never
int canvasWidth = 700;
int canvasHeight = 700;

int charWidth = 70;
int charHeight = 70;
int charX = canvasWidth/2 - charWidth/2;
int charY = canvasHeight/2 - charHeight/2;

int charSpeed = 70;

int obstacle1X, obstacle1Y, obstacle1Width = 50, obstacle1Height = 100;
int obstacle2X, obstacle2Y, obstacle2Width = 100, obstacle2Height = 50;

void setup() {
  size(canvasWidth, canvasHeight);
  obstacle1X = int(random(canvasWidth - obstacle1Width));
  obstacle1Y = int(random(canvasHeight - obstacle1Height));
  obstacle2X = int(random(canvasWidth - obstacle2Width));
  obstacle2Y = int(random(canvasHeight - obstacle2Height));
}

void draw() {
  background(0);
  fill(255);
  rect(obstacle1X, obstacle1Y, obstacle1Width, obstacle1Height);
  rect(obstacle2X, obstacle2Y, obstacle2Width, obstacle2Height);
  
  // check collision with obstacles
  if (collideRectRect(charX, charY, charWidth, charHeight, obstacle1X, obstacle1Y, obstacle1Width, obstacle1Height) ||
      collideRectRect(charX, charY, charWidth, charHeight, obstacle2X, obstacle2Y, obstacle2Width, obstacle2Height)) {
    background(0);
    textSize(64);
    textAlign(CENTER, CENTER);
    fill(255, 0, 0);
    text("GAME OVER", canvasWidth/2, canvasHeight/2);
    noLoop();
  }
  
  // draw character and web trail
  noStroke();
  fill(255, 0, 0);
  ellipse(charX + charWidth/2, charY + charHeight/2, charWidth, charHeight);
  stroke(255);
  line(charX + charWidth/2, charY + charHeight/2, charX + charWidth/2, 0);
  
  // move character using arrow keys
  if (keyPressed) {
    if (keyCode == UP && charY > 0) {
      charY -= charSpeed;
    } else if (keyCode == DOWN && charY < canvasHeight - charHeight) {
      charY += charSpeed;
    } else if (keyCode == LEFT && charX > 0) {
      charX -= charSpeed;
    } else if (keyCode == RIGHT && charX < canvasWidth - charWidth) {
      charX += charSpeed;
    }
  }
  
  // wrap character around the canvas boundaries
  if (charX < 0) {
    charX = canvasWidth - charWidth;
  } else if (charX > canvasWidth - charWidth) {
    charX = 0;
  }
}