how do i create a game using javascript and p5?

You can place all the objects with respect to the centre, using the formula: xpos = distance * sin(angle), ypos = distance * cos(angle); Here’s an example you could refer to:

let a;
let dist = 50;

function setup() {
  createCanvas(400, 400);
  rectMode(CENTER);
  a = PI;
}

function draw() {
  background(220);
  translate(width / 2, height / 2);
  let x = dist * sin(a);
  let y = dist * cos(a);
  rect(x, y, 20, 20);
  line(0, 0, x, y);
}

function keyTyped() {
  if (key == 'q' || key == 'Q')
    a += PI / 2;
  if (key == 'e' || key == 'E')
    a -= PI / 2;
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top