Created
November 17, 2015 16:25
-
-
Save claytical/a3ff7292869ab3c5723a to your computer and use it in GitHub Desktop.
Simple p5.play collision
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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