Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setup() {
- createCanvas(800, 600);
- var entities = [player, obs1, obs2]
- var drawables = [player, obs1, obs2, clickedCircle, exit];
- var movables = [player, obs1, obs2];
- noStroke()
- }
- function draw() {
- background(100);
- //Iterate through systems;
- for (var drawable of drawables) {
- drawable.drawCallback();
- }
- for (var movable of movables) {
- movable.moveCallback();
- }
- for (var entity of entities) {
- checkEdges(entity);
- }
- checkForExit();
- }
- var player = {
- rgb: [255,204,0],
- x: 50,
- y: 50,
- diameter: 25,
- drawCallback: function() {
- fill.apply(null,player.rgb);
- circle(player.x, player.y, player.diameter);
- },
- moveCallback: function() {
- if (keyIsDown(83)) {
- player.y += 10;
- }
- else if (keyIsDown(87)) {
- player.y -= 10;
- }
- if (keyIsDown(65)) {
- player.x -= 10;
- }
- else if (keyIsDown(68)) {
- player.x += 10;
- }
- }
- }
- var obs1 = {
- rgb: [165,33,233],
- x: 83,
- y: 76,
- size: 62,
- drawCallback: function () { fill.apply(null,this.rgb); circle(this.x, this.y, this.size) },
- moveCallback: function () {
- this.x = this.x + random(30);
- this.x = this.x - random(30);
- this.y = this.y + random(30);
- this.y = this.y - random(30);
- }
- }
- var obs2 = {
- rgb: [65,42,233],
- x: 46,
- y: 98,
- size: 90,
- drawCallback: function () { fill.apply(null,this.rgb); square(this.x, this.y, this.size) },
- moveCallback: function () {
- this.x = this.x + random(30);
- this.x = this.x - random(30);
- this.y = this.y + random(30);
- this.y = this.y - random(30);
- }
- }
- function checkEdges(entity) {
- if (entity.x > 800) {
- entity.x = entity.x - 800;
- } else if (entity.x < 0) {
- entity.x = entity.x + 800;
- }
- if (entity.y > 600) {
- entity.y = entity.y - 600;
- } else if (entity.y < 0) {
- entity.y = entity.y + 600;
- }
- }
- var clickedCircle = {
- x: 0,
- y: 0,
- drawCallback: function() { fill(50,86,10);circle(this.x, this.y, 30) }
- }
- function mousePressed() {
- clickedCircle.x = mouseX;
- clickedCircle.y = mouseY;
- }
- var exit = {
- x1: 750,
- y1: 550,
- x2: 800,
- y2: 600,
- drawCallback: function () {
- fill(255, 0, 0)
- rect(exit.x1, exit.y1, 50, 50)
- fill(0, 0, 0)
- textSize(12)
- text('Exit', 765, 580)
- }
- }
- function checkForExit() {
- if (player.x < exit.x2 && player.x > exit.x1 && player.y < exit.y2 && player.y > exit.y1) {
- fill(255, 0, 0);
- rect(0, 0, 800, 600);
- fill(0, 0, 0);
- textSize(72);
- text('YOU WIN!', 200, 300);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement