Skip to content

Instantly share code, notes, and snippets.

@claytical
Created November 17, 2015 16:25
Show Gist options
  • Save claytical/a3ff7292869ab3c5723a to your computer and use it in GitHub Desktop.
Save claytical/a3ff7292869ab3c5723a to your computer and use it in GitHub Desktop.
Simple p5.play collision
var player;
var enemy;
var angle;
var speed;
function setup() {
createCanvas(windowWidth,windowHeight);
player = createSprite(0, 0, 50, 50);
enemy = createSprite(width/2, height/2, 100, 100);
speed = 1;
angle = 270;
}
function draw() {
background(255,255,255);
enemy.setSpeed(speed, angle);
drawSprites();
checkForWallCollisions();
player.position.x = mouseX;
player.position.y = mouseY;
if (player.overlap(enemy)) {
alert("You hit the enemy!");
}
}
function checkForWallCollisions() {
if (enemy.position.x > width) {
angle = 180;
}
if (enemy.position.x < 0) {
angle = 0;
}
if (enemy.position.y > height) {
angle = 270;
}
if (enemy.position.y < 0) {
angle = 90;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment